Skip to content

Commit

Permalink
feat: can now specify composition when creating deck
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed May 15, 2022
1 parent 1d25d14 commit c7ea205
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ const docTemplate = `{
"description": "list of codes",
"name": "codes",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"example": "french, uno",
"description": "composition of deck",
"name": "comp",
"in": "query"
}
],
"responses": {
Expand Down
10 changes: 10 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
"description": "list of codes",
"name": "codes",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"example": "french, uno",
"description": "composition of deck",
"name": "comp",
"in": "query"
}
],
"responses": {
Expand Down
7 changes: 7 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ paths:
type: string
name: codes
type: array
- description: composition of deck
example: french, uno
in: query
items:
type: string
name: comp
type: array
produces:
- application/json
responses:
Expand Down
20 changes: 17 additions & 3 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/mgjules/deckr/card"
"github.com/mgjules/deckr/composition"
"github.com/mgjules/deckr/deck"
"github.com/mgjules/deckr/docs"
"github.com/mgjules/deckr/repo/inmemory"
Expand Down Expand Up @@ -53,18 +55,30 @@ func (Server) handleSwagger() gin.HandlerFunc {
// @Description creates a new full or partial deck of cards given an optional list of codes
// @Tags deck
// @Produce json
// @Param codes query []string false "list of codes" example(AS, 2C, 3D, 4H, 5S)
// @Param codes query []string false "list of codes" example(AS, 2C, 3D, 4H, 5S)
// @Param comp query []string false "composition of deck" example(french, uno)
// @Success 201 {object} http.DeckClosed
// @Failure 400 {object} http.Error
// @Failure 500 {object} http.Error
// @Router /decks [post]
func (s *Server) handleCreateDeck() gin.HandlerFunc {
return func(c *gin.Context) {
cc := c.QueryArray("codes")
comp := c.Query("comp")
codes := c.QueryArray("codes")

d, err := deck.New(deck.WithCodes(cc...))
d, err := deck.New(deck.WithComposition(comp), deck.WithCodes(codes...))
if err != nil {
s.log.Errorf("new deck: %v", err)

if errors.Is(err, composition.ErrUnknownComposition) ||
errors.Is(err, card.ErrInvalidCode) ||
errors.Is(err, card.ErrInvalidRank) ||
errors.Is(err, card.ErrInvalidSuit) {
c.AbortWithStatusJSON(http.StatusBadRequest, Error{err.Error()})

return
}

c.AbortWithStatusJSON(http.StatusInternalServerError, Error{err.Error()})

return
Expand Down

0 comments on commit c7ea205

Please sign in to comment.