(Versão em Português | Clique aqui para a versão em Inglês.)
This project is a REST API that simulates services for casino users.
It was developed in Python using the FastAPI framework and PostgreSQL database.
It includes end-to-end (e2e), integration, and unit tests to ensure code robustness and quality. Tests were developed using Pytest. Additionally, this project uses Docker Compose to orchestrate two containerized services: casino-api (localhost:3001
) and postgresql-data (localhost:5433
) with password example
for access.
The objectives of this project are:
- Have code published for general access on the Github platform
- Use of
commit patterns
- Use of the FastAPI framework for backend application development
- Use of Pytest for end-to-end, integration, and unit test development
- API responses to be in JSON format
- Application containerization using Docker and Docker Compose for orchestration
- API documented with OpenAPI Swagger standard (
localhost:3001/docs
) - Definition of general endpoints
/players
,/balance
,/bet
,/win
,/rollback
, and/history
and their respective specifics - Validation of non-negative values
- Validation of player existence
- Data persistence with PostgreSQL relational database
- Code structure and readability: structure developed with
models
,schemas
,routes
,services
,repositories
, andexceptions
Barbara Calderon, software developer.
- Technologies Used
- Project Structure
- Running Locally with Docker Compose
- Running Tests with Pytest
- Application Endpoints Casino API
- Language: Python
- Framework: FastAPI
- Database: PostgreSQL
- Management: Poetry
- Main Dependencies:
- Pydantic: Used for data validation and serialization.
- Psycopg-binary: Driver for communication with PostgreSQL database.
- SQLAlchemy: ORM (Object-Relational Mapping) to interact with the database more intuitively.
- Asyncpg: Asynchronous driver for PostgreSQL, used for asynchronous database operations.
- Pytest: Testing framework used to write and execute tests.
The project is containerized into two main services:
- casino-api: contains the FastAPI application (
app/
) and tests (tests/
). - postgresql-data: contains the persisted data in PostgreSQL database.
The casino-api
directory has a Dockerfile
to create the Docker image of the application.
The project uses docker-compose.yml
to orchestrate the casino-api
and postgresql-data
containers.
The project was developed using poetry
, which manages dependencies, and also pytest
for testing.
To run the project locally, follow these steps:
-
Have Docker and Docker Compose installed on your machine.
-
Clone the repository:
git clone git@github.com:barbaracalderon/casino-backend.git
- Navigate to the project directory:
cd casino-backend/
- Execute Docker Compose to bring up the containers:
docker-compose up --build
If you encounter issues with the above command, you may need to use sudo docker-compose up --build
- In the browser, the casino-api service is available at:
localhost:3001
- PTo access the OpenAPI Swagger documentation:
localhost:3001/docs
- To access the database in Pg4Admin (if you wish), you need to define a server with database name, password, and address:port.
database = 'casino'
password = 'example'
localhost:5433
- To run tests, navigate to the directory:
cd casino-backend/casino-api
- Run the test command:
docker exec -it casino-api pytest
Those are 48 successfull tests.
HTTP Method | Endpoint | Description |
---|---|---|
POST | /players | Creates a new player. |
GET | /players | Retrieves the list of players. |
GET | /players/{player_id} | Retrieves details of a specific player. |
DELETE | /players/{player_id} | Deletes a specific player. |
PUT | /players/{player_id} | Updates information of a specific player. |
GET | /players/{player_id}/history | Retrieves transaction history of a specific player. |
GET | /balance | Retrieves the balance of a specific player. |
POST | /transactions/bet | Creates a new bet. |
GET | /transactions | Retrieves the list of transactions. |
GET | /transactions/{txn_uuid} | Retrieves details of a specific transaction. |
DELETE | /transactions/{transaction_id} | Deletes a specific transaction. |
POST | /transactions/win | Registers a win for a balance. |
POST | /transactions/rollback | Performs a rollback of a transaction. |
The project developed using FastAPI, PostgreSQL, Poetry, Pytest, and Docker represents a solution for managing transactions and players in a virtual casino environment. Choosing FastAPI enabled the development of an API leveraging modern Python features. PostgreSQL was used for data persistence, ensuring integrity and reliability in database operations.
The use of Poetry simplified dependency management, offering a consistent development environment and facilitating project maintenance. Implementation of unit, integration, and end-to-end tests with Pytest ensured the application functioned as expected across different scenarios.
The structured organization in models, exceptions, routes, services, repositories, and schemas aims to provide clean, modular, and maintainable code. Additionally, containerization with Docker and orchestration via Docker Compose enables portability, facilitating deployment in different environments.
In summary, this project dealt with building a REST API with dependency management and database management, implementing tests, and sought structural organization with code readability.
Regards,
Barbara Calderon.