Skip to content

nvzard/Fantasy-Soccer-Manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Soccer Manager

Summary

This project exposes a RESTful API for a simple application where football/soccer fans will create fantasy teams and will be able to sell or buy players.

Documentation


Tech Stack

Written in Go (Golang 1.18) using gin as web framework, Postgres as database.

The application is containerized using docker and code-syncin development env uses docker-sync.

  • Use make build to build the web server and make start to start the server. Server runs on localhost:8080.
  • Use make stop stop the development environment.
  • Use make clean to clean the dev environment by deleting stale containers.
  • Use docker volume prune to clean all volumes in case of local DB failure. [Warning: This will prune all volumes]
  • Use make test runs the tests in a seperate docker container for isolation. [TODO]
  • Air and docker-sync are used for live reloading during development.

Development Setup

Requirements

make
git
docker (v20.10.10)
docker-compose (v1.29.2)
docker-sync (0.7.2) [Use `gem install docker-sync -v 0.7.2` for mac]

Install Requirements:

  1. Docker-Sync Install: http://docker-sync.io/
  2. Docker Install: https://docs.docker.com/engine/install/

Getting Started with Development

  1. Clone repository
git clone https://github.com/nvzard/soccer-manager.git
  1. Change the directory to the cloned repository
cd soccer-manager
  1. Make sure all requirements are installed (docker, docker-compose, docker-sync)
docker --version
docker-compose --version
docker-sync --version
  1. Build image
make build
  1. Run start command
make start

PS: If make start fails for the first time use make stop to stop the containers and do make start again.

Stop Dev Environment

make stop

Run Tests [WIP]

make test

Remove all artifacts and dependencies

make clean

API Documentation

GET     /healthcheck                          # ok

POST    /api/user                             # register new user (json parameters: first_name, last_name, email, password)
POST    /api/auth                             # generate Authorization token for the user (json parameters: email, password)

[** Below API Endpoints Require Authorization token in header **]

GET     /api/ping                             # pong

GET     /api/user/:email                      # get user by email

GET     /api/team/:id                         # get team by id
PATCH   /api/team/:id                         # update team by id (json parameters: name, country)

GET     /api/player/:id                       # get player by id
PATCH   /api/player/:id                       # update team by id (json parameters: first_name, last_name, country)

GET     /api/transfers/                       # get the transfers list
POST    /api/transfers/                       # add player to transfer list (json parameters: player_id, asking_price)
POST    /api/transfers/buy/:player_id         # buy a player on the transfers list for the asking_price

1. Register a new user

POST /api/user
JSONParameter Type Description
first_name string FirstName of the user
last_name string LastName of the user
email string valid email address
password string password (len >= 8)

Example Request

POST /api/user

{
    "first_name": "John",
    "last_name": "Lennon",
    "email": "[email protected]",
    "password": "qwerty65",
}

Response

{
    "id": 1,
    "email": "[email protected]"
}

2. Generate Authorization Token

POST /api/auth
JSONParameter Type Description
email string email of the user
password string password of the user

Example Request

POST /api/user

{
    "email": "[email protected]",
    "password": "qwerty65",
}

Response

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJkcmFrZUBnbWFpbC5jb20iLCJ0ZWFtX2lkIjoxLCJleHAiOjE2NTc4Mjc4MTl9"
}

3. Get User By Email [Authorization Token Required in headers]

GET /api/user/:email

Example Request

GET /api/user/[email protected]

Response

{
    "id": 1,
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Lennon",
    "team_id": 1
}

4. Get Team By ID [Authorization Token Required in headers]

GET /api/team/:id

Example Request

GET /api/team/1

Response

Every newly formed team has 3 goalkeepers(GK), 6 defenders(DEF), 6 midfielders(MID), 5 attackers(ATT)

{
    "available_cash": 5000000,
    "country": "France",
    "id": 1,
    "name": "Iron Squids",
    "owner_id": 1,
    "players": [
        {
            "id": 1,
            "first_name": "Douglas",
            "last_name": "Mejia",
            "country": "Germany",
            "age": 36,
            "position": "GK",
            "market_value": 1000000,
            "team_id": 1
        },
        .
        .
        .
        {
            "id": 20,
            "first_name": "Ryland",
            "last_name": "Mejia",
            "country": "USA",
            "age": 23,
            "position": "ATT",
            "market_value": 1000000,
            "team_id": 1
        }
    ],
    "team_value": 20000000
}

5. Update Team (Name, Country) By ID [Authorization Token Required in headers]

PATCH /api/team/:id
JSONParameter Type Description
name string new team name
country string new team country

Example Request

PATCH /api/team/1

{
    "name": "Leopards"
    "country": "China",
}

Response

{
    "id": 1,
    "available_cash": 5000000,
    "country": "China",
    "name": "Leopards",
    "owner_id": 1,
    "players": [
        {
            "id": 1,
            "first_name": "Douglas",
            "last_name": "Mejia",
            "country": "Germany",
            "age": 36,
            "position": "GK",
            "market_value": 1000000,
            "team_id": 1
        },
        .
        .
        .
        {
            "id": 20,
            "first_name": "Ryland",
            "last_name": "Mejia",
            "country": "USA",
            "age": 23,
            "position": "ATT",
            "market_value": 1000000,
            "team_id": 1
        }
    ],
    "team_value": 20000000
}

6. Get Player By ID [Authorization Token Required in headers]

GET /api/player/:id

Example Request

GET /api/player/5

Response

{
    "id": 5,
    "age": 32,
    "country": "Germany",
    "first_name": "Nathaniel",
    "last_name": "Mejia",
    "market_value": 1000000,
    "position": "DEF",
    "team_id": 1
}

7. Update Player (First Name, Last Name, Country) By ID [Authorization Token Required in headers]

PATCH /api/player/:id
JSONParameter Type Description
first_name string new first name
last_name string new last name
country string new country

Example Request

PATCH /api/team/1

{
    "first_name": "Saitama",
    "last_name": "Senpai",
    "country": "Japan",
}

Response

{
    "id": 1,
     "first_name": "Saitama",
    "last_name": "Senpai",
    "age": 36,
    "country": "Japan",
    "market_value": 1000000,
    "position": "GK",
    "team_id": 1
}

8. Add Player to transfer list [Authorization Token Required in headers]

POST /api/transfers
JSONParameter Type Description
player_id string id of the player
asked_price string asked price for the player

Example Request

POST /api/transfers

{
    "player_id": 6,
    "asked_price": 1050000
}

Response

{
    "transfer_id": 8
}

9. Get all players on the transfer list [Authorization Token Required in headers]

GET /api/transfers

Example Request

GET /api/transfers

Response

{
    "transfer_list": [
        {
            "id": 1,
            "player_id": 6,
            "market_value": 1000000,
            "asked_price": 1050000,
            "player": {
                "id": 6,
                "first_name": "Grant",
                "last_name": "Klein",
                "country": "Qatar",
                "age": 23,
                "position": "DEF",
                "market_value": 1000000,
                "team_id": 1
            }
        }
    ]
}

10. Buy player from the transfers list [Authorization Token Required in headers]

POST /transfers/buy/:player_id

This transfers the player from selling team to buying team and adjusts the team's bank balance accordingly. A player's market value increases by 10-100% after every transfer.

Example Request

POST /transfers/buy/6

Response

{
    "status": "done"
}

Future Improvements

Some of the improvements that can be made but were skipped due to time constraints.

  • Add unit tests.
  • Add postman collection to test and document the API.
  • Add admin roles to have CRUD access for all the users, teams, players and transfers.
  • Build a user-interface(UI) for the backend.

About

REST Backend for a Fantasy Soccer/Football Game

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published