diff --git a/db/files.go b/db/files.go
index 0c65159..d1c7b18 100644
--- a/db/files.go
+++ b/db/files.go
@@ -40,3 +40,43 @@ func GetFileName(token string) (string, error) {
return fileName, nil
}
+
+//GetCategories will return the list of categories to be
+//rendered in the template
+func GetCategories() []string {
+ stmt := "select name from category"
+ rows := database.query(stmt)
+ var categories []string
+ var category string
+
+ for rows.Next() {
+ err := rows.Scan(&category)
+ if err != nil {
+ log.Println(err)
+ }
+ categories = append(categories, category)
+ }
+ return categories
+}
+
+//AddCategory is used to add the task in the database
+func AddCategory(category string) error {
+ err := taskQuery("insert into category(name) values(?)", category)
+ return err
+}
+
+// GetCategoryById will return the ID of that category passed as args
+// used while inserting tasks into the table
+func GetCategoryById(category string) int {
+ stmt := "select id from category where name=?"
+ rows := database.query(stmt, category)
+ var categoryID int
+
+ for rows.Next() {
+ err := rows.Scan(&categoryID)
+ if err != nil {
+ log.Println(err)
+ }
+ }
+ return categoryID
+}
diff --git a/db/tasks.go b/db/tasks.go
index 985109d..2e7df69 100644
--- a/db/tasks.go
+++ b/db/tasks.go
@@ -67,7 +67,7 @@ func Close() {
//GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed
-func GetTasks(status string) (types.Context, error) {
+func GetTasks(status, category string) (types.Context, error) {
var task []types.Task
var context types.Context
var TaskID int
@@ -76,9 +76,10 @@ func GetTasks(status string) (types.Context, error) {
var TaskCreated time.Time
var TaskPriority string
var getTasksql string
+ var rows *sql.Rows
- basicSQL := "select id, title, content, created_date, priority from task "
- if status == "pending" {
+ basicSQL := "select id, title, content, created_date, priority from task t"
+ if status == "pending" && category == "" {
getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, created_date asc"
} else if status == "deleted" {
getTasksql = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc"
@@ -86,7 +87,16 @@ func GetTasks(status string) (types.Context, error) {
getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc"
}
- rows := database.query(getTasksql)
+ if category != "" {
+ status = category
+ getTasksql = "select t.id, title, content, created_date, priority from task t, category c where c.id = t.cat_id and name = ?"
+ rows, err = database.db.Query(getTasksql, category)
+ if err != nil {
+ log.Println("something went wrong while getting query")
+ }
+ } else {
+ rows = database.query(getTasksql)
+ }
defer rows.Close()
for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
@@ -161,8 +171,14 @@ func DeleteTask(id int) error {
}
//AddTask is used to add the task in the database
-func AddTask(title, content string, taskPriority int) error {
- err := taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority)
+func AddTask(title, content, category string, taskPriority int) error {
+ var err error
+ if category == "" {
+ err = taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority)
+ } else {
+ categoryID := GetCategoryById(category)
+ err = taskQuery("insert into task(title, content, priority, created_date, last_modified_at, cat_id) values(?,?,?,datetime(), datetime(), ?)", title, content, taskPriority, categoryID)
+ }
return err
}
diff --git a/main.go b/main.go
index 6cec264..3a8f5e8 100644
--- a/main.go
+++ b/main.go
@@ -16,6 +16,8 @@ func main() {
values := config.ReadConfig("config.json")
views.PopulateTemplates()
http.HandleFunc("/", views.ShowAllTasksFunc)
+ http.HandleFunc("/add-category/", views.AddCategoryFunc)
+ http.HandleFunc("/category/", views.ShowCategoryFunc)
http.HandleFunc("/complete/", views.CompleteTaskFunc)
//delete permanently deletes from db
http.HandleFunc("/delete/", views.DeleteTaskFunc)
diff --git a/public/templates/_head.html b/public/templates/_head.html
index fb6d402..0b60555 100644
--- a/public/templates/_head.html
+++ b/public/templates/_head.html
@@ -8,6 +8,7 @@
{{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted
{{ else if eq .Navigation "edit"}} Edit
+ {{else }} {{.Navigation}}
{{end}}
@@ -49,11 +50,17 @@
{{if .Search}} Results for: {{.Search}}
{{else}}
+ href='{{ if eq .Navigation "pending"}} /
+ {{else if eq .Navigation "completed"}} /{{.Navigation}}
+ {{else if eq .Navigation "deleted"}} /{{.Navigation}}
+ {{else if eq .Navigation "edit"}} /{{.Navigation}}
+ {{else}} /category/{{.Navigation}}
+ {{end}}'>
{{if eq .Navigation "pending"}} Pending
{{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted
{{ else if eq .Navigation "edit"}} Edit
+ {{else }} {{.Navigation}}
{{end}}
{{end}}
@@ -77,16 +84,38 @@
diff --git a/public/templates/home.html b/public/templates/home.html
index 5cb78f8..a2d6031 100644
--- a/public/templates/home.html
+++ b/public/templates/home.html
@@ -27,6 +27,13 @@
Medium
Low
+
+