Skip to content

Commit

Permalink
feat: add character type, refactor http translations, fetch additiona…
Browse files Browse the repository at this point in the history
…l data
  • Loading branch information
SomethingSexy committed Oct 11, 2024
1 parent 3874abd commit 02e003f
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"github.com/google/uuid"
)

func NewCharacterRequest(c domain.Character) CharacterRequest {
return CharacterRequest{
ID: c.CharacterId.String(),
CharacterId: c.CharacterId.String(),
Name: c.Name,
Description: c.Description,
}
}

type CharacterRequest struct {
ID string `jsonapi:"primary,characters"`
CharacterId string `jsonapi:"attr,characterId"`
Expand Down
16 changes: 16 additions & 0 deletions internal/chronicle/adapter/http/game/game_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
"github.com/google/uuid"
)

func NewGameRequest(g domain.Game) GameRequest {
worlds := make([]*WorldRequest, len(g.Worlds))
for x, world := range g.Worlds {
worldRquest := NewWorldRequest(world)
worlds[x] = &worldRquest
}

return GameRequest{
ID: g.GameId.String(),
GameId: g.GameId.String(),
Name: g.Name,
Type: g.Type,
Worlds: worlds,
}
}

type GameRequest struct {
ID string `jsonapi:"primary,games"`
GameId string `jsonapi:"attr,gameId"`
Expand Down
16 changes: 16 additions & 0 deletions internal/chronicle/adapter/http/game/location_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
"github.com/google/uuid"
)

func NewLocationRequest(l domain.Location) LocationRequest {
paths := make([]string, len(l.Path))
for x, path := range l.Path {
paths[x] = path.String()
}

return LocationRequest{
ID: l.LocationId.String(),
LocationId: l.LocationId.String(),
WorldId: l.WorldId.String(),
Name: l.Name,
Type: l.Type,
Path: paths,
}
}

type LocationRequest struct {
ID string `jsonapi:"primary,locations"`
LocationId string `jsonapi:"attr,locationId"`
Expand Down
83 changes: 8 additions & 75 deletions internal/chronicle/adapter/http/game/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package game
import (
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/http/character"
corePort "github.com/SomethingSexy/chronicle/internal/chronicle/core/port"
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
"github.com/SomethingSexy/chronicle/internal/common"
Expand Down Expand Up @@ -76,23 +75,8 @@ func (h GameHttpServer) ListGames(w http.ResponseWriter, r *http.Request) {

responses := make([]*GameRequest, len(games))
for i, game := range games {
worlds := make([]*WorldRequest, len(game.Worlds))

for x, world := range game.Worlds {
worlds[x] = &WorldRequest{
ID: world.WorldId.String(),
WorldId: world.WorldId.String(),
GameId: world.GameId.String(),
Name: world.Name,
}
}
responses[i] = &GameRequest{
ID: game.GameId.String(),
GameId: game.GameId.String(),
Name: game.Name,
Type: game.Type,
Worlds: worlds,
}
gameRequest := NewGameRequest((game))
responses[i] = &gameRequest
}

w.WriteHeader(http.StatusOK)
Expand All @@ -115,16 +99,11 @@ func (h GameHttpServer) GetGame(w http.ResponseWriter, r *http.Request) {
return
}

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

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
if err := jsonapi.MarshalPayload(w, response); err != nil {
if err := jsonapi.MarshalPayload(w, &gameRequest); err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand Down Expand Up @@ -161,45 +140,11 @@ func (h GameHttpServer) GetWorld(w http.ResponseWriter, r *http.Request) {
return
}

locations := make([]*LocationRequest, len(world.Locations))
for i, location := range world.Locations {
paths := make([]string, len(location.Path))
for x, path := range location.Path {
paths[x] = path.String()
}

locations[i] = &LocationRequest{
ID: location.LocationId.String(),
LocationId: location.LocationId.String(),
WorldId: location.WorldId.String(),
Name: location.Name,
Type: location.Type,
Path: paths,
}
}

characters := make([]*character.CharacterRequest, len(world.Characters))
for i, c := range world.Characters {
characters[i] = &character.CharacterRequest{
ID: c.CharacterId.String(),
CharacterId: c.CharacterId.String(),
Name: c.Name,
Description: c.Description,
}
}

response := &WorldRequest{
ID: world.WorldId.String(),
WorldId: world.WorldId.String(),
GameId: world.GameId.String(),
Name: world.Name,
Locations: locations,
Characters: characters,
}
worldRquest := NewWorldRequest(world)

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
if err := jsonapi.MarshalPayload(w, response); err != nil {
if err := jsonapi.MarshalPayload(w, &worldRquest); err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand Down Expand Up @@ -237,21 +182,9 @@ func (h GameHttpServer) GetLocations(w http.ResponseWriter, r *http.Request) {
}

responses := make([]*LocationRequest, len(locations))

for i, location := range locations {
paths := make([]string, len(location.Path))
for x, path := range location.Path {
paths[x] = path.String()
}

responses[i] = &LocationRequest{
ID: location.LocationId.String(),
LocationId: location.LocationId.String(),
WorldId: location.WorldId.String(),
Name: location.Name,
Type: location.Type,
Path: paths,
}
locationRequest := NewLocationRequest(location)
responses[i] = &locationRequest
}

w.WriteHeader(http.StatusOK)
Expand Down
23 changes: 23 additions & 0 deletions internal/chronicle/adapter/http/game/world_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import (
"github.com/google/uuid"
)

func NewWorldRequest(w domain.World) WorldRequest {
locations := make([]*LocationRequest, len(w.Locations))
for i, location := range w.Locations {
locationRequest := NewLocationRequest(location)
locations[i] = &locationRequest
}

characters := make([]*character.CharacterRequest, len(w.Characters))
for i, c := range w.Characters {
characterRequest := character.NewCharacterRequest(c)
characters[i] = &characterRequest
}

return WorldRequest{
ID: w.WorldId.String(),
WorldId: w.WorldId.String(),
GameId: w.GameId.String(),
Name: w.Name,
Locations: locations,
Characters: characters,
}
}

type WorldRequest struct {
ID string `jsonapi:"primary,worlds"`
WorldId string `jsonapi:"attr,worldId"`
Expand Down
19 changes: 14 additions & 5 deletions internal/chronicle/adapter/persistence/postgres/query/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ func (g WorldQuery) ListLocations(ctx context.Context, gameId uuid.UUID, worldId
return locations, nil
}

func (g WorldQuery) AddCharacterToGameWorld(ctx context.Context, worldId uuid.UUID, characterId uuid.UUID) error {
func (g WorldQuery) UpsertCharacterToGameWorld(ctx context.Context, worldId uuid.UUID, characterId uuid.UUID, character *domain.WorldCharacter) error {
world, err := g.Queries.GetWorldFromUuid(ctx, worldId)
if err != nil {
return err
}

character, err := g.Queries.GetCharacterFromUuid(ctx, characterId)
existingCharacter, err := g.Queries.GetCharacterFromUuid(ctx, characterId)
if err != nil {
return err
}
Expand All @@ -164,15 +164,24 @@ func (g WorldQuery) AddCharacterToGameWorld(ctx context.Context, worldId uuid.UU
Valid: true,
}

return g.Queries.AddCharacterToGameWorld(ctx, repository.AddCharacterToGameWorldParams{
requestArgs := repository.UpsertCharacterToGameWorldParams{
// Just generating this now for future use but since we are using a unique
// index for worldId and characterId, this is probably too important
WorldCharacterID: uuid.New(),
WorldID: world.ID,
CharacterID: character.ID,
CharacterID: existingCharacter.ID,
UpdatedAt: ts,
CreatedAt: ts,
})
}

if character != nil {
requestArgs.CharacterType = pgtype.Text{
Valid: true,
String: character.Type.String(),
}
}

return g.Queries.UpsertCharacterToGameWorld(ctx, requestArgs)
}

func (g WorldQuery) ListCharacters(ctx context.Context, gameId uuid.UUID, worldId uuid.UUID) ([]domain.Character, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ CREATE TABLE "public"."world_character" (
"world_character_id" uuid NOT NULL,
"character_id" bigserial NOT NULL,
"world_id" bigserial NOT NULL,
"character_type" text NULL,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY ("id"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1:WV1bqxlgwnWG5/Vps8ZW+YtymgqUc5m/cgcjW6MNXPQ=
20241007183159_initial.sql h1:BZjBs/hNodmJ6DTudwr7agmc5fqwtRnYdxwnOr3CEuE=
h1:Co8gnvkgqPsXbjuDhROpfKEbZiOuDkysI8ubk167I18=
20241011174940_initial.sql h1:AauSR5nSnNnn1pcPjAlq/M/bjDS5lESaNHBJkVJ+v4A=
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ WHERE id = $1 LIMIT 1;
SELECT * FROM character
WHERE character.character_id = $1 LIMIT 1;

-- name: AddCharacterToGameWorld :exec
-- name: UpsertCharacterToGameWorld :exec
INSERT INTO world_character (
world_character_id, world_id, character_id, created_at, updated_at
world_character_id, world_id, character_id, character_type, created_at, updated_at
) VALUES (
$1, $2, $3, $4, $5
$1, $2, $3, $4, $5, $6
)
ON CONFLICT (world_id, character_id) DO UPDATE SET
character_type = EXCLUDED.character_type,
updated_at = EXCLUDED.updated_at;

-- name: GetWorldCharacters :many
Expand Down

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

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

Loading

0 comments on commit 02e003f

Please sign in to comment.