Example #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
	}
}
Example #2
0
func (r *LocationResource) includeConditions(loc *api.Location) error {
	cond, err := r.WeatherClient.FindForLocation(loc.City, loc.State)
	if err == nil {
		loc.Temperature = cond.Main.Temperature
		if len(cond.Weather) > 0 {
			loc.Weather = cond.Weather[0].Description
		}
	}
	return err
}
Example #3
0
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
	}
}