func postNewTask(response http.ResponseWriter, request *http.Request) { if request.Body == nil { log.Warn("Empty body in new task POST request") WriteBasicResponse(http.StatusBadRequest, response) return } defer request.Body.Close() decoder := json.NewDecoder(request.Body) var params NewTaskParams err := decoder.Decode(¶ms) if err != nil { log.Warn("Couldn't decode params") WriteBasicResponse(http.StatusBadRequest, response) return } tasklist, err := storage.GetTasklist() if err != nil { log.Errorf("Could not get tasklist") WriteBasicResponse(http.StatusInternalServerError, response) return } var parentTasks []*task.Task for _, parentID := range params.ParentIDs { parentTask, ok := tasklist.Registry[parentID] if !ok { log.Warnf("Couldn't find parent task (%v)", parentID) WriteBasicResponse(http.StatusBadRequest, response) return } parentTasks = append(parentTasks, parentTask) } newTask := tasklist.AddTask(params.Name, parentTasks) newTask.DueDate = params.DueDate newTask.Categories = params.Categories if err := storage.SaveTasklist(tasklist); err != nil { log.Errorf("Couldn't save tasklist (%v)", err) WriteBasicResponse(http.StatusInternalServerError, response) return } WriteJSONResponse(response, newTask, http.StatusOK) }
func putTask(response http.ResponseWriter, request *http.Request) { if request.Body == nil { WriteBasicResponse(http.StatusBadRequest, response) return } defer request.Body.Close() decoder := json.NewDecoder(request.Body) var params UpdateTaskParams err := decoder.Decode(¶ms) if err != nil { WriteBasicResponse(http.StatusBadRequest, response) return } vars := mux.Vars(request) taskID := vars["id"] tasklist, err := storage.GetTasklist() if err != nil { log.Errorf("Couldn't get tasklist (%v)", err) WriteBasicResponse(http.StatusInternalServerError, response) return } task, ok := tasklist.Registry[taskID] if !ok { WriteBasicResponse(http.StatusNotFound, response) return } // TODO: Modify the task if params.Name != "" { task.Name = params.Name } task.SetComplete(params.Complete) if params.Categories != nil { task.Categories = params.Categories } if params.DueDate != 0 { task.DueDate = params.DueDate } for _, subtaskID := range params.SubtaskIDs { subtask, ok := tasklist.Registry[subtaskID] if !ok { log.Warnf("Could not find subtask (%v)", subtaskID) WriteBasicResponse(http.StatusBadRequest, response) return } task.AddSubtask(subtask) } for _, parentID := range params.ParentIDs { parent, ok := tasklist.Registry[parentID] if !ok { log.Warnf("Could not find parent task (%v)", parentID) WriteBasicResponse(http.StatusBadRequest, response) return } task.AddParent(parent) } err = storage.SaveTasklist(tasklist) if err != nil { log.Errorf("Couldn't save tasklist (%v)", err) WriteBasicResponse(http.StatusInternalServerError, response) return } WriteBasicResponse(http.StatusOK, response) }