diff --git a/db/files.go b/db/files.go index 572ca80..58e97df 100644 --- a/db/files.go +++ b/db/files.go @@ -1,7 +1,7 @@ package db /* -stores the functions related to file IO +stores the functions related to file IO and category */ import ( "log" @@ -67,9 +67,9 @@ func AddCategory(category string) error { return err } -// GetCategoryById will return the ID of that category passed as args +// GetCategoryByName will return the ID of that category passed as args // used while inserting tasks into the table -func GetCategoryById(category string) int { +func GetCategoryByName(category string) int { stmt := "select id from category where name=?" rows := database.query(stmt, category) var categoryID int @@ -82,3 +82,28 @@ func GetCategoryById(category string) int { } return categoryID } + +//DeleteCategoryByName will be used to delete a category from the category page +func DeleteCategoryByName(category string) error { + //first we delete entries from task and then from category + categoryID := GetCategoryByName(category) + query := "update task set cat_id = null where id =?" + err := taskQuery(query, categoryID) + if err == nil { + err = taskQuery("delete from category where id=?", categoryID) + if err != nil { + return err + } + } + return err +} + +//UpdateCategoryByName will be used to delete a category from the category page +func UpdateCategoryByName(oldName, newName string) error { + query := "update category set name = ? where name=?" + log.Println(query) + err := taskQuery(query, newName, oldName) + + return err + +} diff --git a/db/tasks.go b/db/tasks.go index 6ee9250..2065a77 100644 --- a/db/tasks.go +++ b/db/tasks.go @@ -176,7 +176,7 @@ func AddTask(title, content, category string, taskPriority int) 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) + categoryID := GetCategoryByName(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 3a8f5e8..6d0b479 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,8 @@ func main() { views.PopulateTemplates() http.HandleFunc("/", views.ShowAllTasksFunc) http.HandleFunc("/add-category/", views.AddCategoryFunc) + http.HandleFunc("/del-category/", views.DeleteCategoryFunc) + http.HandleFunc("/upd-category/", views.UpdateCategoryFunc) http.HandleFunc("/category/", views.ShowCategoryFunc) http.HandleFunc("/complete/", views.CompleteTaskFunc) //delete permanently deletes from db diff --git a/views/addViews.go b/views/addViews.go index f59bbd9..3f8c25d 100644 --- a/views/addViews.go +++ b/views/addViews.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "strconv" + "strings" "text/template" "time" @@ -122,7 +123,7 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) { func AddCategoryFunc(w http.ResponseWriter, r *http.Request) { r.ParseForm() category := r.Form.Get("category") - if category != "" { + if strings.Trim(category, " ") != "" { err := db.AddCategory(category) if err != nil { message = "Error adding category" diff --git a/views/deleteViews.go b/views/deleteViews.go index c227edc..5b2e326 100644 --- a/views/deleteViews.go +++ b/views/deleteViews.go @@ -121,3 +121,18 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/completed", http.StatusFound) } } + +//DeleteCategoryFunc will delete any category +func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + categoryName := r.URL.Path[len("/del-category/"):] + err := db.DeleteCategoryByName(categoryName) + if err != nil { + message = "error deleting category" + } else { + message = "Category " + categoryName + " deleted" + } + + http.Redirect(w, r, "/", http.StatusFound) + } +} diff --git a/views/otherViews.go b/views/otherViews.go index aaae3c9..e1ce230 100644 --- a/views/otherViews.go +++ b/views/otherViews.go @@ -113,3 +113,25 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusFound) } } + +//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL +func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + var redirectURL string + r.ParseForm() + oldName := r.URL.Path[len("/upd-category/"):] + newName := r.Form.Get("catname") + + err := db.UpdateCategoryByName(oldName, newName) + if err != nil { + message = "error updating category" + log.Println("not updated category " + oldName) + redirectURL = "/category/" + oldName + } else { + message = "cat " + oldName + " -> " + newName + redirectURL = "/category/" + newName + } + log.Println("redirecting to " + redirectURL) + http.Redirect(w, r, redirectURL, http.StatusFound) + } +}