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

DELETE /genres/{genreID}の実装 #729

Merged
merged 16 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ tmp
!src/repository/mock/db.go
cache/*
.idea
.DS_Store
33 changes: 27 additions & 6 deletions src/handler/v2/game_genre.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,13 +29,25 @@ 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
// ゲームのジャンル編集
// (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)
}
94 changes: 94 additions & 0 deletions src/handler/v2/game_genre_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
18 changes: 18 additions & 0 deletions src/repository/game_genre.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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)
// RemoveGameGenre
// ゲームジャンルを削除する。
// IDに該当するゲームジャンルが存在しない場合は、ErrNoRecordDeletedを返す。
RemoveGameGenre(ctx context.Context, gameGenreID values.GameGenreID) error
}
74 changes: 74 additions & 0 deletions src/repository/gorm2/game_genre.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gorm2

import (
"context"
"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"
"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
}

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 err != nil {
return fmt.Errorf("failed to remove game genre: %w", err)
}

if result.RowsAffected == 0 {
return repository.ErrNoRecordDeleted
}

return nil
}
Loading