示例#1
0
func (c *AclCtrl) 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
	}

	acl := &domain.Acl{}

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

	lastRessource := interfaces.GetLastRessource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	acl.SetRelatedID(lastRessource.IDKey, lastRessource.ID)
	acl, err = c.interactor.UpdateByID(id, acl, 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
	}

	acl.BeforeRender()
	c.render.JSON(w, http.StatusCreated, acl)
}
示例#2
0
func (c *AclCtrl) Upsert(w http.ResponseWriter, r *http.Request, _ map[string]string) {
	acl := &domain.Acl{}
	var acls []domain.Acl

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

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

	lastRessource := interfaces.GetLastRessource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	if acls == nil {
		acl.SetRelatedID(lastRessource.IDKey, lastRessource.ID)
		acl, err = c.interactor.UpsertOne(acl, usecases.QueryContext{Filter: filter, OwnerRelations: relations})
	} else {
		for i := range acls {
			(&acls[i]).SetRelatedID(lastRessource.IDKey, lastRessource.ID)
		}
		acls, err = c.interactor.Upsert(acls, 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 acls == nil {
		acl.BeforeRender()
		c.render.JSON(w, http.StatusCreated, acl)
	} else {
		for i := range acls {
			(&acls[i]).BeforeRender()
		}
		c.render.JSON(w, http.StatusCreated, acls)
	}
}
示例#3
0
func (c *AccountCtrl) UpdateByID(w http.ResponseWriter, r *http.Request, params map[string]string) {
	var (
		id  int
		err error
	)

	if params["id"] == "me" {
		sessionCtx := context.Get(r, "currentSession")
		if sessionCtx == nil {
			c.render.JSONError(w, http.StatusUnauthorized, apierrors.SessionNotFound, nil)
			return
		}

		id = sessionCtx.(domain.Session).AccountID
	} else {
		id, err = strconv.Atoi(params["id"])
		if err != nil {
			c.render.JSONError(w, http.StatusBadRequest, apierrors.InvalidPathParams, err)
			return
		}
	}

	account := &domain.Account{}

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

	lastRessource := interfaces.GetLastRessource(r)
	filter := interfaces.FilterIfOwnerRelations(r, nil)
	relations := interfaces.GetOwnerRelations(r)

	account.SetRelatedID(lastRessource.IDKey, lastRessource.ID)
	account, err = c.interactor.UpdateByID(id, account, 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
	}

	account.BeforeRender()
	c.render.JSON(w, http.StatusCreated, account)
}
示例#4
0
func (c *AclCtrl) Create(w http.ResponseWriter, r *http.Request, _ map[string]string) {
	acl := &domain.Acl{}
	var acls []domain.Acl

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

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

	lastRessource := interfaces.GetLastRessource(r)

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

	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 acls == nil {
		acl.BeforeRender()
		c.render.JSON(w, http.StatusCreated, acl)
	} else {
		for i := range acls {
			(&acls[i]).BeforeRender()
		}
		c.render.JSON(w, http.StatusCreated, acls)
	}
}