Skip to content

Commit

Permalink
feat: add deck shuffle endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed May 14, 2022
1 parent dd3f48e commit e2832bc
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,54 @@ const docTemplate = `{
}
}
},
"/decks/{id}/shuffle": {
"post": {
"description": "shuffle a deck of cards given an id",
"produces": [
"application/json"
],
"tags": [
"deck"
],
"summary": "shuffle a deck of cards",
"parameters": [
{
"type": "string",
"example": "9302b603-13bb-5275-a3b9-5fcefafa34e0",
"description": "id of deck",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/http.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/http.Error"
}
}
}
}
},
"/version": {
"get": {
"description": "checks the server's version",
Expand Down
48 changes: 48 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,54 @@
}
}
},
"/decks/{id}/shuffle": {
"post": {
"description": "shuffle a deck of cards given an id",
"produces": [
"application/json"
],
"tags": [
"deck"
],
"summary": "shuffle a deck of cards",
"parameters": [
{
"type": "string",
"example": "9302b603-13bb-5275-a3b9-5fcefafa34e0",
"description": "id of deck",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/http.Error"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/http.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/http.Error"
}
}
}
}
},
"/version": {
"get": {
"description": "checks the server's version",
Expand Down
32 changes: 32 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,38 @@ paths:
summary: draws cards from a deck of cards
tags:
- deck
/decks/{id}/shuffle:
post:
description: shuffle a deck of cards given an id
parameters:
- description: id of deck
example: 9302b603-13bb-5275-a3b9-5fcefafa34e0
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/http.Error'
"404":
description: Not Found
schema:
$ref: '#/definitions/http.Error'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/http.Error'
summary: shuffle a deck of cards
tags:
- deck
/version:
get:
description: checks the server's version
Expand Down
88 changes: 88 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,91 @@ func (s *Server) handleDrawCards() gin.HandlerFunc {
c.JSON(http.StatusOK, Cards{cards})
}
}

// handleShuffleDeck godoc
// @Summary shuffle a deck of cards
// @Description shuffle a deck of cards given an id
// @Tags deck
// @Produce json
// @Param id path string true "id of deck" example(9302b603-13bb-5275-a3b9-5fcefafa34e0)
// @Success 200 {string} deck shuffled
// @Failure 400 {object} http.Error
// @Failure 404 {object} http.Error
// @Failure 500 {object} http.Error
// @Router /decks/{id}/shuffle [post]
func (s *Server) handleShuffleDeck() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
if _, err := uuid.FromString(id); err != nil {
s.log.ErrorfContext(c, "parse id: %v", err)
c.AbortWithStatusJSON(http.StatusBadRequest, Error{"invalid or missing id"})

return
}

rd, err := s.repo.Get(c, id)
if err != nil {
s.log.ErrorfContext(c, "get deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{inmemory.ErrDeckNotFound.Error()})

return
}

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

return
}

var cc []card.Card
for _, rc := range rd.Cards {
rank := card.NewRank(rc.Rank.Name, rc.Rank.Code)
suit := card.NewSuit(rc.Suit.Name, rc.Suit.Code)

var code *card.Code
code, err = card.NewCode(rc.Code)
if err != nil {
s.log.ErrorfContext(c, "new code: %v", err)

continue
}

cc = append(cc, *card.NewCard(rank, suit, *code))
}

d, err := deck.New(
deck.WithID(rd.ID),
deck.WithShuffled(rd.Shuffled),
deck.WithCards(cc...),
)
if err != nil {
s.log.ErrorfContext(c, "new deck: %v", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, Error{err.Error()})

return
}

d.Shuffle()

rd.Shuffled = d.IsShuffled()

rd.Cards = []repo.Card{}
for _, c := range d.Cards() {
rd.Cards = append(rd.Cards, repo.Card{
Rank: repo.Rank{Name: c.Rank().String(), Code: c.Rank().Code()},
Suit: repo.Suit{Name: c.Suit().String(), Code: c.Suit().Code()},
Code: c.Code().String(),
})
}

if err := s.repo.Save(c, rd); err != nil {
s.log.ErrorfContext(c, "save deck: %v", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, Error{err.Error()})

return
}

c.JSON(http.StatusOK, Success{"deck shuffled"})
}
}
1 change: 1 addition & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *Server) registerRoutes() {
deck.POST("", s.handleCreateDeck())
deck.GET("/:id", s.handleOpenDeck())
deck.GET("/:id/draw", s.handleDrawCards())
deck.POST("/:id/shuffle", s.handleShuffleDeck())
}
}

Expand Down

0 comments on commit e2832bc

Please sign in to comment.