From cde24f7ee9cd96e7fa90ea5e97562fddf0718fd6 Mon Sep 17 00:00:00 2001 From: thewhitetulip Date: Sat, 14 Nov 2015 16:26:53 +0530 Subject: [PATCH] diffrentiate between delete & complete --- db/db.go | 31 ++++++++-- main.go | 44 +++++++++++--- templates/completed.gtpl | 128 +++++++++++++++++++++++++++++++++++++++ templates/deleted.gtpl | 11 ++-- templates/home.gtpl | 48 +++------------ viewmodels/viewmodels.go | 16 +++-- 6 files changed, 217 insertions(+), 61 deletions(-) create mode 100644 templates/completed.gtpl diff --git a/db/db.go b/db/db.go index d8a03dd..4a46186 100644 --- a/db/db.go +++ b/db/db.go @@ -23,17 +23,19 @@ func Close() { database.Close() } -func GetTasks(deleted bool) []types.Task { +func GetTasks(status string) []types.Task { var task []types.Task var TaskId int var TaskTitle string var TaskContent string var TaskCreated time.Time var getTasksql string - if deleted == true { - getTasksql = "select id, title, content, created_date from task where is_deleted!='Y' order by created_date asc" - } else { + if status == "pending" { + getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc" + } else if status == "trashed" { getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc" + } else if status == "complete" { + getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc" } rows, err := database.Query(getTasksql) @@ -76,7 +78,26 @@ func GetTaskById(id int) types.Task { return task } -func ArchiveTask(id int) error { +func TrashTask(id int) error { + trashSql, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?") + if err != nil { + fmt.Println(err) + } + tx, err := database.Begin() + if err != nil { + fmt.Println(err) + } + _, err = tx.Stmt(trashSql).Exec(id) + if err != nil { + fmt.Println("doing rollback") + tx.Rollback() + } else { + tx.Commit() + } + return err +} + +func CompleteTask(id int) error { stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?") if err != nil { fmt.Println(err) diff --git a/main.go b/main.go index 6d7701e..068fb3c 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( var homeTemplate *template.Template var deletedTemplate *template.Template +var completedTemplate *template.Template var editTemplate *template.Template var searchTemplate *template.Template var err error @@ -39,12 +40,19 @@ func main() { if err != nil { fmt.Println(err) } + completedTemplate, err = template.ParseFiles("./templates/completed.gtpl") + if err != nil { + fmt.Println(err) + } + router := httprouter.New() router.GET("/", ShowAllTasks) - router.GET("/archive/:id", ArchiveTask) + router.GET("/complete/:id", CompleteTask) router.GET("/delete/:id", DeleteTask) + router.GET("/deleted/", ShowTrashTask) + router.GET("/trash/:id", TrashTask) router.GET("/edit/:id", EditTask) - router.GET("/trash/", ShowTrashTask) + router.GET("/complete/", ShowCompleteTasks) router.GET("/restore/:id", RestoreTask) router.POST("/add/", AddTask) router.POST("/update/", UpdateTask) @@ -55,10 +63,15 @@ func main() { } func ShowAllTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) { - context := viewmodels.GetTasks(true) //true when you want non deleted notes + context := viewmodels.GetTasks("pending") //true when you want non deleted notes homeTemplate.Execute(w, context) } +func ShowTrashTask(w http.ResponseWriter, r *http.Request, parm httprouter.Params) { + context := viewmodels.GetTasks("trashed") //false when you want deleted notes + deletedTemplate.Execute(w, context) +} + func SearchTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { r.ParseForm() query := r.Form.Get("query") @@ -76,9 +89,9 @@ func AddTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { } } -func ShowTrashTask(w http.ResponseWriter, r *http.Request, parm httprouter.Params) { - context := viewmodels.GetTasks(false) //false when you want finished notes - deletedTemplate.Execute(w, context) +func ShowCompleteTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) { + context := viewmodels.GetTasks("complete") //false when you want finished notes + completedTemplate.Execute(w, context) } func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { @@ -91,12 +104,12 @@ func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { } } -func ArchiveTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { +func CompleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { id, err := strconv.Atoi(param.ByName("id")) if err != nil { fmt.Println(err) } else { - viewmodels.ArchiveTask(id) + viewmodels.CompleteTask(id) http.Redirect(w, r, "/", http.StatusFound) } } @@ -112,18 +125,29 @@ func DeleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) fmt.Println(err) } else { viewmodels.DeleteTask(id) - http.Redirect(w, r, "/trash/", http.StatusFound) + http.Redirect(w, r, "/deleted/", http.StatusFound) } } } +func TrashTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { + id, err := strconv.Atoi(param.ByName("id")) + if err != nil { + fmt.Println(err) + } else { + fmt.Println("deleting ", id) + viewmodels.TrashTask(id) + http.Redirect(w, r, "/", http.StatusFound) + } +} + func RestoreTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) { id, err := strconv.Atoi(param.ByName("id")) if err != nil { fmt.Println(err) } else { viewmodels.RestoreTask(id) - http.Redirect(w, r, "/trash/", http.StatusFound) + http.Redirect(w, r, "/", http.StatusFound) } } diff --git a/templates/completed.gtpl b/templates/completed.gtpl new file mode 100644 index 0000000..e30c946 --- /dev/null +++ b/templates/completed.gtpl @@ -0,0 +1,128 @@ + + + + + + + Completed Tasks + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ {{ if .}} + {{range .}} +
+

{{.Title}}


+

{{.Content}}

+ + + +
+ {{end}} + {{else}} +

No tasks here

+ {{end}} +
+ + + + + diff --git a/templates/deleted.gtpl b/templates/deleted.gtpl index f8b58d2..87a1206 100644 --- a/templates/deleted.gtpl +++ b/templates/deleted.gtpl @@ -35,7 +35,7 @@ - + @@ -61,14 +61,17 @@