//UpdateTaskFunc is used to update a task, handes "/update/" URL func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { r.ParseForm() id, err := strconv.Atoi(r.Form.Get("id")) if err != nil { log.Println(err) } category := r.Form.Get("category") title := r.Form.Get("title") content := r.Form.Get("content") priority, err := strconv.Atoi(r.Form.Get("priority")) if err != nil { log.Println(err) } err = db.UpdateTask(id, title, content, category, priority) if err != nil { message = "Error updating task" } else { message = "Task updated" log.Println(message) } http.Redirect(w, r, "/", 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" { http.Redirect(w, r, "/", http.StatusBadRequest) return } r.ParseForm() id, err := strconv.Atoi(r.Form.Get("id")) if err != nil { log.Println(err) } category := r.Form.Get("category") title := r.Form.Get("title") content := r.Form.Get("content") priority, err := strconv.Atoi(r.Form.Get("priority")) if err != nil { log.Println(err) } username := sessions.GetCurrentUserName(r) var hidden int hideTimeline := r.FormValue("hide") if hideTimeline != "" { hidden = 1 } else { hidden = 0 } err = db.UpdateTask(id, title, content, category, priority, username, hidden) if err != nil { message = "Error updating task" } else { message = "Task updated" log.Println(message) } http.Redirect(w, r, "/", http.StatusFound) }
//UpdateTaskFuncAPI will add the tasks for the user func UpdateTaskFuncAPI(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { var taskErr bool 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") r.ParseForm() strID := r.Form.Get("id") id, err := strconv.Atoi(strID) if err != nil { log.Println(err) taskErr = true } category := r.Form.Get("category") title := r.Form.Get("title") content := r.Form.Get("content") priority, err := strconv.Atoi(r.Form.Get("priority")) if err != nil { log.Println(err) priority = 1 } var hidden int hideTimeline := r.FormValue("hide") if hideTimeline != "" { hidden = 1 } else { hidden = 0 } if strID != "" && title != "" && content != "" { err = db.UpdateTask(id, title, content, category, priority, username, hidden) if err != nil { taskErr = true } taskErr = false } else { taskErr = true } var statusCode int var message string if taskErr { statusCode = http.StatusBadRequest message = "unable to update task id " } else { statusCode = http.StatusOK message = "updated task id " } 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) } } }