From cd97fdb272c49f30275adf57e72486e22b746312 Mon Sep 17 00:00:00 2001 From: YuitoSakano <133097663+SakanoYuito@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:07:55 +0900 Subject: [PATCH 01/16] =?UTF-8?q?service=E3=81=AEGetGame=E3=81=A7genre?= =?UTF-8?q?=E8=BF=94=E3=81=99=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/repository/v2_game_genre.go | 14 ++++++++++++++ src/service/v2/game.go | 17 +++++++++++++---- src/service/v2_game.go | 1 + 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/repository/v2_game_genre.go diff --git a/.gitignore b/.gitignore index 8c0128dd..eb7bb8c9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ tmp !src/repository/mock/db.go cache/* .idea +.DS_Store \ No newline at end of file diff --git a/src/repository/v2_game_genre.go b/src/repository/v2_game_genre.go new file mode 100644 index 00000000..40a91ffc --- /dev/null +++ b/src/repository/v2_game_genre.go @@ -0,0 +1,14 @@ +package repository + +//go:generate go run github.com/golang/mock/mockgen -source=$GOFILE -destination=mock/${GOFILE} -package=mock + +import ( + "context" + + "github.com/traPtitech/trap-collection-server/src/domain" + "github.com/traPtitech/trap-collection-server/src/domain/values" +) + +type GameGenre interface { + GetGenresByGameID(ctx context.Context, gameID values.GameID) ([]*domain.GameGenre, error) +} diff --git a/src/service/v2/game.go b/src/service/v2/game.go index 6e2bc162..02039ede 100644 --- a/src/service/v2/game.go +++ b/src/service/v2/game.go @@ -13,22 +13,25 @@ import ( ) type Game struct { - db repository.DB - gameRepository repository.GameV2 - gameManagementRole repository.GameManagementRole - user *User + db repository.DB + gameRepository repository.GameV2 + gameManagementRole repository.GameManagementRole + gameGenreRepository repository.GameGenre + user *User } func NewGame( db repository.DB, gameRepository repository.GameV2, gameManagementRole repository.GameManagementRole, + gameGenreRepository repository.GameGenre, user *User, ) *Game { return &Game{ db: db, gameRepository: gameRepository, gameManagementRole: gameManagementRole, + gameGenreRepository: gameGenreRepository, user: user, } } @@ -196,10 +199,16 @@ func (g *Game) GetGame(ctx context.Context, session *domain.OIDCSession, gameID } } + genres, err := g.gameGenreRepository.GetGenresByGameID(ctx, gameID) + if err != nil { + return nil, fmt.Errorf("failed to get game genre: %w", err) + } + gameInfo := &service.GameInfoV2{ Game: game, Owners: ownersInfo, Maintainers: maintainersInfo, + Genres: genres, } return gameInfo, nil diff --git a/src/service/v2_game.go b/src/service/v2_game.go index aef041c0..8fd4a4c8 100644 --- a/src/service/v2_game.go +++ b/src/service/v2_game.go @@ -50,4 +50,5 @@ type GameInfoV2 struct { Game *domain.Game Owners []*UserInfo Maintainers []*UserInfo + Genres []*domain.GameGenre } From 64d61954a521fff93f5422440e1000c1a5a32872 Mon Sep 17 00:00:00 2001 From: YuitoSakano <133097663+SakanoYuito@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:03:02 +0900 Subject: [PATCH 02/16] =?UTF-8?q?GetGame=20=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ikura-hamu --- src/service/v2/game_test.go | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/service/v2/game_test.go b/src/service/v2/game_test.go index 55581d72..002bb393 100644 --- a/src/service/v2/game_test.go +++ b/src/service/v2/game_test.go @@ -30,6 +30,7 @@ func TestCreateGame(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -40,6 +41,7 @@ func TestCreateGame(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, user, ) @@ -514,6 +516,7 @@ func TestGetGame(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -524,6 +527,7 @@ func TestGetGame(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, userUtils, ) @@ -536,6 +540,9 @@ func TestGetGame(t *testing.T) { executeGetGameManagersByGameID bool administrators []*repository.UserIDAndManagementRole GetGameManagersByGameIDErr error + executeGetGenresByGameID bool + genres []*domain.GameGenre + GetGenresByGameIDErr error isErr bool err error } @@ -544,6 +551,9 @@ func TestGetGame(t *testing.T) { userID1 := values.NewTrapMemberID(uuid.New()) + gameGenreID := values.NewGameGenreID() + gameGenreName := values.NewGameGenreName("ジャンル") + activeUsers := []*service.UserInfo{ service.NewUserInfo( userID1, @@ -570,6 +580,8 @@ func TestGetGame(t *testing.T) { Role: values.GameManagementRoleAdministrator, }, }, + executeGetGenresByGameID: true, + genres: []*domain.GameGenre{domain.NewGameGenre(gameGenreID, gameGenreName, time.Now().Add(-time.Hour))}, }, { description: "ゲームが存在しないのでErrNoGame", @@ -591,6 +603,35 @@ func TestGetGame(t *testing.T) { GetGameManagersByGameIDErr: errors.New("error"), isErr: true, }, + { + description: "GetGameGenresByGameIDがエラーなのでエラー", + gameID: gameID, + executeGetGameManagersByGameID: true, + executeGetActiveUsers: true, + executeGetGenresByGameID: true, + GetGenresByGameIDErr: errors.New("error"), + isErr: true, + }, + { + description: "Genreが空でも問題ない", + gameID: gameID, + game: domain.NewGame( + gameID, + "game name", + "game description", + time.Now(), + ), + executeGetActiveUsers: true, + executeGetGameManagersByGameID: true, + administrators: []*repository.UserIDAndManagementRole{ + { + UserID: userID1, + Role: values.GameManagementRoleAdministrator, + }, + }, + executeGetGenresByGameID: true, + genres: []*domain.GameGenre{}, + }, } for _, testCase := range testCases { @@ -612,6 +653,12 @@ func TestGetGame(t *testing.T) { GetGameManagersByGameID(ctx, testCase.gameID). Return(testCase.administrators, testCase.GetGameManagersByGameIDErr) } + if testCase.executeGetGenresByGameID { + mockGameGenreRepository. + EXPECT(). + GetGenresByGameID(ctx, testCase.gameID). + Return(testCase.genres, testCase.GetGenresByGameIDErr) + } gameInfo, err := gameService.GetGame(ctx, domain.NewOIDCSession("access token", time.Now().Add(time.Hour)), testCase.gameID) @@ -642,6 +689,18 @@ func TestGetGame(t *testing.T) { } else { assert.Nil(t, gameInfo.Maintainers) } + + if testCase.genres != nil { + for i:= 0; i < len(testCase.genres); i++ { + assert.Equal(t, testCase.genres[i], gameInfo.Genres[i]) + + assert.Equal(t, testCase.genres[i].GetID(), gameInfo.Genres[i].GetID()) + assert.Equal(t, testCase.genres[i].GetName(), gameInfo.Genres[i].GetName()) + assert.WithinDuration(t, testCase.genres[i].GetCreatedAt(), gameInfo.Genres[i].GetCreatedAt(), time.Second) + } + } else { + assert.Nil(t, gameInfo.Genres) + } }) } } @@ -657,6 +716,7 @@ func TestGetGames(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -667,6 +727,7 @@ func TestGetGames(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, userUtils, ) @@ -824,6 +885,7 @@ func TestGetMyGames(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -834,6 +896,7 @@ func TestGetMyGames(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, userUtils, ) @@ -1061,6 +1124,7 @@ func TestUpdateGame(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -1071,6 +1135,7 @@ func TestUpdateGame(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, userUtils, ) @@ -1240,6 +1305,7 @@ func TestDeleteGame(t *testing.T) { mockDB := mockRepository.NewMockDB(ctrl) mockGameRepository := mockRepository.NewMockGameV2(ctrl) mockGameManagementRoleRepository := mockRepository.NewMockGameManagementRole(ctrl) + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) mockUserCache := mockCache.NewMockUser(ctrl) mockUserAuth := mockAuth.NewMockUser(ctrl) @@ -1250,6 +1316,7 @@ func TestDeleteGame(t *testing.T) { mockDB, mockGameRepository, mockGameManagementRoleRepository, + mockGameGenreRepository, userUtils, ) From e266ff14278bf650661bda82e02f4466be85e71c Mon Sep 17 00:00:00 2001 From: YuitoSakano <133097663+SakanoYuito@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:03:39 +0900 Subject: [PATCH 03/16] =?UTF-8?q?Repository=20=E3=81=A7=20GetGenresByGameI?= =?UTF-8?q?D=20=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{v2_game_genre.go => game_genre.go} | 0 src/repository/gorm2/game_genre.go | 52 +++++++++++++++++++ 2 files changed, 52 insertions(+) rename src/repository/{v2_game_genre.go => game_genre.go} (100%) create mode 100644 src/repository/gorm2/game_genre.go diff --git a/src/repository/v2_game_genre.go b/src/repository/game_genre.go similarity index 100% rename from src/repository/v2_game_genre.go rename to src/repository/game_genre.go diff --git a/src/repository/gorm2/game_genre.go b/src/repository/gorm2/game_genre.go new file mode 100644 index 00000000..4c9b9d07 --- /dev/null +++ b/src/repository/gorm2/game_genre.go @@ -0,0 +1,52 @@ +package gorm2 + +import ( + "context" + "errors" + "fmt" + + "github.com/traPtitech/trap-collection-server/src/domain" + "github.com/traPtitech/trap-collection-server/src/domain/values" + "github.com/traPtitech/trap-collection-server/src/repository" + "github.com/traPtitech/trap-collection-server/src/repository/gorm2/migrate" + "gorm.io/gorm" +) + +type GameGenre struct { + db *DB +} + +func NewGameGenre(db *DB) *GameGenre { + return &GameGenre{ + db: db, + } +} + +var _ repository.GameGenre = &GameGenre{} + +func (gameGenre *GameGenre) GetGenresByGameID(ctx context.Context, gameID values.GameID) ([]*domain.GameGenre, error) { + db, err := gameGenre.db.getDB(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get db: %w", err) + } + + var genres []*migrate.GameGenreTable + err = db. + Joins("Games"). + Where("game_genre_relations.game_id = ?", gameID). + Order("created_at DESC"). + Find(&genres).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return []*domain.GameGenre{}, nil + } + if err != nil { + return nil, fmt.Errorf("failed to get GameGenre: %w", err) + } + + result := make([]*domain.GameGenre, 0, len(genres)) + + for _, genre := range genres { + result = append(result, domain.NewGameGenre(values.GameGenreID(genre.ID), values.GameGenreName(genre.Name), genre.CreatedAt)) + } + return result, nil +} From bd48e7358bd3969d6b5ee54eded4643ef4f7dd88 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:46:19 +0900 Subject: [PATCH 04/16] =?UTF-8?q?wire=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/wire/repository.go | 3 +++ src/wire/wire_gen.go | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wire/repository.go b/src/wire/repository.go index 036a7a3c..a28aa9a7 100644 --- a/src/wire/repository.go +++ b/src/wire/repository.go @@ -71,4 +71,7 @@ var repositorySet = wire.NewSet( wire.Bind(new(repository.Seat), new(*gorm2.Seat)), gorm2.NewSeat, + + wire.Bind(new(repository.GameGenre), new(*gorm2.GameGenre)), + gorm2.NewGameGenre, ) diff --git a/src/wire/wire_gen.go b/src/wire/wire_gen.go index 0dfd4d81..125723be 100644 --- a/src/wire/wire_gen.go +++ b/src/wire/wire_gen.go @@ -204,10 +204,11 @@ func InjectApp() (*App, error) { } user3 := v2.NewUser(v2Session, v1User) admin := v2.NewAdmin(v2AdminAuth, v2Session) - v2Game := v2_2.NewGame(db, gameV2, gameManagementRole, v2User) + gameGenre := gorm2.NewGameGenre(db) + v2Game := v2_2.NewGame(db, gameV2, gameManagementRole, gameGenre, v2User) game3 := v2.NewGame(v2Session, v2Game) gameRole2 := v2.NewGameRole(v2GameRole, v2Game, v2Session) - gameGenre := v2.NewGameGenre() + v2GameGenre := v2.NewGameGenre() gameImageV2 := gorm2.NewGameImageV2(db) gameVideoV2 := gorm2.NewGameVideoV2(db) v2GameVersion := v2_2.NewGameVersion(db, game, gameImageV2, gameVideoV2, gameFileV2, gameVersionV2) @@ -227,7 +228,7 @@ func InjectApp() (*App, error) { } v2Seat := v2_2.NewSeat(db, seat, ristrettoSeat) seat2 := v2.NewSeat(v2Seat) - v2API := v2.NewAPI(checker, v2Session, v2OAuth2, user3, admin, game3, gameRole2, gameGenre, gameVersion3, gameFile3, gameImage3, gameVideo3, edition2, v2EditionAuth, seat2) + v2API := v2.NewAPI(checker, v2Session, v2OAuth2, user3, admin, game3, gameRole2, v2GameGenre, gameVersion3, gameFile3, gameImage3, gameVideo3, edition2, v2EditionAuth, seat2) handlerAPI, err := handler.NewAPI(app, v1Handler, session, api, v2API) if err != nil { return nil, err From b7860a2f63fe140410ff26e10c7597f7430abcf1 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:44:36 +0900 Subject: [PATCH 05/16] =?UTF-8?q?:sparkles:=20service=E3=81=A7=E3=82=B2?= =?UTF-8?q?=E3=83=BC=E3=83=A0=E3=82=B8=E3=83=A3=E3=83=B3=E3=83=AB=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/game_genre.go | 4 ++++ src/service/errors.go | 1 + src/service/game_genre.go | 13 +++++++++++++ src/service/v2/game_genre.go | 31 +++++++++++++++++++++++++++++++ src/wire/service.go | 7 +++++-- 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/service/game_genre.go create mode 100644 src/service/v2/game_genre.go diff --git a/src/repository/game_genre.go b/src/repository/game_genre.go index 40a91ffc..2a1d1fa2 100644 --- a/src/repository/game_genre.go +++ b/src/repository/game_genre.go @@ -11,4 +11,8 @@ import ( type GameGenre interface { GetGenresByGameID(ctx context.Context, gameID values.GameID) ([]*domain.GameGenre, error) + // RemoveGameGenre + // ゲームジャンルを削除する。 + // IDに該当するゲームジャンルが存在しない場合は、ErrNoRecordDeletedを返す。 + RemoveGameGenre(ctx context.Context, gameGenreID values.GameGenreID) error } diff --git a/src/service/errors.go b/src/service/errors.go index 2ec084b9..bf380afb 100644 --- a/src/service/errors.go +++ b/src/service/errors.go @@ -44,4 +44,5 @@ var ( ErrDuplicateGameVersion = errors.New("duplicate game version") ErrInvalidLimit = errors.New("invalid limit") ErrNoAsset = errors.New("no asset") + ErrNoGameGenre = errors.New("no game genre") ) diff --git a/src/service/game_genre.go b/src/service/game_genre.go new file mode 100644 index 00000000..20d29f14 --- /dev/null +++ b/src/service/game_genre.go @@ -0,0 +1,13 @@ +package service + +import ( + "context" + + "github.com/traPtitech/trap-collection-server/src/domain/values" +) + +type GameGenre interface { + // ゲームジャンルを削除する。 + // ゲームジャンルが存在しない場合は、ErrNoGameGenreを返す。 + DeleteGameGenre(ctx context.Context, gameGenreID values.GameGenreID) error +} diff --git a/src/service/v2/game_genre.go b/src/service/v2/game_genre.go new file mode 100644 index 00000000..0c81794e --- /dev/null +++ b/src/service/v2/game_genre.go @@ -0,0 +1,31 @@ +package v2 + +import ( + "context" + "errors" + + "github.com/traPtitech/trap-collection-server/src/domain/values" + "github.com/traPtitech/trap-collection-server/src/repository" + "github.com/traPtitech/trap-collection-server/src/service" +) + +type GameGenre struct { + gameGenreRepository repository.GameGenre +} + +func NewGameGenre(gameGenreRepository repository.GameGenre) *GameGenre { + return &GameGenre{ + gameGenreRepository: gameGenreRepository, + } +} + +var _ service.GameGenre = &GameGenre{} + +func (gameGenre *GameGenre) DeleteGameGenre(ctx context.Context, gameGenreID values.GameGenreID) error { + err := gameGenre.gameGenreRepository.RemoveGameGenre(ctx, gameGenreID) + if errors.Is(err, repository.ErrNoRecordDeleted) { + return service.ErrNoGameGenre + } + + return nil +} diff --git a/src/wire/service.go b/src/wire/service.go index 3a3c8031..2f818213 100644 --- a/src/wire/service.go +++ b/src/wire/service.go @@ -5,8 +5,8 @@ package wire import ( "github.com/google/wire" "github.com/traPtitech/trap-collection-server/src/service" - "github.com/traPtitech/trap-collection-server/src/service/v1" - "github.com/traPtitech/trap-collection-server/src/service/v2" + v1 "github.com/traPtitech/trap-collection-server/src/service/v1" + v2 "github.com/traPtitech/trap-collection-server/src/service/v2" ) var ( @@ -87,6 +87,9 @@ var ( wire.Bind(new(service.Seat), new(*v2.Seat)), v2.NewSeat, + wire.Bind(new(service.GameGenre), new(*v2.GameGenre)), + v2.NewGameGenre, + v2.NewUser, ) ) From ad710f357a27033fdca7e0376993da3441c4db1a Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:54:19 +0900 Subject: [PATCH 06/16] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/v2/game_genre.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/service/v2/game_genre.go b/src/service/v2/game_genre.go index 0c81794e..3664f45b 100644 --- a/src/service/v2/game_genre.go +++ b/src/service/v2/game_genre.go @@ -26,6 +26,9 @@ func (gameGenre *GameGenre) DeleteGameGenre(ctx context.Context, gameGenreID val if errors.Is(err, repository.ErrNoRecordDeleted) { return service.ErrNoGameGenre } + if err != nil { + return err + } return nil } From e7ed3de486bafc44406235fef7bd1867d99adc44 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sat, 14 Oct 2023 13:58:58 +0900 Subject: [PATCH 07/16] =?UTF-8?q?:white=5Fcheck=5Fmark:=20DeleteGameGenre?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/v2/game_genre_test.go | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/service/v2/game_genre_test.go diff --git a/src/service/v2/game_genre_test.go b/src/service/v2/game_genre_test.go new file mode 100644 index 00000000..760e3bd8 --- /dev/null +++ b/src/service/v2/game_genre_test.go @@ -0,0 +1,75 @@ +package v2 + +import ( + "context" + "errors" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + "github.com/traPtitech/trap-collection-server/src/domain/values" + "github.com/traPtitech/trap-collection-server/src/repository" + mockRepository "github.com/traPtitech/trap-collection-server/src/repository/mock" + "github.com/traPtitech/trap-collection-server/src/service" +) + +func TestDeleteGameGenre(t *testing.T) { + t.Parallel() + + ctx := context.Background() + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockGameGenreRepository := mockRepository.NewMockGameGenre(ctrl) + + gameGenreService := NewGameGenre(mockGameGenreRepository) + + type test struct { + ID values.GameGenreID + RemoveGameGenreErr error + isErr bool + expectedErr error + } + + genreID := values.NewGameGenreID() + + testCases := map[string]test{ + "特に問題ないのでエラー無し": { + ID: genreID, + }, + "RemoveGameGenreがErrNoRecordDeletedなのでエラー": { + ID: genreID, + RemoveGameGenreErr: repository.ErrNoRecordDeleted, + isErr: true, + expectedErr: service.ErrNoGameGenre, + }, + "RemoveGameGenreが他のエラーなのでエラー": { + ID: genreID, + RemoveGameGenreErr: errors.New("error"), + isErr: true, + }, + } + + for description, testCase := range testCases { + t.Run(description, func(t *testing.T) { + mockGameGenreRepository. + EXPECT(). + RemoveGameGenre(ctx, testCase.ID). + Return(testCase.RemoveGameGenreErr) + + err := gameGenreService.DeleteGameGenre(ctx, genreID) + + if !testCase.isErr { + assert.NoError(t, err) + return + } + + if testCase.expectedErr != nil { + assert.ErrorIs(t, err, testCase.expectedErr) + } else { + assert.Error(t, err) + } + }) + } +} From 4bc6aef21507ba969e5e8fcb7d40bb16b7d7899c Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sat, 14 Oct 2023 14:14:53 +0900 Subject: [PATCH 08/16] =?UTF-8?q?:sparkles:=20repository=E3=81=A7RemoveGam?= =?UTF-8?q?eGenre=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/repository/gorm2/game_genre.go b/src/repository/gorm2/game_genre.go index 4c9b9d07..53a1dbae 100644 --- a/src/repository/gorm2/game_genre.go +++ b/src/repository/gorm2/game_genre.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/google/uuid" "github.com/traPtitech/trap-collection-server/src/domain" "github.com/traPtitech/trap-collection-server/src/domain/values" "github.com/traPtitech/trap-collection-server/src/repository" @@ -22,7 +23,7 @@ func NewGameGenre(db *DB) *GameGenre { } } -var _ repository.GameGenre = &GameGenre{} +var _ repository.GameGenre = &GameGenre{} func (gameGenre *GameGenre) GetGenresByGameID(ctx context.Context, gameID values.GameID) ([]*domain.GameGenre, error) { db, err := gameGenre.db.getDB(ctx) @@ -50,3 +51,23 @@ func (gameGenre *GameGenre) GetGenresByGameID(ctx context.Context, gameID values } return result, nil } + +func (gameGenre *GameGenre) RemoveGameGenre(ctx context.Context, gameGenreID values.GameGenreID) error { + db, err := gameGenre.db.getDB(ctx) + if err != nil { + return fmt.Errorf("failed to get db: %w", err) + } + + result := db. + Select("Games"). + Delete(&migrate.GameGenreTable{ID: uuid.UUID(gameGenreID)}) + err = result.Error + if result.RowsAffected == 0 { + return repository.ErrNoRecordDeleted + } + if err != nil { + return fmt.Errorf("failed to remove game genre: %w", err) + } + + return nil +} From b0eae2a7753307d179e84cbec7ff57c8c30981b0 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:04:13 +0900 Subject: [PATCH 09/16] =?UTF-8?q?:white=5Fcheck=5Fmark:=20repository?= =?UTF-8?q?=E3=81=AERemoveGameGenre=E3=81=AB=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre_test.go | 227 ++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 src/repository/gorm2/game_genre_test.go diff --git a/src/repository/gorm2/game_genre_test.go b/src/repository/gorm2/game_genre_test.go new file mode 100644 index 00000000..54d9923c --- /dev/null +++ b/src/repository/gorm2/game_genre_test.go @@ -0,0 +1,227 @@ +package gorm2 + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/traPtitech/trap-collection-server/src/domain/values" + "github.com/traPtitech/trap-collection-server/src/repository" + "github.com/traPtitech/trap-collection-server/src/repository/gorm2/migrate" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func TestRemoveGameGenre(t *testing.T) { + ctx := context.Background() + + db, err := testDB.getDB(ctx) + if err != nil { + t.Fatalf("failed to get db: %+v\n", err) + } + + gameGenreRepository := NewGameGenre(testDB) + + type test struct { + genreID values.GameGenreID + beforeGameGenres []migrate.GameGenreTable + afterGameGenres []migrate.GameGenreTable + isErr bool + expectedErr error + } + + now := time.Now() + + genreID1 := values.NewGameGenreID() + genreID2 := values.NewGameGenreID() + genreID3 := values.NewGameGenreID() + genreID4 := values.NewGameGenreID() + genreID5 := values.NewGameGenreID() + genreID6 := values.NewGameGenreID() + genreID7 := values.NewGameGenreID() + genreID8 := values.NewGameGenreID() + + gameID1 := values.NewGameID() + + testCases := map[string]test{ + "特に問題ないのでエラー無し": { + genreID: genreID1, + beforeGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID1), + Name: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + afterGameGenres: []migrate.GameGenreTable{}, + }, + "該当するジャンルが存在しないのでErrNoRecordDeleted": { + genreID: genreID2, + beforeGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID3), + Name: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + afterGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID3), + Name: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + isErr: true, + expectedErr: repository.ErrNoRecordDeleted, + }, + "ジャンルが複数あっても問題なし": { + genreID: genreID4, + beforeGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID4), + Name: "test1", + CreatedAt: now.Add(-time.Hour), + }, + { + ID: uuid.UUID(genreID5), + Name: "test2", + CreatedAt: now.Add(-time.Hour * 2), + }, + }, + afterGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID5), + Name: "test2", + CreatedAt: now.Add(-time.Hour * 2), + }, + }, + }, + "削除に失敗したのでエラー": { + genreID: genreID6, + isErr: true, + }, + "ゲームが紐づいていてもエラー無し": { + genreID: genreID7, + beforeGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID7), + Name: "test", + CreatedAt: now.Add(-time.Hour), + Games: []migrate.GameTable2{ + { + ID: uuid.UUID(gameID1), + Name: "test", + Description: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + }, + { + ID: uuid.UUID(genreID8), + Name: "test2", + CreatedAt: now.Add(-time.Hour * 2), + Games: []migrate.GameTable2{ + { + ID: uuid.UUID(gameID1), + Name: "test", + Description: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + }, + }, + afterGameGenres: []migrate.GameGenreTable{ + { + ID: uuid.UUID(genreID8), + Name: "test2", + CreatedAt: now.Add(-time.Hour * 2), + Games: []migrate.GameTable2{ + { + ID: uuid.UUID(gameID1), + Name: "test", + Description: "test", + CreatedAt: now.Add(-time.Hour), + }, + }, + }, + }, + }, + } + + for description, testCase := range testCases { + t.Run(description, func(t *testing.T) { + // 1個テストケースを実行したらテーブルの中身全部削除 + defer func() { + _db := db. + Session(&gorm.Session{ + AllowGlobalUpdate: true, + }) + + var genres []migrate.GameGenreTable + err := _db.Find(&genres).Error + if err != nil { + t.Fatalf("failed to get genres") + } + + err = _db. + Select("Games"). + Delete(&genres).Error + if err != nil { + t.Fatalf("failed to delete genres: %+v\n", err) + } + }() + + if testCase.beforeGameGenres != nil && len(testCase.beforeGameGenres) != 0 { + err := db. + Session(&gorm.Session{ + Logger: logger.Default.LogMode(logger.Info), + }). + Create(&testCase.beforeGameGenres).Error + if err != nil { + t.Fatalf("failed to create genre: %+v\n", err) + } + } + + err = gameGenreRepository.RemoveGameGenre(ctx, testCase.genreID) + + if testCase.isErr { + if testCase.expectedErr == nil { + assert.Error(t, err) + } else { + assert.ErrorIs(t, err, testCase.expectedErr) + } + } else { + assert.NoError(t, err) + } + if err != nil { + return + } + + var genres []migrate.GameGenreTable + err = db. + Preload("Games"). + Find(&genres).Error + if err != nil { + t.Fatalf("failed to get genres: %+v", err) + } + + assert.Len(t, genres, len(testCase.afterGameGenres)) + + for i, genre := range genres { + assert.Equal(t, testCase.afterGameGenres[i].ID, genre.ID) + assert.Equal(t, testCase.afterGameGenres[i].Name, genre.Name) + assert.WithinDuration(t, testCase.afterGameGenres[i].CreatedAt, genre.CreatedAt, time.Second) + + assert.Len(t, genre.Games, len(testCase.afterGameGenres[i].Games)) + for j, game := range genre.Games { + assert.Equal(t, testCase.afterGameGenres[i].Games[j].ID, game.ID) + assert.Equal(t, testCase.afterGameGenres[i].Games[j].Name, game.Name) + assert.Equal(t, testCase.afterGameGenres[i].Games[j].Description, game.Description) + assert.WithinDuration(t, testCase.afterGameGenres[i].Games[j].CreatedAt, game.CreatedAt, time.Second) + } + } + }) + } +} From 2f485cbdd56600424a69b7ffef38088f84e3cc19 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sun, 15 Oct 2023 14:43:51 +0900 Subject: [PATCH 10/16] =?UTF-8?q?:sparkles:=20handler=E3=81=A7DeleteGameGe?= =?UTF-8?q?nre=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/game_genre.go | 33 +++++++++++++++++++++++++++------ src/wire/wire_gen.go | 5 +++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/handler/v2/game_genre.go b/src/handler/v2/game_genre.go index 141ff991..ea4d6c10 100644 --- a/src/handler/v2/game_genre.go +++ b/src/handler/v2/game_genre.go @@ -1,16 +1,25 @@ package v2 import ( + "errors" + "log" + "net/http" + "github.com/labstack/echo/v4" + "github.com/traPtitech/trap-collection-server/src/domain/values" "github.com/traPtitech/trap-collection-server/src/handler/v2/openapi" + "github.com/traPtitech/trap-collection-server/src/service" ) type GameGenre struct { - gameGenreUnimplemented + gameGenreService service.GameGenre + gameGenreUnimplemented //実装し終わったら消す } -func NewGameGenre() *GameGenre { - return &GameGenre{} +func NewGameGenre(gameGenreService service.GameGenre) *GameGenre { + return &GameGenre{ + gameGenreService: gameGenreService, + } } // gameGenreUnimplemented @@ -20,9 +29,6 @@ type gameGenreUnimplemented interface { // 全てのジャンルの取得 // (GET /genres) GetGameGenres(ctx echo.Context) error - // ジャンルの削除 - // (DELETE /genres/{gameGenreID}) - DeleteGameGenre(ctx echo.Context, gameGenreID openapi.GameGenreIDInPath) error // ジャンル情報の変更 // (PATCH /genres/{gameGenreID}) PatchGameGenre(ctx echo.Context, gameGenreID openapi.GameGenreIDInPath) error @@ -30,3 +36,18 @@ type gameGenreUnimplemented interface { // (PUT /games/{gameID}/genres) PutGameGenres(ctx echo.Context, gameID openapi.GameIDInPath) error } + +// ジャンルの削除 +// (DELETE /genres/{gameGenreID}) +func (gameGenre GameGenre) DeleteGameGenre(c echo.Context, gameGenreID openapi.GameGenreIDInPath) error { + err := gameGenre.gameGenreService.DeleteGameGenre(c.Request().Context(), values.GameGenreIDFromUUID(gameGenreID)) + if errors.Is(err, service.ErrNoGameGenre) { + return echo.NewHTTPError(http.StatusNotFound, "invalid game genre ID") + } + if err != nil { + log.Printf("error: failed to delete game genre: %v\n", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete game genre") + } + + return c.NoContent(http.StatusOK) +} diff --git a/src/wire/wire_gen.go b/src/wire/wire_gen.go index 125723be..22c3a490 100644 --- a/src/wire/wire_gen.go +++ b/src/wire/wire_gen.go @@ -208,7 +208,8 @@ func InjectApp() (*App, error) { v2Game := v2_2.NewGame(db, gameV2, gameManagementRole, gameGenre, v2User) game3 := v2.NewGame(v2Session, v2Game) gameRole2 := v2.NewGameRole(v2GameRole, v2Game, v2Session) - v2GameGenre := v2.NewGameGenre() + v2GameGenre := v2_2.NewGameGenre(gameGenre) + gameGenre2 := v2.NewGameGenre(v2GameGenre) gameImageV2 := gorm2.NewGameImageV2(db) gameVideoV2 := gorm2.NewGameVideoV2(db) v2GameVersion := v2_2.NewGameVersion(db, game, gameImageV2, gameVideoV2, gameFileV2, gameVersionV2) @@ -228,7 +229,7 @@ func InjectApp() (*App, error) { } v2Seat := v2_2.NewSeat(db, seat, ristrettoSeat) seat2 := v2.NewSeat(v2Seat) - v2API := v2.NewAPI(checker, v2Session, v2OAuth2, user3, admin, game3, gameRole2, v2GameGenre, gameVersion3, gameFile3, gameImage3, gameVideo3, edition2, v2EditionAuth, seat2) + v2API := v2.NewAPI(checker, v2Session, v2OAuth2, user3, admin, game3, gameRole2, gameGenre2, gameVersion3, gameFile3, gameImage3, gameVideo3, edition2, v2EditionAuth, seat2) handlerAPI, err := handler.NewAPI(app, v1Handler, session, api, v2API) if err != nil { return nil, err From 208ef0fddcd8e3ba9e9005699b4bf943b173708a Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sun, 15 Oct 2023 15:19:48 +0900 Subject: [PATCH 11/16] =?UTF-8?q?:white=5Fcheck=5Fmark:=20handler=E3=81=AE?= =?UTF-8?q?DeleteGameGenre=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/game_genre_test.go | 94 +++++++++++++++++++++++++++++++ src/service/game_genre.go | 2 + 2 files changed, 96 insertions(+) create mode 100644 src/handler/v2/game_genre_test.go diff --git a/src/handler/v2/game_genre_test.go b/src/handler/v2/game_genre_test.go new file mode 100644 index 00000000..362862b5 --- /dev/null +++ b/src/handler/v2/game_genre_test.go @@ -0,0 +1,94 @@ +package v2 + +import ( + "errors" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/golang/mock/gomock" + "github.com/google/uuid" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/traPtitech/trap-collection-server/src/domain/values" + "github.com/traPtitech/trap-collection-server/src/handler/v2/openapi" + "github.com/traPtitech/trap-collection-server/src/service" + "github.com/traPtitech/trap-collection-server/src/service/mock" +) + +func TestDeleteGameGenre(t *testing.T) { + t.Parallel() + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockGameGenreService := mock.NewMockGameGenre(ctrl) + + gameGenre := NewGameGenre(mockGameGenreService) + + type test struct { + genreID openapi.GameGenreIDInPath + DeleteGameGenreErr error + isErr bool + expectedErr error + statusCode int + } + + testCases := map[string]test{ + "特に問題ないのでエラー無し": { + genreID: uuid.New(), + }, + "存在しないジャンルIDなので404": { + genreID: uuid.New(), + DeleteGameGenreErr: service.ErrNoGameGenre, + isErr: true, + statusCode: http.StatusNotFound, + }, + "DeleteGameGenreがエラーなのでエラー": { + genreID: uuid.New(), + DeleteGameGenreErr: errors.New("error"), + isErr: true, + statusCode: http.StatusInternalServerError, + }, + } + + for description, testCase := range testCases { + t.Run(description, func(t *testing.T) { + mockGameGenreService. + EXPECT(). + DeleteGameGenre(gomock.Any(), values.GameGenreIDFromUUID(testCase.genreID)). + Return(testCase.DeleteGameGenreErr) + + e := echo.New() + req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v2/genres/%s", testCase.genreID), nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := gameGenre.DeleteGameGenre(c, testCase.genreID) + + if testCase.isErr { + if testCase.statusCode != 0 { + var httpError *echo.HTTPError + if errors.As(err, &httpError) { + assert.Equal(t, testCase.statusCode, httpError.Code) + } else { + t.Errorf("error is not *echoHTTPError: %v", err) + } + } else if testCase.expectedErr != nil { + assert.ErrorIs(t, err, testCase.expectedErr) + } else { + assert.Error(t, err) + } + } else { + assert.NoError(t, err) + } + + if err != nil || testCase.isErr { + return + } + + assert.Equal(t, http.StatusOK, rec.Code) + }) + } +} diff --git a/src/service/game_genre.go b/src/service/game_genre.go index 20d29f14..31d903cd 100644 --- a/src/service/game_genre.go +++ b/src/service/game_genre.go @@ -1,5 +1,7 @@ package service +//go:generate go run github.com/golang/mock/mockgen -source=$GOFILE -destination=mock/${GOFILE} -package=mock + import ( "context" From f0e06035f107f647f5abe4cdd9c16c728452717a Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sun, 15 Oct 2023 15:47:50 +0900 Subject: [PATCH 12/16] =?UTF-8?q?:bug:=20=E3=82=B8=E3=83=A3=E3=83=B3?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=81=A7=E3=82=B2?= =?UTF-8?q?=E3=83=BC=E3=83=A0=E3=82=92=E5=89=8A=E9=99=A4=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=AA=E3=81=8B=E3=81=A3=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/repository/gorm2/game_genre_test.go b/src/repository/gorm2/game_genre_test.go index 54d9923c..3131607e 100644 --- a/src/repository/gorm2/game_genre_test.go +++ b/src/repository/gorm2/game_genre_test.go @@ -171,6 +171,11 @@ func TestRemoveGameGenre(t *testing.T) { if err != nil { t.Fatalf("failed to delete genres: %+v\n", err) } + + err = _db.Delete(&migrate.GameTable2{}).Error + if err != nil { + t.Fatalf("failed to delete games: %+v\n", err) + } }() if testCase.beforeGameGenres != nil && len(testCase.beforeGameGenres) != 0 { From eafe3f72e5fa0b191f23885588e15febbcaac3db Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sun, 15 Oct 2023 16:02:41 +0900 Subject: [PATCH 13/16] :art: go fmt --- src/service/v2/game.go | 8 ++++---- src/service/v2/game_test.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/service/v2/game.go b/src/service/v2/game.go index 02039ede..f980482c 100644 --- a/src/service/v2/game.go +++ b/src/service/v2/game.go @@ -28,11 +28,11 @@ func NewGame( user *User, ) *Game { return &Game{ - db: db, - gameRepository: gameRepository, - gameManagementRole: gameManagementRole, + db: db, + gameRepository: gameRepository, + gameManagementRole: gameManagementRole, gameGenreRepository: gameGenreRepository, - user: user, + user: user, } } diff --git a/src/service/v2/game_test.go b/src/service/v2/game_test.go index 002bb393..7e1bd10c 100644 --- a/src/service/v2/game_test.go +++ b/src/service/v2/game_test.go @@ -604,17 +604,17 @@ func TestGetGame(t *testing.T) { isErr: true, }, { - description: "GetGameGenresByGameIDがエラーなのでエラー", - gameID: gameID, + description: "GetGameGenresByGameIDがエラーなのでエラー", + gameID: gameID, executeGetGameManagersByGameID: true, - executeGetActiveUsers: true, - executeGetGenresByGameID: true, - GetGenresByGameIDErr: errors.New("error"), - isErr: true, + executeGetActiveUsers: true, + executeGetGenresByGameID: true, + GetGenresByGameIDErr: errors.New("error"), + isErr: true, }, { description: "Genreが空でも問題ない", - gameID: gameID, + gameID: gameID, game: domain.NewGame( gameID, "game name", @@ -691,7 +691,7 @@ func TestGetGame(t *testing.T) { } if testCase.genres != nil { - for i:= 0; i < len(testCase.genres); i++ { + for i := 0; i < len(testCase.genres); i++ { assert.Equal(t, testCase.genres[i], gameInfo.Genres[i]) assert.Equal(t, testCase.genres[i].GetID(), gameInfo.Genres[i].GetID()) From 5929f71821336ea674c55c17dfa8f24add8a3a8b Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sun, 15 Oct 2023 17:05:54 +0900 Subject: [PATCH 14/16] =?UTF-8?q?:recycle:=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B1=E3=83=BC=E3=82=B9=E3=82=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre.go | 7 ++++--- src/repository/gorm2/game_genre_test.go | 13 ++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/repository/gorm2/game_genre.go b/src/repository/gorm2/game_genre.go index 53a1dbae..a55c15ca 100644 --- a/src/repository/gorm2/game_genre.go +++ b/src/repository/gorm2/game_genre.go @@ -62,12 +62,13 @@ func (gameGenre *GameGenre) RemoveGameGenre(ctx context.Context, gameGenreID val Select("Games"). Delete(&migrate.GameGenreTable{ID: uuid.UUID(gameGenreID)}) err = result.Error - if result.RowsAffected == 0 { - return repository.ErrNoRecordDeleted - } if err != nil { return fmt.Errorf("failed to remove game genre: %w", err) } + if result.RowsAffected == 0 { + return repository.ErrNoRecordDeleted + } + return nil } diff --git a/src/repository/gorm2/game_genre_test.go b/src/repository/gorm2/game_genre_test.go index 3131607e..9aebf5b1 100644 --- a/src/repository/gorm2/game_genre_test.go +++ b/src/repository/gorm2/game_genre_test.go @@ -41,7 +41,6 @@ func TestRemoveGameGenre(t *testing.T) { genreID5 := values.NewGameGenreID() genreID6 := values.NewGameGenreID() genreID7 := values.NewGameGenreID() - genreID8 := values.NewGameGenreID() gameID1 := values.NewGameID() @@ -98,15 +97,11 @@ func TestRemoveGameGenre(t *testing.T) { }, }, }, - "削除に失敗したのでエラー": { - genreID: genreID6, - isErr: true, - }, "ゲームが紐づいていてもエラー無し": { - genreID: genreID7, + genreID: genreID6, beforeGameGenres: []migrate.GameGenreTable{ { - ID: uuid.UUID(genreID7), + ID: uuid.UUID(genreID6), Name: "test", CreatedAt: now.Add(-time.Hour), Games: []migrate.GameTable2{ @@ -119,7 +114,7 @@ func TestRemoveGameGenre(t *testing.T) { }, }, { - ID: uuid.UUID(genreID8), + ID: uuid.UUID(genreID7), Name: "test2", CreatedAt: now.Add(-time.Hour * 2), Games: []migrate.GameTable2{ @@ -134,7 +129,7 @@ func TestRemoveGameGenre(t *testing.T) { }, afterGameGenres: []migrate.GameGenreTable{ { - ID: uuid.UUID(genreID8), + ID: uuid.UUID(genreID7), Name: "test2", CreatedAt: now.Add(-time.Hour * 2), Games: []migrate.GameTable2{ From 90e85786e6c9acf146d2d30ba13079df1ca6bd5d Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:32:30 +0900 Subject: [PATCH 15/16] =?UTF-8?q?:bug:=20=E3=83=87=E3=83=BC=E3=82=BF?= =?UTF-8?q?=E3=81=AE=E6=AF=94=E8=BC=83=E3=82=92=E8=A1=8C=E3=81=88=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=AA=E3=81=84=E9=83=A8=E5=88=86=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/repository/gorm2/game_genre_test.go b/src/repository/gorm2/game_genre_test.go index 9aebf5b1..377eb71d 100644 --- a/src/repository/gorm2/game_genre_test.go +++ b/src/repository/gorm2/game_genre_test.go @@ -195,9 +195,9 @@ func TestRemoveGameGenre(t *testing.T) { } else { assert.NoError(t, err) } - if err != nil { - return - } + // if err != nil && errors.Is(err, testCase.expectedErr) { + // return + // } var genres []migrate.GameGenreTable err = db. From dc5a573a0ba7b54b06aee296a19deb3813ff2246 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:41:43 +0900 Subject: [PATCH 16/16] =?UTF-8?q?:recycle:=20=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/gorm2/game_genre_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/repository/gorm2/game_genre_test.go b/src/repository/gorm2/game_genre_test.go index 377eb71d..6ec56f30 100644 --- a/src/repository/gorm2/game_genre_test.go +++ b/src/repository/gorm2/game_genre_test.go @@ -195,9 +195,6 @@ func TestRemoveGameGenre(t *testing.T) { } else { assert.NoError(t, err) } - // if err != nil && errors.Is(err, testCase.expectedErr) { - // return - // } var genres []migrate.GameGenreTable err = db.