Esempio n. 1
0
func (r *LocationResource) Add(res http.ResponseWriter, req *http.Request) {
	var location api.Location

	if err := rest.Bind(req, &location); err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	if location.City == "" || location.State == "" {
		rest.SetBadRequestResponse(res)
		return
	}

	var found api.Location
	if location.Id != 0 && !r.Db.First(&found, location.Id).RecordNotFound() {
		rest.SetConflictResponse(res)
		return
	}
	location.Id = 0

	r.Db.Save(&location)

	if err := r.includeConditions(&location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if err := rest.SetCreatedResponse(res, location, fmt.Sprintf("location/%d", location.Id)); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
Esempio n. 2
0
func (r *TodoResource) Add(res http.ResponseWriter, req *http.Request) {
	var todo Todo

	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}

	if err := rest.Bind(req, &todo); err != nil {
		log.Print(err)
		rest.SetBadRequestResponse(res)
		return
	}

	db.Create(&todo)

	if err := rest.SetCreatedResponse(res, todo, fmt.Sprintf("todo/%d", todo.Id)); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}