diff --git a/db/db.go b/db/db.go index afa710e..1e6f06c 100644 --- a/db/db.go +++ b/db/db.go @@ -168,6 +168,26 @@ func RestoreTask(id int) error { return err } +//RestoreTaskFromComplete is used to restore tasks from the Trash +func RestoreTaskFromComplete(id int) error { + restoreSQL, err := database.Prepare("update task set is_deleted='N',finish_date=null,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(restoreSQL).Exec(id) + if err != nil { + fmt.Println("doing rollback") + tx.Rollback() + } else { + tx.Commit() + } + return err +} + //DeleteTask is used to delete the task from the database func DeleteTask(id int) error { deleteSQL, err := database.Prepare("delete from task where id = ?") diff --git a/main.go b/main.go index 7ff258f..9499db1 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ func main() { http.HandleFunc("/edit/", views.EditTaskFunc) http.HandleFunc("/completed/", views.ShowCompleteTasksFunc) http.HandleFunc("/restore/", views.RestoreTaskFunc) + http.HandleFunc("/incomplete/", views.RestoreFromCompleteFunc) http.HandleFunc("/add/", views.AddTaskFunc) http.HandleFunc("/update/", views.UpdateTaskFunc) http.HandleFunc("/search/", views.SearchTaskFunc) diff --git a/public/templates/completed.html b/public/templates/completed.html index f29538a..41baea6 100644 --- a/public/templates/completed.html +++ b/public/templates/completed.html @@ -8,11 +8,15 @@

{{.Content}}

diff --git a/views/views.go b/views/views.go index a0a0802..93e72ba 100644 --- a/views/views.go +++ b/views/views.go @@ -236,6 +236,27 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) { } } +//RestoreFromCompleteFunc restores the task from complete to pending +func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):]) + if err != nil { + fmt.Println(err) + } else { + err = db.RestoreTaskFromComplete(id) + if err != nil { + message = "Restore failed" + } else { + message = "Task restored" + } + http.Redirect(w, r, "/pending/", http.StatusFound) + } + } else { + message = "Method not allowed" + http.Redirect(w, r, "/", http.StatusFound) + } +} + //UpdateTaskFunc is used to update a task, handes "/update/" URL func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" {