This repository contains a REST API written in Golang that simulates a deck of cards.
Details of the Product requirement/PRD can be found here
-
The API is built using the Gin router and follows the Clean Architecture principles, as described by Uncle Bob. This ensures that the code is modular and easy to maintain.
-
The dependencies are organized using a controller-service-dao structure, where the dependencies are concentric. The controller layer depends on the service layer, which in turn depends on the dao layer.
All dependencies are injected using the Google Wire library at the controller level. -
The package structure follows a similar pattern, where the service, model, and dao layers are separated into their own package folders. Any client, such as a controller or worker, can depend on the implementations provided by these packages.
-
Instead of using a database, the API utilizes an in-memory map that is created and injected into the DAO layer during server creation and startup.
To get started with this API, you will need to have Golang installed on your machine. You can then clone this repository and run the following command from your termainal by navigating to entry directory:
# cd to entry directory to run the project cd entry go mod tidy #this will install all the depencies go run main.go #This will start the API on http://localhost:3000/.
To create a new full deck, send a POST request to /decks. The response will contain a JSON object with the id of the new deck: To create a partial deck, send a POST request to /decks?cards=(card_code).
#Request: POST http://localhost:3000/decks?cards=(card_code) { shuffle :(true/false) } #Response: { "deck_id": "a251071b-662f-44b6-ba11-e24863039c59", "shuffled": false, "remaining": 52 }
To draw cards from an existing deck, send a GET request to /deck/:id/draw?count=, where :id is the ID of the deck you want to draw from and count is the count of cards that you want to draw. The response will contain a JSON object with the drawn cards:
#Request: GET http://localhost:3000/decks/a251071b-662f-44b6-ba11-e24863039c59/draw?count=2 #Response: { "cards": [ { "value": "QUEEN", "suit": "HEARTS", "code": "QH" }, { "value": "4", "suit": "DIAMONDS", "code": "4D" } ] }
To open an existing deck, send a GET request to /deck/:id, where :id is the ID of the deck you want to open. The response will contain a JSON object with the details of the deck:
#Request: GET http://localhost:3000/decks/a251071b-662f-44b6-ba11-e24863039c59 #Response: { "deck_id": "a251071b-662f-44b6-ba11-e24863039c59", "shuffled": false, "remaining": 3, "cards": [ { "value": "ACE", "suit": "SPADES", "code": "AS" }, { "value": "KING", "suit": "HEARTS", "code": "KH" }, { "value": "8", "suit": "CLUBS", "code": "8C" } ] }
Unit tests for the service layer can be found in the "service" directory. To run these tests, navigate to the "pkg" folder in the terminal and run the following command:
go test -v ./...
It should be noted that even though the tests are written in the service layer, the DAO layer is mocked for all test cases, making them unit tests.