2016-01-31 14:20:01 +08:00
|
|
|
package views
|
|
|
|
|
2016-02-01 22:34:19 +08:00
|
|
|
/*
|
|
|
|
Holds the delete related view handlers
|
|
|
|
*/
|
|
|
|
|
2016-01-31 14:20:01 +08:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2016-02-01 21:45:04 +08:00
|
|
|
|
|
|
|
"github.com/thewhitetulip/Tasks/db"
|
2016-05-09 10:11:05 +08:00
|
|
|
"github.com/thewhitetulip/Tasks/sessions"
|
2016-02-06 18:02:46 +08:00
|
|
|
"github.com/thewhitetulip/Tasks/utils"
|
2016-01-31 14:20:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
//TrashTaskFunc is used to populate the trash tasks
|
|
|
|
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
2016-02-01 22:26:06 +08:00
|
|
|
//for best UX we want the user to be returned to the page making
|
|
|
|
//the delete transaction, we use the r.Referer() function to get the link
|
2016-05-10 01:36:35 +08:00
|
|
|
redirectURL := utils.GetRedirectUrl(r.Referer())
|
2016-02-01 22:26:06 +08:00
|
|
|
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
2016-01-31 14:20:01 +08:00
|
|
|
if err != nil {
|
2016-05-10 01:36:35 +08:00
|
|
|
log.Println("TrashTaskFunc", err)
|
|
|
|
message = "Incorrect command"
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
err = db.TrashTask(id)
|
|
|
|
if err != nil {
|
|
|
|
message = "Error trashing task"
|
|
|
|
} else {
|
|
|
|
message = "Task trashed"
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
|
|
|
|
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
2016-01-31 14:20:01 +08:00
|
|
|
if err != nil {
|
2016-05-10 01:36:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/deleted", http.StatusBadRequest)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
err = db.RestoreTask(id)
|
|
|
|
if err != nil {
|
|
|
|
message = "Restore failed"
|
|
|
|
} else {
|
|
|
|
message = "Task restored"
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
|
|
|
|
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id := r.URL.Path[len("/delete/"):]
|
|
|
|
if id == "all" {
|
|
|
|
err := db.DeleteAll()
|
|
|
|
if err != nil {
|
|
|
|
message = "Error deleting tasks"
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
id, err := strconv.Atoi(id)
|
2016-01-31 14:20:01 +08:00
|
|
|
if err != nil {
|
2016-05-10 01:36:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
err = db.DeleteTask(id)
|
|
|
|
if err != nil {
|
|
|
|
message = "Error deleting task"
|
|
|
|
} else {
|
|
|
|
message = "Task deleted"
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/deleted", http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//RestoreFromCompleteFunc restores the task from complete to pending
|
|
|
|
func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
|
2016-01-31 14:20:01 +08:00
|
|
|
if err != nil {
|
2016-05-10 01:36:35 +08:00
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/completed", http.StatusBadRequest)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
err = db.RestoreTaskFromComplete(id)
|
|
|
|
if err != nil {
|
|
|
|
message = "Restore failed"
|
|
|
|
} else {
|
|
|
|
message = "Task restored"
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/completed", http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-06 14:58:00 +08:00
|
|
|
|
|
|
|
//DeleteCategoryFunc will delete any category
|
|
|
|
func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
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"
|
|
|
|
}
|
2016-02-06 14:58:00 +08:00
|
|
|
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-02-06 14:58:00 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 00:53:15 +08:00
|
|
|
|
|
|
|
//DeleteCommentFunc will delete any category
|
|
|
|
func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-10 01:36:35 +08:00
|
|
|
if sessions.IsLoggedIn(r) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id := r.URL.Path[len("/del-comment/"):]
|
|
|
|
commentID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-02-17 00:53:15 +08:00
|
|
|
|
2016-05-10 01:36:35 +08:00
|
|
|
err = db.DeleteCommentByID(commentID)
|
2016-02-17 00:53:15 +08:00
|
|
|
|
2016-05-10 01:36:35 +08:00
|
|
|
if err != nil {
|
|
|
|
message = "comment not deleted"
|
|
|
|
} else {
|
|
|
|
message = "comment deleted"
|
|
|
|
}
|
2016-02-17 00:53:15 +08:00
|
|
|
|
2016-05-10 01:36:35 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/login/", 302)
|
2016-02-17 00:53:15 +08:00
|
|
|
}
|
|
|
|
}
|