func (r *TodoResource) Delete(res http.ResponseWriter, req *http.Request) { id, err := rest.PathInt(req, "id") if err != nil { rest.SetBadRequestResponse(res) return } var todo Todo db, err := r.Db.Get() if err != nil { rest.SetInternalServerErrorResponse(res, err) return } if db.First(&todo, id).RecordNotFound() { rest.SetNotFoundResponse(res) return } db.Delete(&todo) if err := rest.SetNoContentResponse(res); err != nil { rest.SetInternalServerErrorResponse(res, err) return } }
func (r *TodoResource) Update(res http.ResponseWriter, req *http.Request) { var todo Todo id, err := rest.PathInt(req, "id") if err != nil { rest.SetBadRequestResponse(res) return } if err := rest.Bind(req, &todo); err != nil { rest.SetBadRequestResponse(res) return } todo.Id = id var found Todo db, err := r.Db.Get() if err != nil { rest.SetInternalServerErrorResponse(res, err) return } if db.First(&found, id).RecordNotFound() { rest.SetNotFoundResponse(res) return } db.Save(&todo) if err := rest.SetOKResponse(res, todo); err != nil { rest.SetInternalServerErrorResponse(res, err) return } }
func (r *LocationResource) Save(res http.ResponseWriter, req *http.Request) { var location api.Location id, err := rest.PathInt(req, "id") if err != nil { rest.SetBadRequestResponse(res) return } if err := rest.Bind(req, &location); err != nil { rest.SetBadRequestResponse(res) return } if location.Id != 0 && location.Id != id { rest.SetBadRequestResponse(res) return } location.Id = id if location.City == "" || location.State == "" { rest.SetBadRequestResponse(res) return } var found api.Location if r.Db.First(&found, id).RecordNotFound() { rest.SetNotFoundResponse(res) return } r.Db.Save(&location) if err = r.includeConditions(&location); err != nil { rest.SetInternalServerErrorResponse(res, err) return } if err := rest.SetOKResponse(res, location); err != nil { rest.SetInternalServerErrorResponse(res, err) return } }
func (r *LocationResource) Find(res http.ResponseWriter, req *http.Request) { id, err := rest.PathInt(req, "id") if err != nil { rest.SetBadRequestResponse(res) return } var location api.Location if r.Db.First(&location, id).RecordNotFound() { rest.SetNotFoundResponse(res) return } if err = r.includeConditions(&location); err != nil { rest.SetInternalServerErrorResponse(res, err) return } if err := rest.SetOKResponse(res, location); err != nil { rest.SetInternalServerErrorResponse(res, err) return } }