//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 var redirectUrl string redirect := strings.Split(r.Referer(), "/") index := len(redirect) - 1 if len(redirect) == 4 { redirectUrl = "/" } else { redirectUrl = redirect[index] } if r.Method == "GET" { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if err != nil { log.Println("TrashTaskFunc", err) http.Redirect(w, r, redirectUrl, http.StatusBadRequest) } 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) } }
//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) } }
//DeleteTaskFuncAPI will add the tasks for the user func DeleteTaskFuncAPI(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { token := r.Header["Token"][0] w.Header().Set("Content-Type", "application/json; charset=UTF-8") IsTokenValid, username := ValidateToken(token) //When the token is not valid show the default error JSON document if !IsTokenValid { status := types.Status{StatusCode: http.StatusInternalServerError, Message: message} w.WriteHeader(http.StatusInternalServerError) err = json.NewEncoder(w).Encode(status) if err != nil { panic(err) } return } log.Println("token is valid " + username + " is logged in") var strtaskID string strtaskID = r.URL.Path[len("/api/delete-task/"):] var statusCode int var message string taskID, err := strconv.Atoi(strtaskID) if err != nil { log.Println("invalid task id") statusCode = http.StatusBadRequest message = "invalid TaskID" } else { db.TrashTask(username, taskID) if err != nil { statusCode = http.StatusOK message = "deleted task id " + strtaskID } else { statusCode = http.StatusOK message = "deleted task id " + strtaskID } } status := types.Status{StatusCode: statusCode, Message: message} w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(status) if err != nil { panic(err) } } }
//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 { fmt.Println(err) } else { err = db.TrashTask(id) if err != nil { message = "Error trashing task" } else { message = "Task trashed" } http.Redirect(w, r, "/", http.StatusFound) } } else { message = "Method not allowed" http.Redirect(w, r, "/trash", http.StatusFound) } }