Skip to content

Commit

Permalink
feat: update how models are being bound from payload body, prototypin…
Browse files Browse the repository at this point in the history
…g game characters
  • Loading branch information
SomethingSexy committed Oct 17, 2024
1 parent a4a01b0 commit 93b73ff
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 65 deletions.
15 changes: 11 additions & 4 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/test-go/testify v1.1.4/go.mod h1:rH7cfJo/47vWGdi4GPj16x3/t1xGOj2YxzmNQzk2ghU=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 12 additions & 1 deletion internal/chronicle/adapter/http/character/character_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package character

import (
"errors"
"io"
"net/http"

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

func NewCharacterRequest(c domain.Character) CharacterRequest {
func NewCharacterResponse(c domain.Character) CharacterRequest {
return CharacterRequest{
ID: c.CharacterId.String(),
CharacterId: c.CharacterId.String(),
Expand All @@ -17,6 +19,15 @@ func NewCharacterRequest(c domain.Character) CharacterRequest {
}
}

func NewCharacterRequest(body io.ReadCloser) (CharacterRequest, error) {
var model CharacterRequest
if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
return model, err
}

return model, nil
}

type CharacterRequest struct {
ID string `jsonapi:"primary,characters"`
CharacterId string `jsonapi:"attr,characterId"`
Expand Down
4 changes: 2 additions & 2 deletions internal/chronicle/adapter/http/character/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (h CharacterHttpServer) Routes() chi.Router {
}

func (h CharacterHttpServer) CreateCharacter(w http.ResponseWriter, r *http.Request) {
data := &CharacterRequest{}
if err := render.Bind(r, data); err != nil {
data, err := NewCharacterRequest(r.Body)
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package game

type GameCharacterRequest struct {
ID string `jsonapi:"primary,game-characters"`
GameId string `jsonapi:"attr,gameId"`
WorldId string `jsonapi:"attr,worldId"`
Name string `jsonapi:"attr,name"`
Type string `jsonapi:"attr,type"`
}
17 changes: 15 additions & 2 deletions internal/chronicle/adapter/http/game/game_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package game

import (
"errors"
"io"
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/http/world"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/google/jsonapi"
"github.com/google/uuid"
)

func NewGameRequest(g domain.Game) GameRequest {
worldRquest := world.NewWorldRequest(g.World)
// Given a domain, return a GameRequest that will be used
// as a response payload.
func NewGameResponse(g domain.Game) GameRequest {
worldRquest := world.NewWorldResponse(g.World)

return GameRequest{
ID: g.GameId.String(),
Expand All @@ -22,6 +26,15 @@ func NewGameRequest(g domain.Game) GameRequest {
}
}

func NewGameRequest(body io.ReadCloser) (GameRequest, error) {
var model GameRequest
if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
return model, err
}

return model, nil
}

type GameRequest struct {
ID string `jsonapi:"primary,games"`
GameId string `jsonapi:"attr,gameId"`
Expand Down
10 changes: 4 additions & 6 deletions internal/chronicle/adapter/http/game/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package game

import (
"log"
"net/http"

corePort "github.com/SomethingSexy/chronicle/internal/chronicle/core/port"
Expand Down Expand Up @@ -35,8 +34,8 @@ func (h GameHttpServer) Routes() chi.Router {
}

func (h GameHttpServer) CreateGame(w http.ResponseWriter, r *http.Request) {
data := &GameRequest{}
if err := render.Bind(r, data); err != nil {
data, err := NewGameRequest(r.Body)
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand All @@ -60,7 +59,7 @@ func (h GameHttpServer) ListGames(w http.ResponseWriter, r *http.Request) {

responses := make([]*GameRequest, len(games))
for i, game := range games {
gameRequest := NewGameRequest((game))
gameRequest := NewGameResponse((game))
responses[i] = &gameRequest
}

Expand All @@ -84,8 +83,7 @@ func (h GameHttpServer) GetGame(w http.ResponseWriter, r *http.Request) {
return
}

log.Println(game)
gameRequest := NewGameRequest(game)
gameRequest := NewGameResponse(game)

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
Expand Down
13 changes: 12 additions & 1 deletion internal/chronicle/adapter/http/world/location_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package world

import (
"errors"
"io"
"net/http"

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

func NewLocationRequest(l domain.Location) LocationRequest {
func NewLocationResponse(l domain.Location) LocationRequest {
paths := make([]string, len(l.Path))
for x, path := range l.Path {
paths[x] = path.String()
Expand All @@ -24,6 +26,15 @@ func NewLocationRequest(l domain.Location) LocationRequest {
}
}

func NewLocationRequest(body io.ReadCloser) (LocationRequest, error) {
var model LocationRequest
if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
return model, err
}

return model, nil
}

type LocationRequest struct {
ID string `jsonapi:"primary,locations"`
LocationId string `jsonapi:"attr,locationId"`
Expand Down
23 changes: 10 additions & 13 deletions internal/chronicle/adapter/http/world/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ type GameHttpServer struct {
func (h GameHttpServer) Routes() chi.Router {
r := chi.NewRouter()

// TODO: This is obviously going to get huge
// we need to decide how to organize these route handlers
// Either at this level or a higher level to apply
// relationships to root routes.
r.Post("/", h.CreateWorld)
r.Get("/{worldId}", h.GetWorld)

Expand All @@ -41,14 +37,15 @@ func (h GameHttpServer) Routes() chi.Router {
r.Post("/{worldId}/relationships/characters", h.AddWorldCharacter)
// Using this format right now for patching a character that has been linked to a world
// This might not be correct and instead it could include "world-characters"
r.Patch("/{worldId}/characters/{characterId}", h.AddWorldCharacter)
// TODO: I don't think we need this anymore
// r.Patch("/{worldId}/characters/{characterId}", h.AddWorldCharacter)

return r
}

func (h GameHttpServer) CreateWorld(w http.ResponseWriter, r *http.Request) {
data := &WorldRequest{}
if err := render.Bind(r, data); err != nil {
data, err := NewWorldRequest(r.Body)
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand All @@ -74,7 +71,7 @@ func (h GameHttpServer) GetWorld(w http.ResponseWriter, r *http.Request) {
return
}

worldRquest := NewWorldRequest(world)
worldRquest := NewWorldResponse(world)

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
Expand All @@ -85,8 +82,8 @@ func (h GameHttpServer) GetWorld(w http.ResponseWriter, r *http.Request) {
}

func (h GameHttpServer) CreateLocation(w http.ResponseWriter, r *http.Request) {
data := &LocationRequest{}
if err := render.Bind(r, data); err != nil {
data, err := NewLocationRequest(r.Body)
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand Down Expand Up @@ -114,7 +111,7 @@ func (h GameHttpServer) GetLocations(w http.ResponseWriter, r *http.Request) {

responses := make([]*LocationRequest, len(locations))
for i, location := range locations {
locationRequest := NewLocationRequest(location)
locationRequest := NewLocationResponse(location)
responses[i] = &locationRequest
}

Expand All @@ -128,8 +125,8 @@ func (h GameHttpServer) GetLocations(w http.ResponseWriter, r *http.Request) {

func (h GameHttpServer) AddWorldCharacter(w http.ResponseWriter, r *http.Request) {
worldId := chi.URLParam(r, "worldId")
data := &AddWorldCharacterRequest{}
if err := render.Bind(r, data); err != nil {
data, err := NewAddWorldCharacterRequest(r.Body)
if err != nil {
render.Render(w, r, common.ErrInvalidRequest(err))
return
}
Expand Down
51 changes: 35 additions & 16 deletions internal/chronicle/adapter/http/world/world_character_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package world

import (
"errors"
"io"
"net/http"

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

func NewAddWorldCharacterRequest(body io.ReadCloser) (AddWorldCharacterRequest, error) {
var model AddWorldCharacterRequest
if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
return model, err
}

return model, nil
}

// Rquest typed used for add a character to a world via a relationship
type AddWorldCharacterRequest struct {
ID string `jsonapi:"primary,characters"`
Expand All @@ -25,23 +35,32 @@ func (a *AddWorldCharacterRequest) ToDomain() uuid.UUID {
return uuid.MustParse(a.ID)
}

// func NewWorldCharacterRequest(body io.ReadCloser) (WorldCharacterRequest, error) {
// var model WorldCharacterRequest
// if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
// return model, err
// }

// return model, nil
// }

// Request type used for updating the attributes for a
// character that has already been added to a world
type WorldCharacterRequest struct {
ID string `jsonapi:"primary,world-characters"`
Type string `jsonapi:"attr,type"`
}
// type WorldCharacterRequest struct {
// ID string `jsonapi:"primary,world-characters"`
// Type string `jsonapi:"attr,type"`
// }

func (a *WorldCharacterRequest) Bind(r *http.Request) error {
if _, err := uuid.Parse(a.ID); err != nil {
return errors.New("id must be valid UUID")
}
// func (a *WorldCharacterRequest) Bind(r *http.Request) error {
// if _, err := uuid.Parse(a.ID); err != nil {
// return errors.New("id must be valid UUID")
// }

return nil
}
// return nil
// }

func (a *WorldCharacterRequest) ToDomain() domain.WorldCharacter {
return domain.WorldCharacter{
Type: domain.NewCharacterType(a.Type),
}
}
// func (a *WorldCharacterRequest) ToDomain() domain.WorldCharacter {
// return domain.WorldCharacter{
// Type: domain.NewCharacterType(a.Type),
// }
// }
17 changes: 14 additions & 3 deletions internal/chronicle/adapter/http/world/world_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package world

import (
"errors"
"io"
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/http/character"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/google/jsonapi"
"github.com/google/uuid"
)

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

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

Expand All @@ -31,6 +33,15 @@ func NewWorldRequest(w domain.World) WorldRequest {
}
}

func NewWorldRequest(body io.ReadCloser) (WorldRequest, error) {
var model WorldRequest
if err := jsonapi.UnmarshalPayload(body, &model); err != nil {
return model, err
}

return model, nil
}

type WorldRequest struct {
ID string `jsonapi:"primary,worlds"`
WorldId string `jsonapi:"attr,worldId"`
Expand Down
Loading

0 comments on commit 93b73ff

Please sign in to comment.