Skip to content

Commit

Permalink
Merge pull request #23 from alexferl/simple_err
Browse files Browse the repository at this point in the history
simplify errors
  • Loading branch information
alexferl authored Mar 19, 2024
2 parents 1a0be84 + 9f4adb3 commit 1c39e30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 72 deletions.
20 changes: 4 additions & 16 deletions handlers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ func (h *TaskHandler) get(c echo.Context) error {

task, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readTask(c, err)
if sErr != nil {
return sErr()
}
return h.readTask(c, err)()
}

return h.Validate(c, http.StatusOK, task.Response())
Expand All @@ -134,10 +131,7 @@ func (h *TaskHandler) update(c echo.Context) error {

task, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readTask(c, err)
if sErr != nil {
return sErr()
}
return h.readTask(c, err)()
}

if currentUser.Id != task.CreatedBy.(*models.User).Id && !currentUser.HasRoleOrHigher(models.AdminRole) {
Expand Down Expand Up @@ -176,10 +170,7 @@ func (h *TaskHandler) transition(c echo.Context) error {

task, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readTask(c, err)
if sErr != nil {
return sErr()
}
return h.readTask(c, err)()
}

if *body.Completed != task.Completed {
Expand Down Expand Up @@ -208,10 +199,7 @@ func (h *TaskHandler) delete(c echo.Context) error {

task, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readTask(c, err)
if sErr != nil {
return sErr()
}
return h.readTask(c, err)()
}

if currentUser.Id != task.CreatedBy.(*models.User).Id && !currentUser.HasRoleOrHigher(models.AdminRole) {
Expand Down
70 changes: 14 additions & 56 deletions handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ func (h *UserHandler) get(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

if currentUser.HasRoleOrHigher(models.AdminRole) {
Expand Down Expand Up @@ -148,10 +145,7 @@ func (h *UserHandler) update(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

if body.Name != nil {
Expand Down Expand Up @@ -180,18 +174,12 @@ func (h *UserHandler) ban(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.Ban(currentUser)
if err != nil {
mErr := h.checkModelErr(c, err, "banning")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "banning")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand All @@ -212,18 +200,12 @@ func (h *UserHandler) unban(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.Unban(currentUser)
if err != nil {
mErr := h.checkModelErr(c, err, "unbanning")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "unbanning")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand All @@ -244,18 +226,12 @@ func (h *UserHandler) lock(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.Lock(currentUser)
if err != nil {
mErr := h.checkModelErr(c, err, "locking")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "locking")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand All @@ -276,18 +252,12 @@ func (h *UserHandler) unlock(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.Unlock(currentUser)
if err != nil {
mErr := h.checkModelErr(c, err, "locking")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "locking")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand All @@ -309,18 +279,12 @@ func (h *UserHandler) addRole(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.AddRole(currentUser, models.RolesMap[role])
if err != nil {
mErr := h.checkModelErr(c, err, "locking")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "locking")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand All @@ -342,18 +306,12 @@ func (h *UserHandler) removeRole(c echo.Context) error {

user, err := h.svc.Read(ctx, id)
if err != nil {
sErr := h.readUser(c, err)
if sErr != nil {
return sErr()
}
return h.readUser(c, err)()
}

err = user.RemoveRole(currentUser, models.RolesMap[role])
if err != nil {
mErr := h.checkModelErr(c, err, "locking")
if mErr != nil {
return mErr()
}
return h.checkModelErr(c, err, "locking")()
}

_, err = h.svc.Update(ctx, currentUser.Id, user)
Expand Down

0 comments on commit 1c39e30

Please sign in to comment.