Saltar a contenido

Routes#

API routes for managing transactions in the "alcancia" (piggy bank) application.

Routes
  • GET /alcancia/transactions: Retrieve the list of all transactions.
  • PUT /alcancia/transaction/{txn_type}/{quantity}: Create a new transaction (deposit or withdraw).

Functions:

Name Description
- `compute_balance`

Compute the balance by summing up the amounts of all transactions in the database.

- `transactions_list`

Retrieve the list of all transactions.

- `create_transaction`

Create a new transaction (Deposit/Withdraw)

Dependencies
  • fastapi: FastAPI framework for building APIs.
  • pydantic: Data validation and settings management using Python type annotations.
  • sqlalchemy: SQL toolkit and Object-Relational Mapping (ORM) library.
  • sqlmodel: SQL databases in Python, designed to be compatible with FastAPI.
  • app.main: Main application module containing the database session.
  • .models: Module containing the Transaction model.
  • .schemas: Module containing the response schemas (TransactionResponse, TransactionResult, Transaction`Type).

compute_balance(db) async #

Compute the balance by summing up the amounts of all transactions in the database.

Parameters:

Name Type Description Default
db Session

The database session used to execute the query.

required

Returns:

Name Type Description
int int

The total balance computed from the sum of all transaction amounts. Returns 0 if there are no transactions.

create_transaction(txn_type, quantity, db, balance) async #

Create a new transaction (deposit or withdraw) in Alcancia.

This endpoint creates a new transaction in the Alcancia application. It can handle both deposit and withdrawal transactions. If the transaction type is withdraw and the quantity exceeds the current balance, the transaction is rejected.

Args: - txn_type (TransactionType): The type of transaction ("deposit" or "withdraw"). - quantity (PositiveInt): The amount for the transaction. Important: Provide a positive integer representing cents, not units.

Returns: - TransactionResponse: The response containing the result of the transaction, the previous balance, and the new balance.

Raises: - HTTPException: If the transaction is a withdrawal and the quantity exceeds the current balance, a 403 Forbidden error is raised with a rejection response.

transactions_list(db) #

Retrieve the list of all transactions in Alcancia.

This endpoint returns all transactions recorded in the Alcancia application.

  • Returns: list[Transaction]: A list of all transactions in the database.

Schemas#

Esquemas de validación de datos.

TransactionResponse #

Bases: BaseModel

Represents the result of a transaction.

balance instance-attribute #

The balance after the transaction takes place

previous_balance instance-attribute #

The balance before the transaction takes place

result instance-attribute #

The result of this transaction

TransactionResult #

Bases: StrEnum

The transaction result.

TransactionType #

Bases: StrEnum

Defines the transaction type. Only two types at the moment.


Models#

All the database models here.

Transaction #

Bases: SQLModel

Modelo para registrar movimientos en la base de datos.

now_utc() #

Get the current UTC datetime.

Returns:

Name Type Description
datetime

The current datetime in UTC.