per category count in sidebar

This commit is contained in:
Suraj 2016-02-05 01:09:36 +05:30
parent 74e60b5062
commit c21afbe7a8
5 changed files with 26 additions and 9 deletions

View File

@ -5,6 +5,8 @@ stores the functions related to file IO
*/ */
import ( import (
"log" "log"
"github.com/thewhitetulip/Tasks/types"
) )
// AddFile is used to add the md5 of a file name which is uploaded to our application // AddFile is used to add the md5 of a file name which is uploaded to our application
@ -43,14 +45,14 @@ func GetFileName(token string) (string, error) {
//GetCategories will return the list of categories to be //GetCategories will return the list of categories to be
//rendered in the template //rendered in the template
func GetCategories() []string { func GetCategories() []types.CategoryCount {
stmt := "select name from category" stmt := "select c.name, count(*) from category c left outer join task t where c.id = t.cat_id group by name union select name, 0 from category where name not in (select distinct name from task t join category c on t.cat_id = c.id)"
rows := database.query(stmt) rows := database.query(stmt)
var categories []string var categories []types.CategoryCount
var category string var category types.CategoryCount
for rows.Next() { for rows.Next() {
err := rows.Scan(&category) err := rows.Scan(&category.Name, &category.Count)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }

View File

@ -17,6 +17,12 @@ ul{
list-style-type: none; list-style-type: none;
} }
.badge{
background-color: #7D8EF0;
margin-right:10px;
float:right;
}
.center{ .center{
text-align:center; text-align:center;
} }

View File

@ -116,7 +116,7 @@
{{ range $index, $cat := .Categories }} {{ range $index, $cat := .Categories }}
<li class="sidebar-item"> <li class="sidebar-item">
<a href="/category/{{$cat}}" {{ if eq $cat $nav }} class="active" {{end}}> <span class="glyphicon glyphicon-stop"></span> <span class="nav-item"> {{$cat}}</span></a> <a href="/category/{{$cat.Name}}" {{ if eq $cat.Name $nav }} class="active" {{end}}> <span class="glyphicon glyphicon-stop"></span> <span class="nav-item"> {{$cat.Name}}</span> <span class="badge pull-right">{{$cat.Count}}</span></a>
</li> </li>
{{end}} {{end}}
</ul> </ul>

View File

@ -24,8 +24,8 @@
Category: Category:
<select name="category"> <select name="category">
<option>---</option> <option>---</option>
{{range $cat := $categories}} {{range $index, $cat := $categories}}
<option value="{{$cat}}" {{if eq $cat $task.Category}} selected="true" {{end}}> {{$cat}} </option> <option value="{{$cat.Name}}" {{if eq $cat.Name $task.Category}} selected="true" {{end}}> {{$cat.Name}} </option>
{{end}} {{end}}
</select> </select>
</div> </div>

View File

@ -21,5 +21,14 @@ type Context struct {
Search string Search string
Message string Message string
CSRFToken string CSRFToken string
Categories []string Categories []CategoryCount
}
//CategoryCount is the struct used to populate the sidebar
//which contains the category name and the count of the tasks
//in each category
type CategoryCount struct {
Name string
Count int
} }