This is a banking management API that allows the creation of accounts and processing of financial transactions through various methods like debit, credit, and Pix. The API is built using Flask, MongoDB, and Pydantic for validation, and follows the MVC pattern.
- Create accounts with initial balances.
- Retrieve account details.
- Process transactions (debit, credit, Pix) with associated transaction fees.
- Ensure account balances don't go negative.
- Input validation with Pydantic.
- API versioning implemented using
/api/v1
.
- Python 3.10 or above
- MongoDB (local instance or MongoDB Atlas)
pipenv
orvirtualenv
for environment management (optional but recommended)
bank-management-api/
├── app/
│ ├── __init__.py
│ ├── controllers/
│ │ ├── account_controller/
│ │ │ └── __init__.py
│ │ ├── transaction_controller/
│ │ │ ├── __init__.py
│ │ │ └── constants.py
│ ├── models/
│ │ ├── account.py
│ │ ├── payment_method.py
│ │ └── schemas.py
│ └── views/
│ ├── account_routes.py
│ └── transaction_routes.py
├── tests/
| ├── __init__.py
│ ├── test_accounts.py
│ ├── test_transactions.py
│ └── conftest.py
├── run.py
├── config.py
├── requirements.txt
└── README.md
You can use virtualenv
or pipenv
to manage dependencies within a virtual environment:
# Using virtualenv
python3 -m venv venv
source venv/bin/activate
Run the following command to install all required Python libraries:
pip install -r requirements.txt
Ensure MongoDB is running on your local machine or configure a connection to MongoDB Atlas. By default, the API expects MongoDB to be running on mongodb://localhost:27017/bank_db
.
To run MongoDB locally:
-
For Linux/macOS, run MongoDB in the background:
mongod --dbpath /path/to/your/db
-
For Windows, you can start MongoDB as a service using the installed service manager or by using the MongoDB shell:
mongod --dbpath "C:\path\to\your\db"
You can also use Docker to run MongoDB:
docker run -d -p 27017:27017 --name mongodb mongo
The project uses a Config
class to define the MongoDB connection settings. You can edit the config.py
file as needed for custom database URIs or environments.
For example, in config.py
:
class Config:
MONGO_URI = "mongodb://localhost:27017/bank_db" # Edit as needed
Once everything is set up, you can run the Flask app:
python run.py
The API will be accessible at http://localhost:5000
.
POST /api/v1/conta
Example Request:
{
"numero_conta": 12345,
"saldo": 100.0
}
Example Response:
{
"numero_conta": 12345,
"saldo": 100.0
}
GET /api/v1/conta?numero_conta=<account_number>
Example Response:
{
"numero_conta": 12345,
"saldo": 100.0
}
POST /api/v1/transacao
Example Request (Debit):
{
"numero_conta": 12345,
"valor": 10,
"forma_pagamento": "D"
}
Example Response:
{
"numero_conta": 12345,
"saldo": 89.7 # after 3% tax
}
The project uses pytest
for unit testing. To ensure MongoDB is running for the tests, it's recommended to use a separate test database, as defined in tests/conftest.py
.
Use the following command to run the tests:
pytest
This will run all the test cases located in the tests/
directory. Ensure that the MongoDB instance is running, as the tests rely on MongoDB for account and transaction data.
Tests will automatically clean up any created accounts or transactions after each test case.
Method | Endpoint | Description |
---|---|---|
POST | /api/v1/conta |
Create a new account |
GET | /api/v1/conta |
Get account details by number |
POST | /api/v1/transacao |
Process a transaction |
- Create Account:
{ "numero_conta": <int>, "saldo": <float> }
- Process Transaction:
{ "numero_conta": <int>, "valor": <float>, "forma_pagamento": "D" | "C" | "P" }
201
: Created200
: OK400
: Bad Request (validation errors)404
: Not Found (account not found)
- Follow best practices for code formatting and structure.
- All the routes are versioned with
/api/v1
to support future enhancements.