//Delete handles save requests func (c *TodoController) Delete(req *relay.Context, next relay.NextHandler) { id, err := strconv.Atoi(req.Get("id").(string)) if err != nil { req.Res.WriteHeader(http.StatusBadRequest) return } if err = c.db.Destroy(id); err != nil { req.Res.WriteHeader(http.StatusExpectationFailed) return } req.Res.WriteHeader(http.StatusOK) next(req) }
//Save handles save requests func (c *TodoController) Save(req *relay.Context, next relay.NextHandler) { message, err := relay.MessageDecoder.Decode(req) if err != nil { req.Res.WriteHeader(404) req.Res.Write([]byte("Err decoding body!")) return } if message.MessageType == "body" { todo := jsonTask{} if err := json.NewEncoder(bytes.NewBuffer(message.Payload)).Encode(&todo); err != nil { req.Res.WriteHeader(http.StatusBadRequest) return } if err := c.db.New(todo.Task, todo.Desc, todo.Stamp); err != nil { req.Res.WriteHeader(http.StatusExpectationFailed) return } req.Res.WriteHeader(http.StatusCreated) return } task := message.Form.Get("task") desc := message.Form.Get("description") if task == "" || desc == "" { req.Res.WriteHeader(http.StatusBadRequest) return } log.Printf("Submitting: %s %s", task, desc) if err := c.db.New(task, desc, 0); err != nil { req.Res.WriteHeader(http.StatusExpectationFailed) log.Printf("failed: %s", err) return } c.Render("/todo", req.Res, req.Req, req.ToMap()) next(req) }