Skip to content

Commit

Permalink
feat: add query to retrieve game
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Aug 20, 2024
1 parent f2116b4 commit 8aeb2c3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
38 changes: 31 additions & 7 deletions internal/chronicle/adapter/http/game/game_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/jsonapi"
"github.com/google/uuid"
)

func NewGameHttpServer(commands port.ChronicleCommands, queries port.GameQueries) GameHttpServer {
Expand All @@ -27,6 +28,7 @@ func (h GameHttpServer) Routes() chi.Router {
r := chi.NewRouter()
r.Post("/", h.CreateGame)
r.Get("/", h.ListGames)
r.Get("/{gameId}", h.GetGame)
return r
}

Expand All @@ -45,7 +47,6 @@ func (h GameHttpServer) CreateGame(w http.ResponseWriter, r *http.Request) {
}

render.Status(r, http.StatusCreated)
// render.Render(w, r, NewGameResponse(article))
}

func (h GameHttpServer) ListGames(w http.ResponseWriter, r *http.Request) {
Expand All @@ -55,11 +56,7 @@ func (h GameHttpServer) ListGames(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)

responses := make([]*GameRequest, len(games))

for i, game := range games {
responses[i] = &GameRequest{
ID: game.GameId.String(),
Expand All @@ -69,10 +66,37 @@ func (h GameHttpServer) ListGames(w http.ResponseWriter, r *http.Request) {
}
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
if err := jsonapi.MarshalPayload(w, responses); err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
// render.Status(r, http.StatusCreated)
// render.Render(w, r, NewGameResponse(article))
}

func (h GameHttpServer) GetGame(w http.ResponseWriter, r *http.Request) {
gameId := chi.URLParam(r, "gameId")

game, err := h.queries.GetGame.Handle(r.Context(), corePort.GetGameQuery{
// TODO: BAD check for error
GameId: uuid.MustParse(gameId),
})
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}

response := &GameRequest{
ID: game.GameId.String(),
GameId: game.GameId.String(),
Name: game.Name,
Type: game.Type,
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
if err := jsonapi.MarshalPayload(w, response); err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
}
16 changes: 16 additions & 0 deletions internal/chronicle/adapter/persistence/postgres/query/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/persistence/postgres/sqlc/repository"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/google/uuid"
)

func NewGameQuery(queries *repository.Queries) GameQuery {
Expand Down Expand Up @@ -60,3 +61,18 @@ func (g GameQuery) ListGames(ctx context.Context) ([]domain.Game, error) {

return games, nil
}

func (g GameQuery) GetGame(ctx context.Context, id uuid.UUID) (domain.Game, error) {
response, err := g.Queries.GetGameFromUuid(ctx, id)
if err != nil {
return domain.Game{}, err
}

game := domain.Game{
Name: response.Name,
Type: response.Type,
GameId: response.GameID,
}

return game, nil
}
1 change: 1 addition & 0 deletions internal/chronicle/core/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewApplication(persistence port.ChronicleQueries) port.ChronicleApplication

queries := port.GameQueries{
ListGames: query.NewListGamesHandler(persistence),
GetGame: query.NewGetGameHandler(persistence),
}

return port.ChronicleApplication{
Expand Down
23 changes: 23 additions & 0 deletions internal/chronicle/core/application/query/get_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package query

import (
"context"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
gamePort "github.com/SomethingSexy/chronicle/internal/chronicle/core/port"
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
)

func NewGetGameHandler(persistence port.ChronicleQueries) gamePort.GetGameHandler {
return getGameHandler{
Persistence: persistence,
}
}

type getGameHandler struct {
Persistence port.ChronicleQueries
}

func (h getGameHandler) Handle(ctx context.Context, q gamePort.GetGameQuery) (domain.Game, error) {
return h.Persistence.GetGame(ctx, q.GameId)
}
6 changes: 6 additions & 0 deletions internal/chronicle/core/port/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package port
import (
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/SomethingSexy/chronicle/internal/common"
"github.com/google/uuid"
)

type CreateGame struct {
Expand All @@ -14,3 +15,8 @@ type CreateGameHander common.CommandHandler[CreateGame]
type AllGamesQuery struct {
}
type ListGamesHandler common.QueryHandler[AllGamesQuery, []domain.Game]

type GetGameQuery struct {
GameId uuid.UUID
}
type GetGameHandler common.QueryHandler[GetGameQuery, domain.Game]
2 changes: 2 additions & 0 deletions internal/chronicle/port/chronicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/google/uuid"
)

// TODO: Probably a better name for this
type ChronicleQueries interface {
CreateGame(ctx context.Context, game domain.Game) (domain.Game, error)
ListGames(ctx context.Context) ([]domain.Game, error)
GetGame(ctx context.Context, id uuid.UUID) (domain.Game, error)
}
1 change: 1 addition & 0 deletions internal/chronicle/port/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ type ChronicleCommands struct {

type GameQueries struct {
ListGames corePort.ListGamesHandler
GetGame corePort.GetGameHandler
}

0 comments on commit 8aeb2c3

Please sign in to comment.