Skip to content

Commit

Permalink
feat: work on listing games, start world and location
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Aug 20, 2024
1 parent a1b0e70 commit f2116b4
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ atlas migrate apply \
--dir "file://migrations" \
--baseline "20240731025606"
```

Using the free version of Atlas, we need to manually add extensions.

Right now we are adding them to the root migration (probably should make this a separate first migration).
1 change: 1 addition & 0 deletions internal/chronicle/adapter/http/game/game_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

type GameRequest struct {
ID string `jsonapi:"primary,games"`
GameId string `jsonapi:"attr,gameId"`
Name string `jsonapi:"attr,name"`
Type string `jsonapi:"attr,type"`
Expand Down
31 changes: 31 additions & 0 deletions internal/chronicle/adapter/http/game/game_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/SomethingSexy/chronicle/internal/common"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/jsonapi"
)

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

Expand All @@ -45,3 +47,32 @@ 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) {
games, err := h.queries.ListGames.Handle(r.Context(), corePort.AllGamesQuery{})
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
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(),
GameId: game.GameId.String(),
Name: game.Name,
Type: game.Type,
}
}

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))
}
2 changes: 1 addition & 1 deletion internal/chronicle/adapter/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h HttpServer) Start() {
render.Decode = DefaultDecoder

// TODO: Given the application, this should mount all of the route handlers
r.Mount("/game", h.app.Routes()[0])
r.Mount("/games", h.app.Routes()[0])

http.ListenAndServe(":3000", r)
}
Expand Down
37 changes: 28 additions & 9 deletions internal/chronicle/adapter/persistence/postgres/query/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/persistence/postgres/sqlc/repository"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/jackc/pgx/v5/pgtype"
)

func NewGameQuery(queries *repository.Queries) GameQuery {
Expand All @@ -19,16 +18,12 @@ type GameQuery struct {
Queries *repository.Queries
}

// Create a game
func (g GameQuery) CreateGame(ctx context.Context, game domain.Game) (domain.Game, error) {
// s := fmt.Sprintf("%x-%x-%x-%x-%x", myUUID.Bytes[0:4], myUUID.Bytes[4:6], myUUID.Bytes[6:8], myUUID.Bytes[8:10], myUUID.Bytes[10:16])

args := repository.CreateGameParams{
GameID: pgtype.UUID{
Bytes: game.GameId,
Valid: true,
},
Name: game.Name,
Type: game.Type,
GameID: game.GameId,
Name: game.Name,
Type: game.Type,
}

response, err := g.Queries.CreateGame(ctx, args)
Expand All @@ -41,3 +36,27 @@ func (g GameQuery) CreateGame(ctx context.Context, game domain.Game) (domain.Gam
Type: response.Type,
}, nil
}

// List all available games.
// TODO:
// - Limit games by user who created them
// - Limit games if they are marked private
// - Allow admin to see all games
func (g GameQuery) ListGames(ctx context.Context) ([]domain.Game, error) {
response, err := g.Queries.ListGames(ctx)
if err != nil {
return nil, err
}

games := make([]domain.Game, len(response))

for i, game := range response {
games[i] = domain.Game{
Name: game.Name,
Type: game.Type,
GameId: game.GameID,
}
}

return games, nil
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE EXTENSION "ltree";

-- Create "game" table
CREATE TABLE "public"."game" (
"id" bigserial NOT NULL,
Expand All @@ -12,10 +14,13 @@ CREATE TABLE "public"."world" (
"id" bigserial NOT NULL,
"world_id" uuid NOT NULL,
"game_id" bigserial NOT NULL,
"name" text NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "world_world_id_key" UNIQUE ("world_id"),
CONSTRAINT "world_game_id_fkey" FOREIGN KEY ("game_id") REFERENCES "public"."game" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create index "world_game_id" to table: "world"
CREATE INDEX "world_game_id" ON "public"."world" ("game_id");
-- Create "location" table
CREATE TABLE "public"."location" (
"id" bigserial NOT NULL,
Expand All @@ -30,3 +35,5 @@ CREATE TABLE "public"."location" (
);
-- Create index "location_path_idx" to table: "location"
CREATE INDEX "location_path_idx" ON "public"."location" USING gist ("path");
-- Create index "location_world_id" to table: "location"
CREATE INDEX "location_world_id" ON "public"."location" ("world_id");
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1:92SiNSQUSUJ2MaDX0pAvt0LDqO9sw13Fl2hVm2Nkjck=
20240818215306_initial.sql h1:rs/Idnz34OnOmdLKzVOUs15THtfcW+3H5auFWBqVc+s=
h1:e7Whf5lPTyJ5gZaclj0NLm1BA25BpU/S/uuWiNUeGFs=
20240820200446_initial.sql h1:FINnqTjzKlGS/n7twb3x1BGaGGqC/f/ztU7N/RNOQcU=
41 changes: 38 additions & 3 deletions internal/chronicle/adapter/persistence/postgres/sqlc/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
SELECT * FROM game
WHERE id = $1 LIMIT 1;

-- name: GetGameFromUuid :one
SELECT * FROM game
WHERE game_id = $1 LIMIT 1;

-- name: ListGames :many
SELECT * FROM game
ORDER BY name;
Expand All @@ -12,7 +16,7 @@ INSERT INTO game (
) VALUES (
$1, $2, $3
)
ON CONFLICT (game_id) DO UPDATE SET
ON CONFLICT (game_id) DO UPDATE SET
name = EXCLUDED.name,
type = EXCLUDED.type
RETURNING *;
Expand All @@ -21,8 +25,39 @@ RETURNING *;
UPDATE game
set name = $2,
type = $3
WHERE id = $1;
WHERE game_id = $1;

-- name: DeleteGame :exec
DELETE FROM game
WHERE id = $1;
WHERE game_id = $1;

-- name: GetWorld :one
SELECT * FROM world
WHERE id = $1 LIMIT 1;

-- name: GetWorldFromUuid :one
SELECT * FROM world
WHERE world_id = $1 LIMIT 1;

-- name: ListWorlds :many
SELECT * FROM world
ORDER BY name;

-- name: CreateWorld :one
INSERT INTO world (
world_id, game_id, name
) VALUES (
$1, $2, $3
)
ON CONFLICT (world_id) DO UPDATE SET
name = EXCLUDED.name
RETURNING *;

-- name: UpdateWorld :exec
UPDATE world
set name = $2
WHERE world_id = $1;

-- name: DeleteWorld :exec
DELETE FROM world
WHERE world_id = $1;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f2116b4

Please sign in to comment.