Skip to content

Commit

Permalink
:sparcles: PatchGameの修正、GetGameIcon GetGameImageの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
pikachu0310 committed Jan 31, 2024
1 parent cb60b2e commit f320a78
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 50 deletions.
60 changes: 44 additions & 16 deletions internal/handler/game.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/game3-back/internal/api/models"
Expand All @@ -19,23 +18,19 @@ func (h *Handler) GetGames(c echo.Context, params models.GetGamesParams) error {

func (h *Handler) PostGame(c echo.Context) error {
req := &models.PostGameRequest{}
if err := c.Bind(req); err != nil {
err := c.Bind(req)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

iconFile, err := c.FormFile("icon")
icon, err := h.handleFile(c, "icon")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to get icon file: "+err.Error())
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get image file: "+err.Error())
}
req.Icon.InitFromMultipart(iconFile)

imageFile, err := c.FormFile("image")
if errors.Is(err, http.ErrMissingFile) {
// 画像がない場合はエラーにしない
} else if err != nil {
req.Icon = *icon
req.Image, err = h.handleFile(c, "image")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get image file: "+err.Error())
} else {
req.Image.InitFromMultipart(imageFile)
}

newGameID := uuid.New()
Expand All @@ -61,13 +56,46 @@ func (h *Handler) GetGame(c echo.Context, gameID models.GameIdInPath) error {
}

func (h *Handler) PatchGame(c echo.Context, gameID models.GameIdInPath) error {
panic("implement me")
req := &models.PatchGameRequest{}
err := c.Bind(req)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

req.Icon, err = h.handleFile(c, "icon")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get image file: "+err.Error())
}
req.Image, err = h.handleFile(c, "image")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get image file: "+err.Error())
}

if err := h.repo.PatchGame(gameID, req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

game, err := h.repo.GetGame(gameID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, game)
}

func (h *Handler) GetGameIcon(c echo.Context, gameID models.GameIdInPath) error {
//TODO implement me
panic("implement me")
icon, err := h.repo.GetGameIcon(gameID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.Blob(http.StatusOK, "image/png", icon)
}
func (h *Handler) GetGameImage(c echo.Context, gameID models.GameIdInPath) error {
panic("implement me")
image, err := h.repo.GetGameImage(gameID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.Blob(http.StatusOK, "image/png", image)
}
24 changes: 0 additions & 24 deletions internal/repository/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,6 @@ func (r *Repository) DeleteEvent(eventSlug models.EventSlugInPath) error {
return nil
}

func (r *Repository) GetEventTerms(eventSlug models.EventSlugInPath) ([]*models.Term, error) {
terms := []*models.Term{}
query := "SELECT term.* FROM term JOIN event ON term.event_slug = event.slug WHERE event.slug = ?"
if err := r.db.Select(&terms, query, eventSlug); err != nil {
return nil, err
}

return terms, nil
}

func (r *Repository) GetEventGames(eventSlug models.EventSlugInPath) ([]*models.Game, error) {
games := []*models.Game{}
query := selectGameWithoutImagesQuery() + `
FROM game
JOIN term ON game.term_id = term.id
JOIN event ON term.event_slug = event.slug
WHERE event.slug = ?`
if err := r.db.Select(&games, query, eventSlug); err != nil {
return nil, err
}

return games, nil
}

func (r *Repository) GetEventImage(eventSlug models.EventSlugInPath) ([]byte, error) {
file := []byte{}
query := "SELECT image FROM event WHERE slug = ?"
Expand Down
73 changes: 67 additions & 6 deletions internal/repository/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,39 @@ func (r *Repository) GetGames(params models.GetGamesParams) ([]*domains.Game, er
query := selectGameWithoutImagesQuery()
whereClauses := []string{}
args := []interface{}{}
joinedTerm := false

// term_idによるフィルタ
if params.TermId != nil {
whereClauses = append(whereClauses, "game.termId = ?")
whereClauses = append(whereClauses, "game.term_id = ?")
args = append(args, params.TermId)
}

// event_slugによるフィルタ。eventとtermテーブルを結合
if params.EventSlug != nil {
whereClauses = append(whereClauses, "game.eventId = ?")
query += `JOIN term ON game.term_id = term.id
JOIN event ON term.event_slug = event.slug `
whereClauses = append(whereClauses, "event.slug = ?")
args = append(args, params.EventSlug)
joinedTerm = true
}

// discordUserIdによるフィルタ
if params.UserId != nil {
whereClauses = append(whereClauses, "game.discordUserId = ?")
whereClauses = append(whereClauses, "game.discord_user_id = ?")
args = append(args, params.UserId)
}

// 未公開のゲームを含むかどうか
if params.Include != nil && *params.Include == "unpublished" {
query += "JOIN term ON game.termId = term.id "
whereClauses = append(whereClauses, "term.isDefault = TRUE")
if !joinedTerm {
// termテーブルをJOINしていない場合、ここでJOINする
query += `JOIN term ON game.term_id = term.id `
}
whereClauses = append(whereClauses, "term.is_default = TRUE")
}

// WHERE句の組み立て
if len(whereClauses) > 0 {
query += "WHERE " + strings.Join(whereClauses, " AND ")
}
Expand All @@ -59,10 +74,11 @@ func (r *Repository) PostGame(newGameID uuid.UUID, game *models.PostGameRequest)
return err
}
}
_, err = r.db.Exec("INSERT INTO game (id, termId, discordUserId, creatorName, creatorPageUrl, gamePageUrl, title, description, icon, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", newGameID, uuid.UUID{}, uuid.UUID{}, game.CreatorName, game.CreatorPageUrl, game.GamePageUrl, game.Title, game.Description, iconData, imageData)
_, err = r.db.Exec("INSERT INTO game (id, term_id, discord_user_id, creator_name, creator_page_url, game_page_url, title, description, icon, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", newGameID, uuid.UUID{}, uuid.UUID{}, game.CreatorName, game.CreatorPageUrl, game.GamePageUrl, game.Title, game.Description, iconData, imageData)
if err != nil {
return err
}

return nil
}

Expand All @@ -75,3 +91,48 @@ func (r *Repository) GetGame(gameID uuid.UUID) (*domains.Game, error) {

return game, nil
}

func (r *Repository) PatchGame(gameID uuid.UUID, game *models.PatchGameRequest) error {
return r.Patch("game", "id", gameID, game)
}

func (r *Repository) DeleteGame(gameID uuid.UUID) error {
_, err := r.db.Exec("DELETE FROM game WHERE id = ?", gameID)
if err != nil {
return err
}

return nil
}

func (r *Repository) GetGameIcon(gameID uuid.UUID) ([]byte, error) {
icon := []byte{}
if err := r.db.Get(&icon, "SELECT icon FROM game WHERE id = ?", gameID); err != nil {
return nil, err
}

return icon, nil
}

func (r *Repository) GetGameImage(gameID uuid.UUID) ([]byte, error) {
image := []byte{}
if err := r.db.Get(&image, "SELECT icon FROM game WHERE id = ?", gameID); err != nil {
return nil, err
}

return image, nil
}

func (r *Repository) GetEventGames(eventSlug models.EventSlugInPath) ([]*models.Game, error) {
games := []*models.Game{}
query := selectGameWithoutImagesQuery() + `
FROM game
JOIN term ON game.term_id = term.id
JOIN event ON term.event_slug = event.slug
WHERE event.slug = ?`
if err := r.db.Select(&games, query, eventSlug); err != nil {
return nil, err
}

return games, nil
}
14 changes: 10 additions & 4 deletions internal/repository/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *Repository) GetTerms() ([]*models.Term, error) {
}

func (r *Repository) PostTerm(newTermID uuid.UUID, req *models.PostTermRequest) error {
if _, err := r.db.Exec("INSERT INTO term (id, eventSlug, isDefault, startAt, endAt) VALUES (?, ?, ?, ?, ?)", newTermID, req.EventSlug, false, req.StartAt, req.EndAt); err != nil {
if _, err := r.db.Exec("INSERT INTO term (id, event_slug, is_default, start_at, end_at) VALUES (?, ?, ?, ?, ?)", newTermID, req.EventSlug, false, req.StartAt, req.EndAt); err != nil {
return err
}

Expand All @@ -32,9 +32,15 @@ func (r *Repository) GetTerm(termID uuid.UUID) (*models.Term, error) {
}

func (r *Repository) PatchTerm(termID uuid.UUID, req *models.PatchTermRequest) error {
if _, err := r.db.Exec("UPDATE term SET eventSlug = ?, isDefault = ?, startAt = ?, endAt = ? WHERE id = ?", req.EventSlug, req.IsDefault, req.StartAt, req.EndAt, termID); err != nil {
return err
return r.Patch("term", "id", termID, req)
}

func (r *Repository) GetEventTerms(eventSlug models.EventSlugInPath) ([]*models.Term, error) {
terms := []*models.Term{}
query := "SELECT term.* FROM term JOIN event ON term.event_slug = event.slug WHERE event.slug = ?"
if err := r.db.Select(&terms, query, eventSlug); err != nil {
return nil, err
}

return nil
return terms, nil
}

0 comments on commit f320a78

Please sign in to comment.