Beispiel #1
0
// @Title UpdateByID
// @Description Update attributes of a Url instance
// @Accept  json
// @Param   id path int true "Url id"
// @Param   Url body domain.Url true "Url instance data"
// @Success 201 {object} domain.Url
// @Router /urls/{id} [put]
func (c *UrlCtrl) UpdateByID(w http.ResponseWriter, r *http.Request, params map[string]string) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		c.render.JSONError(w, http.StatusBadRequest, apierrors.InvalidPathParams, err)
		return
	}

	url := &domain.Url{}

	err = json.NewDecoder(r.Body).Decode(url)
	if err != nil {
		c.render.JSONError(w, http.StatusBadRequest, apierrors.BodyDecodingError, err)
		return
	}

	lastResource := interfaces.GetLastResource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	url.SetRelatedID(lastResource.IDKey, lastResource.ID)
	url, err = c.interactor.UpdateByID(id, url, usecases.QueryContext{Filter: filter, OwnerRelations: relations})

	if err != nil {
		switch err {
		case internalerrors.NotFound:
			c.render.JSONError(w, http.StatusUnauthorized, apierrors.Unauthorized, err)
		default:
			c.render.JSONError(w, http.StatusInternalServerError, apierrors.InternalServerError, err)
		}
		return
	}

	url.BeforeRender()
	c.render.JSON(w, http.StatusCreated, url)
}
Beispiel #2
0
// @Title Upsert
// @Description Upsert one or multiple Url instances
// @Accept  json
// @Param   Url body domain.Url true "Url instance(s) data"
// @Success 201 {object} domain.Url "Request was successful"
// @Router /urls [put]
func (c *UrlCtrl) Upsert(w http.ResponseWriter, r *http.Request, _ map[string]string) {
	url := &domain.Url{}
	var urls []domain.Url

	buffer, _ := ioutil.ReadAll(r.Body)

	err := json.Unmarshal(buffer, url)
	if err != nil {
		err := json.Unmarshal(buffer, &urls)
		if err != nil {
			c.render.JSONError(w, http.StatusBadRequest, apierrors.BodyDecodingError, err)
			return
		}
	}

	lastResource := interfaces.GetLastResource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	if urls == nil {
		url.SetRelatedID(lastResource.IDKey, lastResource.ID)
		url, err = c.interactor.UpsertOne(url, usecases.QueryContext{Filter: filter, OwnerRelations: relations})
	} else {
		for i := range urls {
			(&urls[i]).SetRelatedID(lastResource.IDKey, lastResource.ID)
		}
		urls, err = c.interactor.Upsert(urls, usecases.QueryContext{Filter: filter, OwnerRelations: relations})
	}

	if err != nil {
		switch err.(type) {
		case *internalerrors.ViolatedConstraint:
			c.render.JSONError(w, 422, apierrors.ViolatedConstraint, err)
		}

		switch err {
		case internalerrors.NotFound:
			c.render.JSONError(w, http.StatusUnauthorized, apierrors.Unauthorized, err)
		default:
			c.render.JSONError(w, http.StatusInternalServerError, apierrors.InternalServerError, err)
		}

		return
	}

	if urls == nil {
		url.BeforeRender()
		c.render.JSON(w, http.StatusCreated, url)
	} else {
		for i := range urls {
			(&urls[i]).BeforeRender()
		}
		c.render.JSON(w, http.StatusCreated, urls)
	}
}
Beispiel #3
0
// @Title Create
// @Description Create one or multiple Url instances
// @Accept  json
// @Param   Url body domain.Url true "Url instance(s) data"
// @Success 201 {object} domain.Url "Request was successful"
// @Router /urls [post]
func (c *UrlCtrl) Create(w http.ResponseWriter, r *http.Request, _ map[string]string) {
	url := &domain.Url{}
	var urls []domain.Url

	buffer, _ := ioutil.ReadAll(r.Body)

	err := json.Unmarshal(buffer, url)
	if err != nil {
		err := json.Unmarshal(buffer, &urls)
		if err != nil {
			c.render.JSONError(w, http.StatusBadRequest, apierrors.BodyDecodingError, err)
			return
		}
	}

	lastResource := interfaces.GetLastResource(r)

	if urls == nil {
		url.SetRelatedID(lastResource.IDKey, lastResource.ID)
		url, err = c.interactor.CreateOne(url)
	} else {
		for i := range urls {
			(&urls[i]).SetRelatedID(lastResource.IDKey, lastResource.ID)
		}
		urls, err = c.interactor.Create(urls)
	}

	if err != nil {
		switch err.(type) {
		case *internalerrors.ViolatedConstraint:
			c.render.JSONError(w, 422, apierrors.ViolatedConstraint, err)
		default:
			c.render.JSONError(w, http.StatusInternalServerError, apierrors.InternalServerError, err)
		}
		return
	}

	if urls == nil {
		url.BeforeRender()
		c.render.JSON(w, http.StatusCreated, url)
	} else {
		for i := range urls {
			(&urls[i]).BeforeRender()
		}
		c.render.JSON(w, http.StatusCreated, urls)
	}
}
Beispiel #4
0
// @Title UpdateByID
// @Description Update attributes of a User instance
// @Accept  json
// @Param   id path int true "User id"
// @Param   User body domain.User true "User instance data"
// @Success 200 {object} domain.User
// @Router /users/{id} [put]
func (c *UserCtrl) UpdateByID(w http.ResponseWriter, r *http.Request, params map[string]string) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		c.render.JSONError(w, http.StatusBadRequest, apierrors.InvalidPathParams, err)
		return
	}

	user := &domain.User{}

	err = json.NewDecoder(r.Body).Decode(user)
	if err != nil {
		c.render.JSONError(w, http.StatusBadRequest, apierrors.BodyDecodingError, err)
		return
	}

	lastResource := interfaces.GetLastResource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	user.SetRelatedID(lastResource.IDKey, lastResource.ID)

	if roles := context.Get(r, "roles"); roles != nil && utils.ContainsStr(roles.([]string), "Admin") {
		user, err = c.interactor.UpdateByID(id, user, usecases.QueryContext{Filter: filter, OwnerRelations: relations})
	} else {
		user, err = c.guestInter.UpdateByID(id, user, usecases.QueryContext{Filter: filter, OwnerRelations: relations})
	}

	if err != nil {
		switch err {
		case internalerrors.NotFound:
			c.render.JSONError(w, http.StatusUnauthorized, apierrors.Unauthorized, err)
		default:
			c.render.JSONError(w, http.StatusInternalServerError, apierrors.InternalServerError, err)
		}
		return
	}

	user.BeforeRender()
	c.render.JSON(w, http.StatusOK, user)
}