update/delete categories now possible

This commit is contained in:
Suraj 2016-02-06 12:28:00 +05:30
parent 58d5d1b39b
commit 8aef7693a8
6 changed files with 70 additions and 5 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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)
}
}

View File

@ -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)
}
}