Skip to content

Commit

Permalink
feat: working with json api, still very early wip
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Jul 23, 2024
1 parent 1b086bd commit c8faedb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
21 changes: 21 additions & 0 deletions internal/chronicle/adapter/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
"github.com/google/jsonapi"
)

var ContentTypeJsonApi = "application/vnd.api+json"

type HttpServer struct {
app port.Service
}
Expand All @@ -23,8 +27,25 @@ func (h HttpServer) Start() {
r := chi.NewRouter()
r.Use(middleware.Logger)

render.Decode = DefaultDecoder

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

http.ListenAndServe(":3000", r)
}

func DefaultDecoder(r *http.Request, v interface{}) error {
var err error

switch r.Header.Get("Content-Type") {
case ContentTypeJsonApi:
if err := jsonapi.UnmarshalPayload(r.Body, v); err != nil {
return err
}
default:
err = render.DefaultDecoder(r, v)
}

return err
}
14 changes: 11 additions & 3 deletions internal/chronicle/core/game/adapter/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,20 @@ func ErrRender(err error) render.Renderer {
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}

type GameRequest struct {
*domain.Game
// TODO: This forces it to be { data: { attributes: { game: { name } }}}
// We probably don't want this and if we don't want jsonapi logic in core
// model, then we have to adapt two types here
*domain.Game `jsonapi:"attr,game"`
// ID int `jsonapi:"primary,blogs"`
// Title string `jsonapi:"attr,title"`
// Posts []*Post `jsonapi:"relation,posts"`
// CurrentPost *Post `jsonapi:"relation,current_post"`
// CurrentPostID int `jsonapi:"attr,current_post_id"`
// CreatedAt time.Time `jsonapi:"attr,created_at"`
// ViewCount int `jsonapi:"attr,view_count"`
}

func (a *GameRequest) Bind(r *http.Request) error {
// a.Article is nil if no Article fields are sent in the request. Return an
// error to avoid a nil pointer dereference.
if a.Game == nil {
return errors.New("missing required game fields")
}
Expand Down
1 change: 1 addition & 0 deletions internal/chronicle/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/ajg/form v1.5.1 // indirect
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/go-chi/render v1.0.3 // indirect
github.com/google/jsonapi v1.0.0 // indirect
)
2 changes: 2 additions & 0 deletions internal/chronicle/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/google/jsonapi v1.0.0 h1:qIGgO5Smu3yJmSs+QlvhQnrscdZfFhiV6S8ryJAglqU=
github.com/google/jsonapi v1.0.0/go.mod h1:YYHiRPJT8ARXGER8In9VuLv4qvLfDmA9ULQqptbLE4s=
20 changes: 9 additions & 11 deletions internal/chronicle/tools/http/create_game.http
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# @name create
POST http://localhost:3000/game
Content-Type: application/json
Content-Type: application/vnd.api+json

{
"name": "{{$guid}}"
"data": {
"type": "game",
"attributes": {
"game": {
"name": "{{$guid}}"
}
}
}
}

# {
# "data": {
# "type": "monitors",
# "attributes": {
# "name": "{{$guid}}"
# }
# }
# }

###

0 comments on commit c8faedb

Please sign in to comment.