1
0

implemented update method via param binder

This commit is contained in:
konrad
2018-07-21 15:17:02 +02:00
committed by kolaente
parent 9e75e9b73b
commit 9979b7e321
12 changed files with 25 additions and 36 deletions

View File

@ -13,28 +13,22 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
p := reflect.ValueOf(c.CObject).Elem()
p.Set(reflect.Zero(p.Type()))
// Get the object
if err := ctx.Bind(&c.CObject); err != nil {
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
// Get the ID
id, err := models.GetIntURLParam("id", ctx)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
// Check if the user has the right to do that
currentUser, err := models.GetCurrentUser(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !c.CObject.CanUpdate(&currentUser, id) {
if !c.CObject.CanUpdate(&currentUser) {
return echo.NewHTTPError(http.StatusForbidden)
}
// Do the update
err = c.CObject.Update(id)
err = c.CObject.Update()
if err != nil {
if models.IsErrNeedToBeListAdmin(err) {
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")