DiegoBeker / quer-apostar

Back-end for a sports bet system

Home Page:https://quer-apostar.onrender.com/health

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quer Apostar?

Back-end for Quer Apostar a sports betting system

Deploy

This API is available on REnder

https://quer-apostar.onrender.com

Tools

TypeScript NodeJs ExpressJs PostgreSQL Prisma Jest

How to run for development

  1. Clone this repository
  2. Install all dependencies
npm i
  1. Create a PostgreSQL database with whatever name you want
  2. Configure the .env.development file using the .env.example file (see "Running application locally or inside docker section" for details)
  3. Run all migrations
npm run dev:migration:run
  1. Run the back-end in a development environment:
npm run dev

How to run tests

  1. Follow the steps in the last section
  2. Configure the .env.test file using the .env.example file (see "Running application locally or inside docker" section for details)
  3. Run all migrations:
npm run test:migration:run
  1. Run test:
npm run test

Building and starting for production

npm run build
npm start

How to run Docker Image

Create a network

docker network create --driver bridge qa-network

Create a volume

docker volume create qa-volume

Run a postgres container inside the created network and using the created volume

docker run --name pg-querapostar --network=qa-network -e POSTGRES_PASSWORD=postgres -p 5433:5432 -v qa-volume:/var/lib/postgresql/data -d postgres
docker run -d --name querapostar --network=qa-network -e POSTGRES_HOST=pg-querapostar -p 4040:4000 diegobeker/quer-apostar-quanto

Check it out on localhost/4040/health

Build docker image from this project

Clone this repository and run

docker build -t myusername/myimage:tag .
docker run -d --name my-container -p 4040:4000 myusername/myimage:tag

Check it out on localhost/4040/health

How to run with docker-compose

Clone this repository and run

docker compose up
docker compose down

Entities

Participant

{
	id: number; // ex: 1
	createdAt: string; // ex: "2023-09-27T19:22:50.503Z"
	updatedAt: string; // ex: "2023-09-27T19:22:50.503Z"
	name: string; // ex: "James"
	balance: number; // balance of a participant in cents, ex: 1000 ($ 10,00)
}

Game

{
	id: number; // game id
	createdAt: string;
	updatedAt: string;
	homeTeamName: string; // ex: "Flamengo"
	awayTeamName: string; // ex: "Fluminense"
	homeTeamScore: number; // score of Home team, ex: 3
	awayTeamScore: number; // score of away team, ex: 1
	isFinished: boolean; // true if game is finished, false if it is not finished
}

Bet

{
	id: number; // bet id
	createdAt: string;
	updatedAt: string;
	homeTeamScore: number; // bet on score of Home team, ex: 3,
	awayTeamScore: number; // bet on score of Away team, ex: 2,
	amountBet: number; // amount bet in cents
	gameId: number; // game id
	participantId: number; // participant id that made the bet
	status: string; // state of the bet, it can be PENDING (game is not finished), WON (got the final score right) ou LOST (got the final score wrong)
	amountWon: number || null; // amount won or null while the bet status is PENDING
}

Endpoints

POST /participants

  • Create a participant with an initial balance

  • Request Body: name and balance

{
	"name": "Diego",
	"balance": 10000, // represented in cents, must be 10000 or greater
}
  • Response: participant created
{
    "id": 65,
    "createdAt": "2023-10-08T14:38:56.720Z",
    "updatedAt": "2023-10-08T14:50:52.898Z",
    "name": "Yasmin Pereira",
    "balance": 659801
}

POST /games

  • Create a new game with initial score 0x0 and isFinished = false

  • Request Body: home team and away team names

{
	"homeTeamName": "Flamengo",
	"awayTeamName": "Fluminense"
}
  • Response: game created
{
   "id": 76,
   "createdAt": "2023-10-08T22:28:03.779Z",
   "updatedAt": "2023-10-08T22:28:03.779Z",
   "homeTeamName": "Flamengo",
   "awayTeamName": "Fluminense",
   "homeTeamScore": 0,
   "awayTeamScore": 0,
   "isFinished": false
}

POST /bets

  • Create a bet that belong to a participant in a specific game

  • Request Body: home team and away team names

{ 
	"homeTeamScore": 3,
	"awayTeamScore": 2, 
	"amountBet": 1000,
	"gameId": 75,
	"participantId": 67
}
  • Response: bet created
{
   "id": 32,
   "createdAt": "2023-10-08T22:57:34.602Z",
   "updatedAt": "2023-10-08T22:57:34.602Z",
   "homeTeamScore": 3,
   "awayTeamScore": 2,
   "amountBet": 1000,
   "gameId": 76,
   "participantId": 67,
   "status": "PENDING",
   "amountWon": null
}

POST /games/:id/finish

  • Finish a game and update all bets that belong to that game, also update the balance of participants.

  • Request Body: home team and away team names

{
	"homeTeamScore": 3,
	"awayTeamScore": 2
}
  • Response: game finished
{
   "id": 76,
   "createdAt": "2023-10-08T22:28:03.779Z",
   "updatedAt": "2023-10-08T23:00:11.027Z",
   "homeTeamName": "Flamengo",
   "awayTeamName": "Fluminense",
   "homeTeamScore": 3,
   "awayTeamScore": 2,
   "isFinished": true
}

GET /participants

  • Return all participants with current balance

  • Response: all participants

[   
   {
       "id": 66,
       "createdAt": "2023-10-08T14:38:57.030Z",
       "updatedAt": "2023-10-08T14:38:57.030Z",
       "name": "Lorena Xavier",
       "balance": 229050
   },
   {
       "id": 67,
       "createdAt": "2023-10-08T14:49:40.128Z",
       "updatedAt": "2023-10-08T22:57:34.555Z",
       "name": "Jose",
       "balance": 11500
   }
]

GET /games

  • Return all games
[
   {
       "id": 75,
       "createdAt": "2023-10-08T14:49:56.005Z",
       "updatedAt": "2023-10-08T14:51:49.718Z",
       "homeTeamName": "Flamengo",
       "awayTeamName": "Fluminense",
       "homeTeamScore": 3,
       "awayTeamScore": 2,
       "isFinished": true
   },
   {
       "id": 76,
       "createdAt": "2023-10-08T22:28:03.779Z",
       "updatedAt": "2023-10-08T23:00:11.027Z",
       "homeTeamName": "Corinthians",
       "awayTeamName": "Palmeiras",
       "homeTeamScore": 0,
       "awayTeamScore": 0,
       "isFinished": false
   }
]

GET /games/:id

  • Return a specifc game with all bets of that game
{
    "id": 76,
    "createdAt": "2023-10-08T22:28:03.779Z",
    "updatedAt": "2023-10-08T23:00:11.027Z",
    "homeTeamName": "Flamengo",
    "awayTeamName": "Fluminense",
    "homeTeamScore": 3,
    "awayTeamScore": 2,
    "isFinished": true,
    "Bet": [
        {
            "id": 32,
            "createdAt": "2023-10-08T22:57:34.602Z",
            "updatedAt": "2023-10-08T22:57:34.602Z",
            "homeTeamScore": 3,
            "awayTeamScore": 2,
            "amountBet": 1000,
            "gameId": 76,
            "participantId": 67,
            "status": "WON",
            "amountWon": 700
        }
    ]
}

About

Back-end for a sports bet system

https://quer-apostar.onrender.com/health


Languages

Language:TypeScript 95.5%Language:JavaScript 4.0%Language:Dockerfile 0.3%Language:Shell 0.2%