Skip to content

Commit

Permalink
applied review suggestions
Browse files Browse the repository at this point in the history
* enhance rollback errors
* use diff for put
* use permissions-api mock

Signed-off-by: Bailin He <[email protected]>
  • Loading branch information
bailinhe committed Oct 2, 2024
1 parent 9dca4ff commit 679d897
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 330 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/api/httpsrv/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var (
status: http.StatusNotFound,
message: "not found",
}

// ErrDBRollbackFailed is returned when a database rollback fails
ErrDBRollbackFailed = errors.New("failed to rollback database transaction")
)

func permissionsError(err error) error {
Expand Down
6 changes: 5 additions & 1 deletion internal/api/httpsrv/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpsrv

import (
"context"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -104,7 +105,10 @@ func (h *APIHandler) Routes(rg *echo.Group) {

func (h *apiHandler) rollbackAndReturnError(ctx context.Context, httpcode int, msg string) *echo.HTTPError {
if err := h.engine.RollbackContext(ctx); err != nil {
return echo.NewHTTPError(http.StatusBadGateway, err)
return echo.NewHTTPError(
http.StatusInternalServerError,
fmt.Errorf("failed to rollback database transaction: %w", err),
)
}

return echo.NewHTTPError(httpcode, msg)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/httpsrv/handler_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (h *apiHandler) CreateGroup(ctx context.Context, req CreateGroupRequestObje
}

if err := h.eventService.CreateGroup(ctx, ownerID, id); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to create group in permissions API")
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to create group in permissions API")
return nil, resperr
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func (h *apiHandler) DeleteGroup(ctx context.Context, req DeleteGroupRequestObje
}

if err := h.eventService.DeleteGroup(ctx, group.OwnerID, group.ID); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to remove group in permissions API")
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to remove group in permissions API")
return nil, resperr
}

Expand Down
18 changes: 7 additions & 11 deletions internal/api/httpsrv/handler_group_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *apiHandler) AddGroupMembers(ctx context.Context, req AddGroupMembersReq
}

if err := h.eventService.AddGroupMembers(ctx, gid, reqbody.MemberIDs...); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to add group members in permissions API")
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to add group members in permissions API")
return nil, resperr
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func (h *apiHandler) RemoveGroupMember(ctx context.Context, req RemoveGroupMembe
}

if err := h.eventService.RemoveGroupMembers(ctx, gid, sid); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to remove group member in permissions API")
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to remove group member in permissions API")
return nil, resperr
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func (h *apiHandler) ReplaceGroupMembers(ctx context.Context, req ReplaceGroupMe
return nil, permissionsError(err)
}

current, err := h.engine.ListGroupMembers(ctx, gid, nil)
add, rm, err := h.engine.ReplaceGroupMembers(ctx, gid, reqbody.MemberIDs...)
if err != nil {
if errors.Is(err, types.ErrNotFound) {
err = echo.NewHTTPError(http.StatusNotFound, err.Error())
Expand All @@ -192,17 +192,13 @@ func (h *apiHandler) ReplaceGroupMembers(ctx context.Context, req ReplaceGroupMe
return nil, err
}

if err := h.engine.ReplaceGroupMembers(ctx, gid, reqbody.MemberIDs...); err != nil {
return nil, err
}

if err := h.eventService.RemoveGroupMembers(ctx, gid, current...); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to replace group members in permissions API")
if err := h.eventService.RemoveGroupMembers(ctx, gid, rm...); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to replace group members in permissions API")
return nil, resperr
}

if err := h.eventService.AddGroupMembers(ctx, gid, reqbody.MemberIDs...); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusBadGateway, "failed to replace group members in permissions API")
if err := h.eventService.AddGroupMembers(ctx, gid, add...); err != nil {
resperr := h.rollbackAndReturnError(ctx, http.StatusInternalServerError, "failed to replace group members in permissions API")
return nil, resperr
}

Expand Down
Loading

0 comments on commit 679d897

Please sign in to comment.