Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handlerのgame_roleのテスト #1092

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions src/handler/v2/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"errors"
"fmt"
"log"
"net/http"

Expand All @@ -25,6 +26,19 @@
}
}

func convertGameVisibility(value values.GameVisibility) (openapi.GameVisibility, error) {
switch value {
case values.GameVisibilityTypePublic:
return openapi.Public, nil
case values.GameVisibilityTypeLimited:
return openapi.Limited, nil

Check warning on line 34 in src/handler/v2/game.go

View check run for this annotation

Codecov / codecov/patch

src/handler/v2/game.go#L31-L34

Added lines #L31 - L34 were not covered by tests
case values.GameVisibilityTypePrivate:
return openapi.Private, nil
default:
return "", fmt.Errorf("invalid game visibility: %v", value)
}
}

// ゲーム一覧の取得
// (GET /games)
func (g *Game) GetGames(ctx echo.Context, params openapi.GetGamesParams) error {
Expand Down
85 changes: 57 additions & 28 deletions src/handler/v2/game_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,34 @@ func (gameRole *GameRole) PatchGameRole(ctx echo.Context, gameID openapi.GameIDI
}

userID := values.NewTrapMemberID(req.Id)
var roleType values.GameManagementRole
switch *req.Type {
case "owner":
roleType = values.GameManagementRoleAdministrator
case "maintainer":
roleType = values.GameManagementRoleCollaborator
default:
return echo.NewHTTPError(http.StatusBadRequest, "role type is invalid")
}

err = gameRole.gameRoleService.EditGameManagementRole(ctx.Request().Context(), authSession, values.GameID(gameID), userID, roleType)
if errors.Is(err, service.ErrNoGameManagementRoleUpdated) {
return echo.NewHTTPError(http.StatusBadRequest, "there is no change")
}
if errors.Is(err, service.ErrNoGame) {
return echo.NewHTTPError(http.StatusNotFound, "no game")
}
if errors.Is(err, service.ErrInvalidUserID) {
return echo.NewHTTPError(http.StatusBadRequest, "userID is invalid or no user")
}
if errors.Is(err, service.ErrCannotEditOwners) {
return echo.NewHTTPError(http.StatusBadRequest, "you cannot change the user role beause there is only 1 owner")
}
if err != nil {
log.Printf("error: failed to edit game management role: %v\n", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to edit game management role")
if req.Type != nil {
var roleType values.GameManagementRole
switch *req.Type {
case "owner":
roleType = values.GameManagementRoleAdministrator
case "maintainer":
roleType = values.GameManagementRoleCollaborator
default:
return echo.NewHTTPError(http.StatusBadRequest, "role type is invalid")
}

err = gameRole.gameRoleService.EditGameManagementRole(ctx.Request().Context(), authSession, values.GameID(gameID), userID, roleType)
if errors.Is(err, service.ErrNoGameManagementRoleUpdated) {
return echo.NewHTTPError(http.StatusBadRequest, "there is no change")
}
if errors.Is(err, service.ErrNoGame) {
return echo.NewHTTPError(http.StatusNotFound, "no game")
}
if errors.Is(err, service.ErrInvalidUserID) {
return echo.NewHTTPError(http.StatusBadRequest, "userID is invalid or no user")
}
if errors.Is(err, service.ErrCannotEditOwners) {
return echo.NewHTTPError(http.StatusBadRequest, "you cannot change the user role because there is only 1 owner")
}
if err != nil {
log.Printf("error: failed to edit game management role: %v\n", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to edit game management role")
}
}

newGameInfo, err := gameRole.gameService.GetGame(ctx.Request().Context(), authSession, values.GameID(gameID))
Expand All @@ -90,13 +92,27 @@ func (gameRole *GameRole) PatchGameRole(ctx echo.Context, gameID openapi.GameIDI
resMaintainers = append(resMaintainers, string(maintainer.GetName()))
}

var visibility openapi.GameVisibility
visibility, err = convertGameVisibility(newGameInfo.Game.GetVisibility())
if err != nil {
log.Printf("error: failed to convert game visibility: %v\n", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert game visibility")
}

genres := make([]openapi.GameGenreName, 0, len(newGameInfo.Genres))
for _, genre := range newGameInfo.Genres {
genres = append(genres, openapi.GameGenreName(genre.GetName()))
}

resGame := openapi.Game{
Id: uuid.UUID(newGameInfo.Game.GetID()),
Name: string(newGameInfo.Game.GetName()),
Description: string(newGameInfo.Game.GetDescription()),
CreatedAt: newGameInfo.Game.GetCreatedAt(),
Visibility: visibility,
Owners: resOwners,
Maintainers: &resMaintainers,
Genres: &genres,
}

return ctx.JSON(http.StatusOK, resGame)
Expand All @@ -116,13 +132,13 @@ func (gameRole *GameRole) DeleteGameRole(ctx echo.Context, gameID openapi.GameID

err = gameRole.gameRoleService.RemoveGameManagementRole(ctx.Request().Context(), values.GameID(gameID), values.TraPMemberID(userID))
if errors.Is(err, service.ErrInvalidRole) {
return echo.NewHTTPError(http.StatusNotFound, "the user does not has any role")
return echo.NewHTTPError(http.StatusNotFound, "the user does not have any role")
}
if errors.Is(err, service.ErrCannotDeleteOwner) {
return echo.NewHTTPError(http.StatusBadRequest, "you cannot delete owner because there is only 1 owner")
}
if errors.Is(err, service.ErrNoGame) {
return echo.NewHTTPError(http.StatusBadRequest, "no game")
return echo.NewHTTPError(http.StatusNotFound, "no game")
}
if err != nil {
log.Printf("error: failed to remove game management role: %v\n", err)
Expand All @@ -149,13 +165,26 @@ func (gameRole *GameRole) DeleteGameRole(ctx echo.Context, gameID openapi.GameID
resMaintainers = append(resMaintainers, string(maintainer.GetName()))
}

resVisibility, err := convertGameVisibility(newGameInfo.Game.GetVisibility())
if err != nil {
log.Printf("error: failed to convert game visibility: %v\n", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert game visibility")
}

resGenres := make([]openapi.GameGenreName, 0, len(newGameInfo.Genres))
for _, genre := range newGameInfo.Genres {
resGenres = append(resGenres, openapi.GameGenreName(genre.GetName()))
}

resGame := openapi.Game{
Id: uuid.UUID(newGameInfo.Game.GetID()),
Name: string(newGameInfo.Game.GetName()),
Description: string(newGameInfo.Game.GetDescription()),
CreatedAt: newGameInfo.Game.GetCreatedAt(),
Owners: resOwners,
Maintainers: &resMaintainers,
Genres: &resGenres,
Visibility: resVisibility,
}

return ctx.JSON(http.StatusOK, resGame)
Expand Down
Loading
Loading