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 (
"log"
"github.com/thewhitetulip/Tasks/types"
)
// 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
//rendered in the template
func GetCategories() []string {
stmt := "select name from category"
func GetCategories() []types.CategoryCount {
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)
var categories []string
var category string
var categories []types.CategoryCount
var category types.CategoryCount
for rows.Next() {
err := rows.Scan(&category)
err := rows.Scan(&category.Name, &category.Count)
if err != nil {
log.Println(err)
}

View File

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

View File

@ -116,7 +116,7 @@
{{ range $index, $cat := .Categories }}
<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>
{{end}}
</ul>

View File

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

View File

@ -21,5 +21,14 @@ type Context struct {
Search string
Message 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
}