Giter Site home page Giter Site logo

darlanschwartz / api-gamebet Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 78 KB

This is a Web Service built for learning purposes, serving as a simulation of a game betting house.

Home Page: https://api-gamebet.onrender.com

Dockerfile 2.41% TypeScript 94.23% JavaScript 3.37%
docker express jest joi nodejs postgresql prisma render typescript

api-gamebet's Introduction

API Gamebet

This is a Web Service built for learning purposes. This was made with JavaScript/TypeScript ecosystem using Node.js with Express.js for the server, Prisma with PostgreSQL for the database, Joi for runtime validations, Jest with Supertest and Faker for automated tests, ESLint and Prettier for code patterns.

Deploy URL

Render deploy URL: https://api-gamebet.onrender.com

Requirements to use all that it is to be used

Usage

Clone and install

git clone https://github.com/DarlanSchwartz/API-Gamebet.git
cd API-Gamebet
npm install
  • Create a .env file at the root of the project folder.

This env.example file contains some field that you have to fill up with the correct information.

# Split your database url like this one into these variables below
# postgresql://postgres:postgres@localhost:2022/gamebet?schema=public

NODE_ENV=development
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=2022
POSTGRES_DATABASE=gamebet
DATABASE_URL=postgresql://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DATABASE}?schema=public

Run

  • Development mode -> auto-reloads when a file in the app is modified using nodemon to "watch" modified files.
npm run dev
  • Start command, this will look for the "dist" folder to start, this dist folder will only appear when you build/transpile the app to javascript.
npm start
  • Build command, this will transpile the app to javascript.
npm run build

Running with Docker

  1. Make sure that the docker service/software is running in your pc.
  • Build command
  1. Run when you need to test a new feature at the docker container, this command will rebuilt the container with the latest changes you have made into your code. Do not forget you have to make changes to the init.sql file with your new migration so the database is created with whatever changes you have made.
docker-compose build
  • Start command. This will run the database and API in the container at ports API:5000, Database: 2022
docker-compose up
  • Close command
docker-compose down

Routes

  • POST /participants

    • Creates a participant with a specific initial balance.

    • Input: participant's name and initial balance.

      {
      	name: string;
      	balance: number; // represented in cents, e.g., R$ 10.00 -> 1000
      }
    • Output: created participant object.

      {
      	id: number;
      	createdAt: string;
      	updatedAt: string;
      	name: string;
      	balance: number; // represented in cents, e.g., R$ 10.00 -> 1000
      }
  • POST /games

    • Creates a new game with an initial score of 0x0 and marked as unfinished.

    • Input: home team name and away team name.

      {
      	homeTeamName: string;
      	awayTeamName: string;
      }
    • Output: the created game object.

      {
      	id: number;
      	createdAt: string;
      	updatedAt: string;
      	homeTeamName: string;
      	awayTeamName: string;
      	homeTeamScore: number; // initially 0
      	awayTeamScore: number; // initially 0
      	isFinished: boolean; // initially false
      }
  • POST /bets

    • Registers a participant's bet on a specific game. The bet amount should be immediately deducted from the participant's balance.

    • Input:

      { 
      	homeTeamScore: number;
      	awayTeamScore: number; 
      	amountBet: number; // represented in cents, e.g., R$ 10.00 -> 1000
      	gameId: number; 
      	participantId: number;
      }
    • Output: the created bet object.

      {
      	id: number;
      	createdAt: string;
      	updatedAt: string;
      	homeTeamScore: number;
      	awayTeamScore: number;
      	amountBet: number; // represented in cents, e.g., R$ 10.00 -> 1000
      	gameId: number; 
      	participantId: number;
      	status: string; // can be PENDING, WON, or LOST
      	amountWon: number || null; // null when the bet is still PENDING; number if the bet is already WON or LOST, with the won amount represented in cents
      }
  • POST /games/:id/finish

    • Finishes a game and consequently updates all bets associated with it, calculating the amount won in each one and updating the balances of winning participants.

    • Input: final score of the game.

      {
      	homeTeamScore: number;
      	awayTeamScore: number;
      }
    • Output: the updated game object.

      {
      	id: number;
      	createdAt: string;
      	updatedAt: string;
      	homeTeamName: string;
      	awayTeamName: string;
      	homeTeamScore: number;
      	awayTeamScore: number;
      	isFinished: boolean;
      }
  • GET /participants

    • Returns all participants and their respective balances.

    • Output: an array of all participants.

      [
      	{
      		id: number;
      		createdAt: string;
      		updatedAt: string;
      		name: string;
      		balance: number; // represented in cents, e.g., R$ 10.00 -> 1000
      	}, 
      	{...}
      ]
  • GET /games

    • Returns all registered games.

    • Output: an array of all games.

      [
      	{
      		id: number;
      		createdAt: string;
      		updatedAt: string;
      		homeTeamName: string;
      		awayTeamName: string;
      		homeTeamScore: number;
      		awayTeamScore: number;
      		isFinished: boolean;
      	},
      	{...}
      ]
  • GET /games/:id

    • Returns the data of a game along with the bets associated with it.

    • Output: the game object containing an array of bets made on it.

      {
      	id: number;
      	createdAt: string;
      	updatedAt: string;
      	homeTeamName: string;
      	awayTeamName: string;
      	homeTeamScore: number;
      	awayTeamScore: number;
      	isFinished: boolean;
      	bets: {
      		id: number;
      		createdAt: string;
      		updatedAt: string;
      		homeTeamScore: number;
      		awayTeamScore: number;
      		amountBet: number; // represented in cents, e.g., R$ 10.00 -> 1000
      		gameId: number; 
      		participantId: number;
      		status: string; // can be PENDING, WON, or LOST
      		amountWon: number || null; // null when the bet is still PENDING; number if the bet is already WON or LOST, with the won amount represented in cents
      	}[]
      }

api-gamebet's People

Contributors

darlanschwartz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.