//EditTaskFunc is used to edit tasks, handles "/edit/" URL func EditTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { http.Redirect(w, r, "/", http.StatusBadRequest) return } id, err := strconv.Atoi(r.URL.Path[len("/edit/"):]) if err != nil { log.Println(err) http.Redirect(w, r, "/", http.StatusBadRequest) return } redirectURL := utils.GetRedirectUrl(r.Referer()) username := sessions.GetCurrentUserName(r) task, err := db.GetTaskByID(username, id) categories := db.GetCategories(username) task.Categories = categories task.Referer = redirectURL if err != nil { task.Message = "Error fetching Tasks" } editTemplate.Execute(w, task) }
//TrashTaskFunc is used to populate the trash tasks func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { //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 redirectURL := utils.GetRedirectUrl(r.Referer()) if r.Method != "GET" { http.Redirect(w, r, "/", http.StatusBadRequest) return } id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if err != nil { log.Println("TrashTaskFunc", err) message = "Incorrect command" http.Redirect(w, r, redirectURL, http.StatusFound) } else { username := sessions.GetCurrentUserName(r) err = db.TrashTask(username, id) if err != nil { message = "Error trashing task" } else { message = "Task trashed" } http.Redirect(w, r, redirectURL, http.StatusFound) } }
//TrashTaskFunc is used to populate the trash tasks func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { //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 redirectUrl := utils.GetRedirectUrl(r.Referer()) if r.Method == "GET" { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if err != nil { log.Println("TrashTaskFunc", err) message = "Incorrect command" http.Redirect(w, r, redirectUrl, http.StatusFound) } else { err = db.TrashTask(id) if err != nil { message = "Error trashing task" } else { message = "Task trashed" } http.Redirect(w, r, redirectUrl, http.StatusFound) } } else { message = "Method not allowed" http.Redirect(w, r, redirectUrl, http.StatusFound) } }
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { redirectURL := utils.GetRedirectUrl(r.Referer()) id, err := strconv.Atoi(r.URL.Path[len("/complete/"):]) if err != nil { log.Println(err) } else { err = db.CompleteTask(id) if err != nil { message = "Complete task failed" } else { message = "Task marked complete" } http.Redirect(w, r, redirectURL, http.StatusFound) } } else { message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } }
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { http.Redirect(w, r, "/", http.StatusBadRequest) return } redirectURL := utils.GetRedirectUrl(r.Referer()) id, err := strconv.Atoi(r.URL.Path[len("/complete/"):]) if err != nil { log.Println(err) } else { username := sessions.GetCurrentUserName(r) err = db.CompleteTask(username, id) if err != nil { message = "Complete task failed" } else { message = "Task marked complete" } http.Redirect(w, r, redirectURL, 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) http.Redirect(w, r, "/", http.StatusBadRequest) } else { redirectUrl := utils.GetRedirectUrl(r.Referer()) task, err := db.GetTaskByID(id) categories := db.GetCategories() task.Categories = categories task.Referer = redirectUrl if err != nil { task.Message = "Error fetching Tasks" } editTemplate.Execute(w, task) } } else { message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } }