Skip to content

madeindra/deck-of-card-api-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toggl Backend Unattended Programming Test

The aim of this project is to create REST API that can simulate a deck of cards.

How To Run

  1. Create .env by copying values from .env.example
  2. Edit the values inside .env to reflect to your environment
  3. Build using make or go
make build

or

go build -o main
  1. Run the app (make sure you already have the required database & tables)
./main

Database Preparation

  1. Create a database in PostgreSQL
CREATE DATABASE deckofcards;
  1. Create table for decks data
CREATE TABLE decks (
  uuid VARCHAR(255) NOT NULL,
  shuffled BOOLEAN,
  PRIMARY KEY (uuid)
);
  1. 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)
);

APIs

Create a new Deck

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

Open a Deck

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

Draw a Card (or some Cards)

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

Open API Specification

Please refer to openapi.yml inside docs directory.

FAQ

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.

Further Development

  • 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

About

Toggl Backend Engineering Test

Resources

Stars

Watchers

Forks

Languages