The aim of this project is to create REST API that can simulate a deck of cards.
- Create
.env
by copying values from.env.example
- Edit the values inside
.env
to reflect to your environment - Build using
make
orgo
make build
or
go build -o main
- Run the app (make sure you already have the required database & tables)
./main
- Create a database in PostgreSQL
CREATE DATABASE deckofcards;
- Create table for decks data
CREATE TABLE decks (
uuid VARCHAR(255) NOT NULL,
shuffled BOOLEAN,
PRIMARY KEY (uuid)
);
- Create table for cards data
CREATE TABLE cards (
uuid VARCHAR(255) NOT NULL,
deck_uuid VARCHAR(255) NOT NULL,
value VARCHAR(255) NOT NULL,
suit VARCHAR(255) NOT NULL,
code VARCHAR(255) NOT NULL,
PRIMARY KEY (uuid),
FOREIGN KEY (deck_uuid) REFERENCES decks(uuid)
);
Path: /v1/decks
Method: POST
Query Parameters:
- shuffled (optional, boolean, default to false)
- cards (optional, comma separated string, 2-3 chars of value [2-10, A, J, Q, K] and suit [H, D, S, C], example 10C)
Path Parameters: -
Request Body: -
Request Header: -
Example Request: POST
/v1/decks?shuffled=true&cards=AS,KD,AC,2C,KH
Path: /v1/decks/{deck_id}
Method: GET
Query Parameters: -
Path Parameters:
- deck_id (required, uuid of the deck)
Request Body: -
Request Header: -
Example Request: GET
/v1/decks/a93c7204-0a94-45be-8062-3bff5e7845cb
Path: /v1/decks/{deck_id}/draw
Method: GET
Query Parameters:
- count (required, int, minimum of 1)
Path Parameters:
- deck_id (required, uuid of the deck)
Request Body: -
Request Header: -
Example Request: GET
/v1/decks/a93c7204-0a94-45be-8062-3bff5e7845cb/draw?count=1
Please refer to openapi.yml inside docs
directory.
Q: I can't run the built file
A: Give permission to execute the file by running this command
chmod +x ./main
Q: What will happen if I draw more cards than the remaining cards in the deck?
A: It will draw the remaining cards.
Q: What will happen if I draw card(s) from empty deck?
A: It will return an error.
- Docker image & compose (for simpler deployment)
- Cache (currently there's no need since the deck can only have 52 cards at most)
- Timestamp in Database (currently there's no data update, so the order of data will not be changed)
- Refactoring & cleaning