Tasks/views/deleteViews.go

128 lines
3.2 KiB
Go
Raw Normal View History

2016-01-31 14:20:01 +08:00
package views
import (
"log"
"net/http"
"strconv"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2016-01-31 14:20:01 +08:00
)
//TrashTaskFunc is used to populate the trash tasks
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil {
log.Println(err)
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/trash", http.StatusBadRequest)
2016-01-31 14:20:01 +08:00
} else {
err = db.TrashTask(id)
if err != nil {
message = "Error trashing task"
} else {
message = "Task trashed"
}
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/trash", http.StatusFound)
2016-01-31 14:20:01 +08:00
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/trash", http.StatusFound)
}
}
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil {
log.Println(err)
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/deleted", http.StatusBadRequest)
2016-01-31 14:20:01 +08:00
} else {
err = db.RestoreTask(id)
if err != nil {
message = "Restore failed"
} else {
message = "Task restored"
}
http.Redirect(w, r, "/deleted/", http.StatusFound)
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/", http.StatusBadRequest)
2016-01-31 14:20:01 +08:00
} else {
2016-02-01 21:45:04 +08:00
task, err := db.GetTaskByID(id)
if err != nil {
task.Message = "Error fetching Tasks"
}
2016-01-31 14:20:01 +08:00
editTemplate.Execute(w, task)
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id := r.URL.Path[len("/delete/"):]
if id == "all" {
2016-02-01 21:45:04 +08:00
err := db.DeleteAll()
if err != nil {
message = "Error deleting tasks"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
2016-01-31 14:20:01 +08:00
http.Redirect(w, r, "/", http.StatusFound)
} else {
id, err := strconv.Atoi(id)
if err != nil {
log.Println(err)
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/", http.StatusBadRequest)
2016-01-31 14:20:01 +08:00
} else {
err = db.DeleteTask(id)
if err != nil {
message = "Error deleting task"
} else {
message = "Task deleted"
}
http.Redirect(w, r, "/", http.StatusFound)
}
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}
//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 {
log.Println(err)
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/completed", http.StatusBadRequest)
2016-01-31 14:20:01 +08:00
} else {
err = db.RestoreTaskFromComplete(id)
if err != nil {
message = "Restore failed"
} else {
message = "Task restored"
}
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/completed", http.StatusFound)
2016-01-31 14:20:01 +08:00
}
} else {
message = "Method not allowed"
2016-02-01 21:45:04 +08:00
http.Redirect(w, r, "/completed", http.StatusFound)
2016-01-31 14:20:01 +08:00
}
}