예제 #1
0
파일: api.go 프로젝트: jens-a-e/api2go
func (res *resource) handleReplaceRelation(w http.ResponseWriter, r *http.Request, ps httprouter.Params, relation jsonapi.Reference) error {
	oldObj, err := res.source.FindOne(ps.ByName("id"), buildRequest(r))
	if err != nil {
		return err
	}

	inc, err := unmarshalRequest(r, res.marshalers)
	if err != nil {
		return err
	}

	data, ok := inc["data"]
	if !ok {
		return errors.New("Invalid object. Need a \"data\" object")
	}

	editObj, updateObj := getRelationUpdateObjects(oldObj)

	err = jsonapi.UnmarshalRelationshipsData(editObj, relation.Name, data)
	if err != nil {
		return err
	}

	if err := res.source.Update(updateObj, buildRequest(r)); err != nil {
		return err
	}

	w.WriteHeader(http.StatusNoContent)
	return nil
}
예제 #2
0
파일: api.go 프로젝트: m0dd3r/api2go
func (res *resource) handleReplaceRelation(c APIContexter, w http.ResponseWriter, r *http.Request, params map[string]string, relation jsonapi.Reference) error {
	var (
		err     error
		editObj interface{}
	)

	id := params["id"]

	response, err := res.source.FindOne(id, buildRequest(c, r))
	if err != nil {
		return err
	}

	inc, err := unmarshalRequest(r, res.marshalers)
	if err != nil {
		return err
	}

	data, ok := inc["data"]
	if !ok {
		return errors.New("Invalid object. Need a \"data\" object")
	}

	resType := reflect.TypeOf(response.Result()).Kind()
	if resType == reflect.Struct {
		editObj = getPointerToStruct(response.Result())
	} else {
		editObj = response.Result()
	}

	err = jsonapi.UnmarshalRelationshipsData(editObj, relation.Name, data)
	if err != nil {
		return err
	}

	if resType == reflect.Struct {
		_, err = res.source.Update(reflect.ValueOf(editObj).Elem().Interface(), buildRequest(c, r))
	} else {
		_, err = res.source.Update(editObj, buildRequest(c, r))
	}

	w.WriteHeader(http.StatusNoContent)
	return err
}
예제 #3
0
파일: api.go 프로젝트: theduke/api2go
func (res *resource) handleReplaceRelation(r Request, relation jsonapi.Reference) response {
	var (
		err     error
		editObj interface{}
	)

	resp, err := res.source.FindOne(r.Params.ByName("id"), r)
	if err != nil {
		return response{Error: err}
	}

	inc := r.Data
	data, ok := inc["data"]
	if !ok {
		return response{
			Error: NewHTTPError(nil, "Invalid object. Need a \"data\" object", http.StatusInternalServerError),
		}
	}

	resType := reflect.TypeOf(resp.Result()).Kind()
	if resType == reflect.Struct {
		editObj = getPointerToStruct(resp.Result())
	} else {
		editObj = resp.Result()
	}

	err = jsonapi.UnmarshalRelationshipsData(editObj, relation.Name, data)
	if err != nil {
		return response{Error: err}
	}

	if resType == reflect.Struct {
		_, err = res.source.Update(reflect.ValueOf(editObj).Elem().Interface(), r)
	} else {
		_, err = res.source.Update(editObj, r)
	}

	if err != nil {
		return response{Error: err}
	}

	return response{Status: http.StatusNoContent}
}
예제 #4
0
파일: api.go 프로젝트: dfeyer/api2go
func (res *resource) handleReplaceRelation(w http.ResponseWriter, r *http.Request, ps httprouter.Params, relation jsonapi.Reference) error {
	var (
		err     error
		editObj interface{}
	)

	oldObj, err := res.source.FindOne(ps.ByName("id"), buildRequest(r))
	if err != nil {
		return err
	}

	inc, err := unmarshalRequest(r, res.marshalers)
	if err != nil {
		return err
	}

	data, ok := inc["data"]
	if !ok {
		return errors.New("Invalid object. Need a \"data\" object")
	}

	resType := reflect.TypeOf(oldObj).Kind()
	if resType == reflect.Struct {
		editObj = getPointerToStruct(oldObj)
	} else {
		editObj = oldObj
	}

	err = jsonapi.UnmarshalRelationshipsData(editObj, relation.Name, data)
	if err != nil {
		return err
	}

	if resType == reflect.Struct {
		err = res.source.Update(reflect.ValueOf(editObj).Elem().Interface(), buildRequest(r))
	} else {
		err = res.source.Update(editObj, buildRequest(r))
	}

	w.WriteHeader(http.StatusNoContent)
	return err
}