4.1 KiB
4.1 KiB
Business Models Python Port
This directory contains a Python port of the business models from the Rust codebase, using SQLModel for database integration.
Overview
This project includes:
- Python port of Rust business models using SQLModel
- FastAPI server with OpenAPI/Swagger documentation
- CRUD operations for all models
- Convenience endpoints for common operations
The models ported from Rust to Python include:
- Currency: Represents a monetary value with amount and currency code
- Customer: Represents a customer who can purchase products or services
- Product: Represents a product or service offered
- ProductComponent: Represents a component of a product
- SaleItem: Represents an item in a sale
- Sale: Represents a sale of products or services
Structure
models.py
: Contains the SQLModel definitions for all business modelsexample.py
: Demonstrates how to use the models with a sample applicationinstall_and_run.sh
: Bash script to install dependencies usinguv
and run the exampleapi.py
: FastAPI server providing CRUD operations for all modelsserver.sh
: Bash script to start the FastAPI server
Requirements
- Python 3.7+
- uv for dependency management
Installation
The project uses uv
for dependency management. To install dependencies and run the example:
./install_and_run.sh
API Server
The project includes a FastAPI server that provides CRUD operations for all models and some convenience endpoints.
Starting the Server
To start the API server:
./server.sh
This script will:
- Create a virtual environment if it doesn't exist
- Install the required dependencies using
uv
- Start the FastAPI server with hot reloading enabled
API Documentation
Once the server is running, you can access the OpenAPI documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Available Endpoints
The API provides the following endpoints:
Currencies
GET /currencies/
: List all currenciesPOST /currencies/
: Create a new currencyGET /currencies/{currency_id}
: Get a specific currencyPUT /currencies/{currency_id}
: Update a currencyDELETE /currencies/{currency_id}
: Delete a currency
Customers
GET /customers/
: List all customersPOST /customers/
: Create a new customerGET /customers/{customer_id}
: Get a specific customerPUT /customers/{customer_id}
: Update a customerDELETE /customers/{customer_id}
: Delete a customerGET /customers/{customer_id}/sales/
: Get all sales for a customer
Products
GET /products/
: List all productsPOST /products/
: Create a new productGET /products/{product_id}
: Get a specific productPUT /products/{product_id}
: Update a productDELETE /products/{product_id}
: Delete a productGET /products/available/
: Get all available productsPOST /products/{product_id}/components/
: Add a component to a productGET /products/{product_id}/components/
: Get all components for a product
Sales
GET /sales/
: List all salesPOST /sales/
: Create a new saleGET /sales/{sale_id}
: Get a specific salePUT /sales/{sale_id}
: Update a saleDELETE /sales/{sale_id}
: Delete a salePUT /sales/{sale_id}/status/{status}
: Update the status of a salePOST /sales/{sale_id}/items/
: Add an item to a saleGET /sales/{sale_id}/items/
: Get all items for a sale
Dependencies
- SQLModel: For database models and ORM functionality
- Pydantic: For data validation (used by SQLModel)
- FastAPI: For creating the API server
- Uvicorn: ASGI server for running FastAPI applications
Example Usage
The example.py
script demonstrates:
- Creating an SQLite database
- Defining and creating tables for the models
- Creating sample data (customers, products, sales)
- Performing operations on the data
- Querying and displaying the data
To run the example manually (after activating the virtual environment):
# From the py directory
python example.py
# Or from the parent directory
cd py && python example.py