//showTask - displays tasks with given ID. func getTask(req *restful.Request, resp *restful.Response) { taskIDfromRequest := req.PathParameter("task-id") taskService := service.GetTaskServiceInstance() task, err := taskService.GetByID(taskIDfromRequest) if err != nil { resp.WriteError(http.StatusNotFound, err) return } taskAsJSON, _ := json.Marshal(task) io.WriteString(resp, string(taskAsJSON)) }
//deleteTask - removes task by ID. func deleteTask(req *restful.Request, resp *restful.Response) { taskIDfromRequest := req.PathParameter("task-id") taskService := service.GetTaskServiceInstance() err := taskService.DeleteByID(taskIDfromRequest) if err != nil { resp.WriteError(http.StatusNotFound, err) return } resp.WriteHeader(http.StatusNoContent) }
//addTask - adds new task. //{"name": "Some task to do"} func addTask(req *restful.Request, resp *restful.Response) { taskIDfromRequest := req.PathParameter("task-id") taskFromRequest := service.Task{ID: taskIDfromRequest} err := req.ReadEntity(&taskFromRequest) if err != nil { resp.WriteError(http.StatusBadRequest, err) return } taskService := service.GetTaskServiceInstance() task, err := taskService.Add(taskFromRequest) if err != nil { resp.WriteError(http.StatusConflict, err) return } taskAsJSON, _ := json.Marshal(task) io.WriteString(resp, string(taskAsJSON)) }
//showTaskList - show all tasks. func showTaskList(req *restful.Request, resp *restful.Response) { taskService := service.GetTaskServiceInstance() tasks := taskService.GetAll() tasksAsJSON, _ := json.Marshal(tasks) io.WriteString(resp, string(tasksAsJSON)) }