Implement complete small business inventory management system
Features include: - User management with JWT authentication and role-based access - Inventory items with SKU/barcode tracking and stock management - Categories and suppliers organization - Inventory transactions with automatic stock updates - Low stock alerts and advanced search/filtering - RESTful API with comprehensive CRUD operations - SQLite database with Alembic migrations - Auto-generated API documentation Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
5c281e6bd9
commit
252ce19872
121
README.md
121
README.md
@ -1,3 +1,120 @@
|
|||||||
# FastAPI Application
|
# Small Business Inventory System
|
||||||
|
|
||||||
This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform.
|
A comprehensive inventory management system for small businesses built with Python and FastAPI.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **User Management**: Secure authentication with JWT tokens and role-based access control
|
||||||
|
- **Inventory Management**: Complete CRUD operations for inventory items with SKU and barcode support
|
||||||
|
- **Category Management**: Organize inventory items by categories
|
||||||
|
- **Supplier Management**: Track supplier information and relationships
|
||||||
|
- **Stock Tracking**: Real-time stock levels with low stock alerts
|
||||||
|
- **Transaction History**: Track all inventory movements (in/out/adjustments)
|
||||||
|
- **Search & Filter**: Advanced search and filtering capabilities
|
||||||
|
- **RESTful API**: Well-documented API endpoints with automatic OpenAPI documentation
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
- **Framework**: FastAPI
|
||||||
|
- **Database**: SQLite with SQLAlchemy ORM
|
||||||
|
- **Authentication**: JWT tokens with bcrypt password hashing
|
||||||
|
- **Migrations**: Alembic for database schema management
|
||||||
|
- **Documentation**: Automatic API documentation with Swagger UI
|
||||||
|
|
||||||
|
## Installation & Setup
|
||||||
|
|
||||||
|
1. Install dependencies:
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Set up environment variables (optional):
|
||||||
|
```bash
|
||||||
|
export SECRET_KEY="your-secret-key-here"
|
||||||
|
export ADMIN_EMAIL="admin@yourcompany.com"
|
||||||
|
export ADMIN_PASSWORD="secure-admin-password"
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Run the application:
|
||||||
|
```bash
|
||||||
|
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
The following environment variables can be set:
|
||||||
|
|
||||||
|
- `SECRET_KEY`: JWT secret key (defaults to development key - change in production!)
|
||||||
|
- `ADMIN_EMAIL`: Initial admin user email (default: admin@example.com)
|
||||||
|
- `ADMIN_PASSWORD`: Initial admin user password (default: admin123)
|
||||||
|
|
||||||
|
**⚠️ Important**: Change the `SECRET_KEY`, `ADMIN_EMAIL`, and `ADMIN_PASSWORD` environment variables in production!
|
||||||
|
|
||||||
|
## API Documentation
|
||||||
|
|
||||||
|
Once the application is running, you can access:
|
||||||
|
|
||||||
|
- **Swagger UI**: http://localhost:8000/docs
|
||||||
|
- **ReDoc**: http://localhost:8000/redoc
|
||||||
|
- **OpenAPI JSON**: http://localhost:8000/openapi.json
|
||||||
|
- **Health Check**: http://localhost:8000/health
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
- `POST /api/v1/auth/login` - Login and get access token
|
||||||
|
|
||||||
|
### Users
|
||||||
|
- `GET /api/v1/users/me` - Get current user info
|
||||||
|
- `GET /api/v1/users/` - List all users (admin only)
|
||||||
|
- `POST /api/v1/users/` - Create new user (admin only)
|
||||||
|
|
||||||
|
### Categories
|
||||||
|
- `GET /api/v1/categories/` - List all categories
|
||||||
|
- `POST /api/v1/categories/` - Create new category
|
||||||
|
- `PUT /api/v1/categories/{id}` - Update category
|
||||||
|
- `DELETE /api/v1/categories/{id}` - Delete category
|
||||||
|
|
||||||
|
### Suppliers
|
||||||
|
- `GET /api/v1/suppliers/` - List all suppliers
|
||||||
|
- `POST /api/v1/suppliers/` - Create new supplier
|
||||||
|
- `PUT /api/v1/suppliers/{id}` - Update supplier
|
||||||
|
- `DELETE /api/v1/suppliers/{id}` - Delete supplier
|
||||||
|
|
||||||
|
### Inventory
|
||||||
|
- `GET /api/v1/inventory/` - List inventory items (with search/filter options)
|
||||||
|
- `GET /api/v1/inventory/low-stock` - Get low stock items
|
||||||
|
- `POST /api/v1/inventory/` - Create new inventory item
|
||||||
|
- `GET /api/v1/inventory/{id}` - Get inventory item by ID
|
||||||
|
- `GET /api/v1/inventory/sku/{sku}` - Get inventory item by SKU
|
||||||
|
- `PUT /api/v1/inventory/{id}` - Update inventory item
|
||||||
|
- `DELETE /api/v1/inventory/{id}` - Delete inventory item
|
||||||
|
|
||||||
|
### Transactions
|
||||||
|
- `GET /api/v1/transactions/` - List inventory transactions
|
||||||
|
- `POST /api/v1/transactions/` - Create new transaction (updates stock levels)
|
||||||
|
- `GET /api/v1/transactions/{id}` - Get transaction by ID
|
||||||
|
|
||||||
|
## Database Schema
|
||||||
|
|
||||||
|
The system uses SQLite with the following main entities:
|
||||||
|
|
||||||
|
- **Users**: System users with authentication
|
||||||
|
- **Categories**: Item categories for organization
|
||||||
|
- **Suppliers**: Supplier information and contacts
|
||||||
|
- **InventoryItems**: Main inventory items with pricing and stock info
|
||||||
|
- **InventoryTransactions**: Track all stock movements
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
- **Linting**: The project uses Ruff for code linting and formatting
|
||||||
|
- **Database**: SQLite database stored in `/app/storage/db/`
|
||||||
|
- **Migrations**: Use Alembic for database schema changes
|
||||||
|
|
||||||
|
## Default Admin User
|
||||||
|
|
||||||
|
The system creates a default admin user on first run:
|
||||||
|
- **Email**: admin@example.com (or value from `ADMIN_EMAIL` env var)
|
||||||
|
- **Password**: admin123 (or value from `ADMIN_PASSWORD` env var)
|
||||||
|
|
||||||
|
**⚠️ Security Note**: Change the default admin credentials immediately in production!
|
||||||
|
41
alembic.ini
Normal file
41
alembic.ini
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
[alembic]
|
||||||
|
script_location = alembic
|
||||||
|
prepend_sys_path = .
|
||||||
|
version_path_separator = os
|
||||||
|
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
|
||||||
|
|
||||||
|
[post_write_hooks]
|
||||||
|
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
56
alembic/env.py
Normal file
56
alembic/env.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
from logging.config import fileConfig
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
|
# Add the parent directory to sys.path to import app modules
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from app.db.base import Base
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
if config.config_file_name is not None:
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
"""Run migrations in 'offline' mode."""
|
||||||
|
url = settings.SQLALCHEMY_DATABASE_URL
|
||||||
|
context.configure(
|
||||||
|
url=url,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
"""Run migrations in 'online' mode."""
|
||||||
|
configuration = config.get_section(config.config_ini_section)
|
||||||
|
configuration["sqlalchemy.url"] = settings.SQLALCHEMY_DATABASE_URL
|
||||||
|
|
||||||
|
connectable = engine_from_config(
|
||||||
|
configuration,
|
||||||
|
prefix="sqlalchemy.",
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection, target_metadata=target_metadata
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
24
alembic/script.py.mako
Normal file
24
alembic/script.py.mako
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = ${repr(up_revision)}
|
||||||
|
down_revision = ${repr(down_revision)}
|
||||||
|
branch_labels = ${repr(branch_labels)}
|
||||||
|
depends_on = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
${downgrades if downgrades else "pass"}
|
121
alembic/versions/001_initial_migration.py
Normal file
121
alembic/versions/001_initial_migration.py
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
"""Initial migration
|
||||||
|
|
||||||
|
Revision ID: 001
|
||||||
|
Revises:
|
||||||
|
Create Date: 2024-01-01 00:00:00.000000
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '001'
|
||||||
|
down_revision = None
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# Create users table
|
||||||
|
op.create_table('users',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('email', sa.String(), nullable=False),
|
||||||
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
||||||
|
sa.Column('full_name', sa.String(), nullable=False),
|
||||||
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||||
|
sa.Column('is_admin', sa.Boolean(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||||
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
||||||
|
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
||||||
|
|
||||||
|
# Create categories table
|
||||||
|
op.create_table('categories',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||||
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_categories_id'), 'categories', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_categories_name'), 'categories', ['name'], unique=True)
|
||||||
|
|
||||||
|
# Create suppliers table
|
||||||
|
op.create_table('suppliers',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('contact_person', sa.String(), nullable=True),
|
||||||
|
sa.Column('email', sa.String(), nullable=True),
|
||||||
|
sa.Column('phone', sa.String(), nullable=True),
|
||||||
|
sa.Column('address', sa.Text(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||||
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_suppliers_id'), 'suppliers', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_suppliers_name'), 'suppliers', ['name'], unique=True)
|
||||||
|
|
||||||
|
# Create inventory_items table
|
||||||
|
op.create_table('inventory_items',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('sku', sa.String(), nullable=False),
|
||||||
|
sa.Column('barcode', sa.String(), nullable=True),
|
||||||
|
sa.Column('cost_price', sa.Float(), nullable=False),
|
||||||
|
sa.Column('selling_price', sa.Float(), nullable=False),
|
||||||
|
sa.Column('quantity_in_stock', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('minimum_stock_level', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('maximum_stock_level', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('category_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('supplier_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||||
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_inventory_items_barcode'), 'inventory_items', ['barcode'], unique=True)
|
||||||
|
op.create_index(op.f('ix_inventory_items_id'), 'inventory_items', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_inventory_items_name'), 'inventory_items', ['name'], unique=False)
|
||||||
|
op.create_index(op.f('ix_inventory_items_sku'), 'inventory_items', ['sku'], unique=True)
|
||||||
|
|
||||||
|
# Create inventory_transactions table
|
||||||
|
op.create_table('inventory_transactions',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('transaction_type', sa.Enum('IN', 'OUT', 'ADJUSTMENT', name='transactiontype'), nullable=False),
|
||||||
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('unit_cost', sa.Float(), nullable=True),
|
||||||
|
sa.Column('total_cost', sa.Float(), nullable=True),
|
||||||
|
sa.Column('reference_number', sa.String(), nullable=True),
|
||||||
|
sa.Column('notes', sa.Text(), nullable=True),
|
||||||
|
sa.Column('item_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['item_id'], ['inventory_items.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_inventory_transactions_id'), 'inventory_transactions', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_inventory_transactions_reference_number'), 'inventory_transactions', ['reference_number'], unique=False)
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_index(op.f('ix_inventory_transactions_reference_number'), table_name='inventory_transactions')
|
||||||
|
op.drop_index(op.f('ix_inventory_transactions_id'), table_name='inventory_transactions')
|
||||||
|
op.drop_table('inventory_transactions')
|
||||||
|
op.drop_index(op.f('ix_inventory_items_sku'), table_name='inventory_items')
|
||||||
|
op.drop_index(op.f('ix_inventory_items_name'), table_name='inventory_items')
|
||||||
|
op.drop_index(op.f('ix_inventory_items_id'), table_name='inventory_items')
|
||||||
|
op.drop_index(op.f('ix_inventory_items_barcode'), table_name='inventory_items')
|
||||||
|
op.drop_table('inventory_items')
|
||||||
|
op.drop_index(op.f('ix_suppliers_name'), table_name='suppliers')
|
||||||
|
op.drop_index(op.f('ix_suppliers_id'), table_name='suppliers')
|
||||||
|
op.drop_table('suppliers')
|
||||||
|
op.drop_index(op.f('ix_categories_name'), table_name='categories')
|
||||||
|
op.drop_index(op.f('ix_categories_id'), table_name='categories')
|
||||||
|
op.drop_table('categories')
|
||||||
|
op.drop_index(op.f('ix_users_id'), table_name='users')
|
||||||
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
||||||
|
op.drop_table('users')
|
1
app/__init__.py
Normal file
1
app/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Small Business Inventory System
|
1
app/api/__init__.py
Normal file
1
app/api/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# API Package
|
1
app/api/endpoints/__init__.py
Normal file
1
app/api/endpoints/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# API Endpoints
|
31
app/api/endpoints/auth.py
Normal file
31
app/api/endpoints/auth.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import user
|
||||||
|
from app.schemas.auth import Token
|
||||||
|
from app.auth.auth_handler import create_access_token
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/login", response_model=Token)
|
||||||
|
def login_for_access_token(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
):
|
||||||
|
user_obj = user.authenticate(
|
||||||
|
db, email=form_data.username, password=form_data.password
|
||||||
|
)
|
||||||
|
if not user_obj:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail="Incorrect email or password",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
access_token = create_access_token(
|
||||||
|
data={"sub": user_obj.email}, expires_delta=access_token_expires
|
||||||
|
)
|
||||||
|
return {"access_token": access_token, "token_type": "bearer"}
|
78
app/api/endpoints/categories.py
Normal file
78
app/api/endpoints/categories.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
from typing import List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import category
|
||||||
|
from app.schemas.category import Category, CategoryCreate, CategoryUpdate
|
||||||
|
from app.auth.auth_handler import get_current_active_user
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/", response_model=Category)
|
||||||
|
def create_category(
|
||||||
|
*,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
category_in: CategoryCreate,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_category = category.get_by_name(db, name=category_in.name)
|
||||||
|
if db_category:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Category with this name already exists"
|
||||||
|
)
|
||||||
|
return category.create(db, obj_in=category_in)
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[Category])
|
||||||
|
def read_categories(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
return category.get_multi(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@router.get("/{category_id}", response_model=Category)
|
||||||
|
def read_category(
|
||||||
|
category_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_category = category.get(db, id=category_id)
|
||||||
|
if not db_category:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Category not found"
|
||||||
|
)
|
||||||
|
return db_category
|
||||||
|
|
||||||
|
@router.put("/{category_id}", response_model=Category)
|
||||||
|
def update_category(
|
||||||
|
category_id: int,
|
||||||
|
category_in: CategoryUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_category = category.get(db, id=category_id)
|
||||||
|
if not db_category:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Category not found"
|
||||||
|
)
|
||||||
|
return category.update(db, db_obj=db_category, obj_in=category_in)
|
||||||
|
|
||||||
|
@router.delete("/{category_id}")
|
||||||
|
def delete_category(
|
||||||
|
category_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_category = category.get(db, id=category_id)
|
||||||
|
if not db_category:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Category not found"
|
||||||
|
)
|
||||||
|
category.remove(db, id=category_id)
|
||||||
|
return {"message": "Category deleted successfully"}
|
138
app/api/endpoints/inventory.py
Normal file
138
app/api/endpoints/inventory.py
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import inventory_item
|
||||||
|
from app.schemas.inventory_item import InventoryItem, InventoryItemCreate, InventoryItemUpdate
|
||||||
|
from app.auth.auth_handler import get_current_active_user
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/", response_model=InventoryItem)
|
||||||
|
def create_inventory_item(
|
||||||
|
*,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
item_in: InventoryItemCreate,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_item = inventory_item.get_by_sku(db, sku=item_in.sku)
|
||||||
|
if db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Item with this SKU already exists"
|
||||||
|
)
|
||||||
|
|
||||||
|
if item_in.barcode:
|
||||||
|
db_item_by_barcode = inventory_item.get_by_barcode(db, barcode=item_in.barcode)
|
||||||
|
if db_item_by_barcode:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Item with this barcode already exists"
|
||||||
|
)
|
||||||
|
|
||||||
|
return inventory_item.create(db, obj_in=item_in)
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[InventoryItem])
|
||||||
|
def read_inventory_items(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
search: Optional[str] = Query(None, description="Search by item name"),
|
||||||
|
category_id: Optional[int] = Query(None, description="Filter by category ID"),
|
||||||
|
supplier_id: Optional[int] = Query(None, description="Filter by supplier ID"),
|
||||||
|
low_stock_only: bool = Query(False, description="Show only low stock items"),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if low_stock_only:
|
||||||
|
return inventory_item.get_low_stock_items(db)
|
||||||
|
elif search:
|
||||||
|
return inventory_item.search_by_name(db, name=search, skip=skip, limit=limit)
|
||||||
|
elif category_id:
|
||||||
|
return inventory_item.get_by_category(db, category_id=category_id, skip=skip, limit=limit)
|
||||||
|
elif supplier_id:
|
||||||
|
return inventory_item.get_by_supplier(db, supplier_id=supplier_id, skip=skip, limit=limit)
|
||||||
|
else:
|
||||||
|
return inventory_item.get_multi(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@router.get("/low-stock", response_model=List[InventoryItem])
|
||||||
|
def get_low_stock_items(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
return inventory_item.get_low_stock_items(db)
|
||||||
|
|
||||||
|
@router.get("/{item_id}", response_model=InventoryItem)
|
||||||
|
def read_inventory_item(
|
||||||
|
item_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_item = inventory_item.get(db, id=item_id)
|
||||||
|
if not db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Inventory item not found"
|
||||||
|
)
|
||||||
|
return db_item
|
||||||
|
|
||||||
|
@router.get("/sku/{sku}", response_model=InventoryItem)
|
||||||
|
def read_inventory_item_by_sku(
|
||||||
|
sku: str,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_item = inventory_item.get_by_sku(db, sku=sku)
|
||||||
|
if not db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Inventory item not found"
|
||||||
|
)
|
||||||
|
return db_item
|
||||||
|
|
||||||
|
@router.put("/{item_id}", response_model=InventoryItem)
|
||||||
|
def update_inventory_item(
|
||||||
|
item_id: int,
|
||||||
|
item_in: InventoryItemUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_item = inventory_item.get(db, id=item_id)
|
||||||
|
if not db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Inventory item not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
if item_in.sku and item_in.sku != db_item.sku:
|
||||||
|
existing_item = inventory_item.get_by_sku(db, sku=item_in.sku)
|
||||||
|
if existing_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Item with this SKU already exists"
|
||||||
|
)
|
||||||
|
|
||||||
|
if item_in.barcode and item_in.barcode != db_item.barcode:
|
||||||
|
existing_item = inventory_item.get_by_barcode(db, barcode=item_in.barcode)
|
||||||
|
if existing_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Item with this barcode already exists"
|
||||||
|
)
|
||||||
|
|
||||||
|
return inventory_item.update(db, db_obj=db_item, obj_in=item_in)
|
||||||
|
|
||||||
|
@router.delete("/{item_id}")
|
||||||
|
def delete_inventory_item(
|
||||||
|
item_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_item = inventory_item.get(db, id=item_id)
|
||||||
|
if not db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Inventory item not found"
|
||||||
|
)
|
||||||
|
inventory_item.remove(db, id=item_id)
|
||||||
|
return {"message": "Inventory item deleted successfully"}
|
78
app/api/endpoints/suppliers.py
Normal file
78
app/api/endpoints/suppliers.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
from typing import List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import supplier
|
||||||
|
from app.schemas.supplier import Supplier, SupplierCreate, SupplierUpdate
|
||||||
|
from app.auth.auth_handler import get_current_active_user
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/", response_model=Supplier)
|
||||||
|
def create_supplier(
|
||||||
|
*,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
supplier_in: SupplierCreate,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_supplier = supplier.get_by_name(db, name=supplier_in.name)
|
||||||
|
if db_supplier:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Supplier with this name already exists"
|
||||||
|
)
|
||||||
|
return supplier.create(db, obj_in=supplier_in)
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[Supplier])
|
||||||
|
def read_suppliers(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
return supplier.get_multi(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@router.get("/{supplier_id}", response_model=Supplier)
|
||||||
|
def read_supplier(
|
||||||
|
supplier_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_supplier = supplier.get(db, id=supplier_id)
|
||||||
|
if not db_supplier:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Supplier not found"
|
||||||
|
)
|
||||||
|
return db_supplier
|
||||||
|
|
||||||
|
@router.put("/{supplier_id}", response_model=Supplier)
|
||||||
|
def update_supplier(
|
||||||
|
supplier_id: int,
|
||||||
|
supplier_in: SupplierUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_supplier = supplier.get(db, id=supplier_id)
|
||||||
|
if not db_supplier:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Supplier not found"
|
||||||
|
)
|
||||||
|
return supplier.update(db, db_obj=db_supplier, obj_in=supplier_in)
|
||||||
|
|
||||||
|
@router.delete("/{supplier_id}")
|
||||||
|
def delete_supplier(
|
||||||
|
supplier_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_supplier = supplier.get(db, id=supplier_id)
|
||||||
|
if not db_supplier:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Supplier not found"
|
||||||
|
)
|
||||||
|
supplier.remove(db, id=supplier_id)
|
||||||
|
return {"message": "Supplier deleted successfully"}
|
83
app/api/endpoints/transactions.py
Normal file
83
app/api/endpoints/transactions.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import inventory_transaction, inventory_item
|
||||||
|
from app.schemas.inventory_transaction import InventoryTransaction, InventoryTransactionCreate
|
||||||
|
from app.models.inventory_transaction import TransactionType
|
||||||
|
from app.auth.auth_handler import get_current_active_user
|
||||||
|
from app.models.user import User
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/", response_model=InventoryTransaction)
|
||||||
|
def create_inventory_transaction(
|
||||||
|
*,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
transaction_in: InventoryTransactionCreate,
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
# Verify the item exists
|
||||||
|
db_item = inventory_item.get(db, id=transaction_in.item_id)
|
||||||
|
if not db_item:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Inventory item not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create the transaction
|
||||||
|
db_transaction = inventory_transaction.create_with_user(
|
||||||
|
db, obj_in=transaction_in, user_id=current_user.id
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update inventory quantities based on transaction type
|
||||||
|
if transaction_in.transaction_type == TransactionType.IN:
|
||||||
|
new_quantity = db_item.quantity_in_stock + transaction_in.quantity
|
||||||
|
elif transaction_in.transaction_type == TransactionType.OUT:
|
||||||
|
new_quantity = db_item.quantity_in_stock - transaction_in.quantity
|
||||||
|
if new_quantity < 0:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Insufficient stock for this transaction"
|
||||||
|
)
|
||||||
|
else: # ADJUSTMENT
|
||||||
|
new_quantity = transaction_in.quantity
|
||||||
|
|
||||||
|
# Update the item's stock quantity
|
||||||
|
inventory_item.update(
|
||||||
|
db,
|
||||||
|
db_obj=db_item,
|
||||||
|
obj_in={"quantity_in_stock": new_quantity}
|
||||||
|
)
|
||||||
|
|
||||||
|
return db_transaction
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[InventoryTransaction])
|
||||||
|
def read_inventory_transactions(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
item_id: Optional[int] = Query(None, description="Filter by item ID"),
|
||||||
|
transaction_type: Optional[TransactionType] = Query(None, description="Filter by transaction type"),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if item_id:
|
||||||
|
return inventory_transaction.get_by_item(db, item_id=item_id, skip=skip, limit=limit)
|
||||||
|
elif transaction_type:
|
||||||
|
return inventory_transaction.get_by_type(db, transaction_type=transaction_type, skip=skip, limit=limit)
|
||||||
|
else:
|
||||||
|
return inventory_transaction.get_multi(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@router.get("/{transaction_id}", response_model=InventoryTransaction)
|
||||||
|
def read_inventory_transaction(
|
||||||
|
transaction_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
db_transaction = inventory_transaction.get(db, id=transaction_id)
|
||||||
|
if not db_transaction:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Transaction not found"
|
||||||
|
)
|
||||||
|
return db_transaction
|
92
app/api/endpoints/users.py
Normal file
92
app/api/endpoints/users.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
from typing import List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.crud import user
|
||||||
|
from app.schemas.user import User, UserCreate, UserUpdate
|
||||||
|
from app.auth.auth_handler import get_current_active_user
|
||||||
|
from app.models.user import User as UserModel
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/", response_model=User)
|
||||||
|
def create_user(
|
||||||
|
*,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
user_in: UserCreate,
|
||||||
|
current_user: UserModel = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
|
detail="Not enough permissions"
|
||||||
|
)
|
||||||
|
|
||||||
|
db_user = user.get_by_email(db, email=user_in.email)
|
||||||
|
if db_user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="User with this email already exists"
|
||||||
|
)
|
||||||
|
return user.create(db, obj_in=user_in)
|
||||||
|
|
||||||
|
@router.get("/", response_model=List[User])
|
||||||
|
def read_users(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
current_user: UserModel = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if not current_user.is_admin:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
|
detail="Not enough permissions"
|
||||||
|
)
|
||||||
|
return user.get_multi(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
@router.get("/me", response_model=User)
|
||||||
|
def read_user_me(
|
||||||
|
current_user: UserModel = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
return current_user
|
||||||
|
|
||||||
|
@router.get("/{user_id}", response_model=User)
|
||||||
|
def read_user(
|
||||||
|
user_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserModel = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if not current_user.is_admin and current_user.id != user_id:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
|
detail="Not enough permissions"
|
||||||
|
)
|
||||||
|
|
||||||
|
db_user = user.get(db, id=user_id)
|
||||||
|
if not db_user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="User not found"
|
||||||
|
)
|
||||||
|
return db_user
|
||||||
|
|
||||||
|
@router.put("/{user_id}", response_model=User)
|
||||||
|
def update_user(
|
||||||
|
user_id: int,
|
||||||
|
user_in: UserUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserModel = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
|
if not current_user.is_admin and current_user.id != user_id:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
|
detail="Not enough permissions"
|
||||||
|
)
|
||||||
|
|
||||||
|
db_user = user.get(db, id=user_id)
|
||||||
|
if not db_user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="User not found"
|
||||||
|
)
|
||||||
|
return user.update(db, db_obj=db_user, obj_in=user_in)
|
11
app/api/routes.py
Normal file
11
app/api/routes.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
from app.api.endpoints import auth, users, categories, suppliers, inventory, transactions
|
||||||
|
|
||||||
|
api_router = APIRouter()
|
||||||
|
|
||||||
|
api_router.include_router(auth.router, prefix="/auth", tags=["Authentication"])
|
||||||
|
api_router.include_router(users.router, prefix="/users", tags=["Users"])
|
||||||
|
api_router.include_router(categories.router, prefix="/categories", tags=["Categories"])
|
||||||
|
api_router.include_router(suppliers.router, prefix="/suppliers", tags=["Suppliers"])
|
||||||
|
api_router.include_router(inventory.router, prefix="/inventory", tags=["Inventory"])
|
||||||
|
api_router.include_router(transactions.router, prefix="/transactions", tags=["Transactions"])
|
3
app/auth/__init__.py
Normal file
3
app/auth/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from .auth_handler import get_password_hash, verify_password, create_access_token, get_current_user
|
||||||
|
|
||||||
|
__all__ = ["get_password_hash", "verify_password", "create_access_token", "get_current_user"]
|
62
app/auth/auth_handler.py
Normal file
62
app/auth/auth_handler.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
from datetime import datetime, timedelta
|
||||||
|
from typing import Optional
|
||||||
|
from fastapi import Depends, HTTPException, status
|
||||||
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||||
|
from jose import JWTError, jwt
|
||||||
|
from passlib.context import CryptContext
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.db.session import get_db
|
||||||
|
from app.models.user import User
|
||||||
|
from app.schemas.auth import TokenData
|
||||||
|
|
||||||
|
# Password hashing
|
||||||
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
|
|
||||||
|
# HTTP Bearer token
|
||||||
|
security = HTTPBearer()
|
||||||
|
|
||||||
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||||
|
return pwd_context.verify(plain_password, hashed_password)
|
||||||
|
|
||||||
|
def get_password_hash(password: str) -> str:
|
||||||
|
return pwd_context.hash(password)
|
||||||
|
|
||||||
|
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||||
|
to_encode = data.copy()
|
||||||
|
if expires_delta:
|
||||||
|
expire = datetime.utcnow() + expires_delta
|
||||||
|
else:
|
||||||
|
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
to_encode.update({"exp": expire})
|
||||||
|
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||||
|
return encoded_jwt
|
||||||
|
|
||||||
|
async def get_current_user(
|
||||||
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
) -> User:
|
||||||
|
credentials_exception = HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail="Could not validate credentials",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
payload = jwt.decode(credentials.credentials, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||||
|
email: str = payload.get("sub")
|
||||||
|
if email is None:
|
||||||
|
raise credentials_exception
|
||||||
|
token_data = TokenData(email=email)
|
||||||
|
except JWTError:
|
||||||
|
raise credentials_exception
|
||||||
|
|
||||||
|
user = db.query(User).filter(User.email == token_data.email).first()
|
||||||
|
if user is None:
|
||||||
|
raise credentials_exception
|
||||||
|
return user
|
||||||
|
|
||||||
|
async def get_current_active_user(current_user: User = Depends(get_current_user)) -> User:
|
||||||
|
if not current_user.is_active:
|
||||||
|
raise HTTPException(status_code=400, detail="Inactive user")
|
||||||
|
return current_user
|
23
app/core/config.py
Normal file
23
app/core/config.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
class Settings:
|
||||||
|
PROJECT_NAME: str = "Small Business Inventory System"
|
||||||
|
VERSION: str = "1.0.0"
|
||||||
|
DESCRIPTION: str = "A comprehensive inventory management system for small businesses"
|
||||||
|
|
||||||
|
# Database
|
||||||
|
DB_DIR = Path("/app/storage/db")
|
||||||
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
|
||||||
|
# Security
|
||||||
|
SECRET_KEY: str = os.getenv("SECRET_KEY", "your-secret-key-change-this-in-production")
|
||||||
|
ALGORITHM: str = "HS256"
|
||||||
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
||||||
|
|
||||||
|
# Admin user (for initial setup)
|
||||||
|
ADMIN_EMAIL: str = os.getenv("ADMIN_EMAIL", "admin@example.com")
|
||||||
|
ADMIN_PASSWORD: str = os.getenv("ADMIN_PASSWORD", "admin123")
|
||||||
|
|
||||||
|
settings = Settings()
|
7
app/crud/__init__.py
Normal file
7
app/crud/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from .user import user
|
||||||
|
from .category import category
|
||||||
|
from .supplier import supplier
|
||||||
|
from .inventory_item import inventory_item
|
||||||
|
from .inventory_transaction import inventory_transaction
|
||||||
|
|
||||||
|
__all__ = ["user", "category", "supplier", "inventory_item", "inventory_transaction"]
|
55
app/crud/base.py
Normal file
55
app/crud/base.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
ModelType = TypeVar("ModelType", bound=Base)
|
||||||
|
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
||||||
|
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
||||||
|
|
||||||
|
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||||
|
def __init__(self, model: Type[ModelType]):
|
||||||
|
self.model = model
|
||||||
|
|
||||||
|
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
||||||
|
return db.query(self.model).filter(self.model.id == id).first()
|
||||||
|
|
||||||
|
def get_multi(
|
||||||
|
self, db: Session, *, skip: int = 0, limit: int = 100
|
||||||
|
) -> List[ModelType]:
|
||||||
|
return db.query(self.model).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
||||||
|
obj_in_data = jsonable_encoder(obj_in)
|
||||||
|
db_obj = self.model(**obj_in_data)
|
||||||
|
db.add(db_obj)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_obj)
|
||||||
|
return db_obj
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self,
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
db_obj: ModelType,
|
||||||
|
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
||||||
|
) -> ModelType:
|
||||||
|
obj_data = jsonable_encoder(db_obj)
|
||||||
|
if isinstance(obj_in, dict):
|
||||||
|
update_data = obj_in
|
||||||
|
else:
|
||||||
|
update_data = obj_in.dict(exclude_unset=True)
|
||||||
|
for field in obj_data:
|
||||||
|
if field in update_data:
|
||||||
|
setattr(db_obj, field, update_data[field])
|
||||||
|
db.add(db_obj)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_obj)
|
||||||
|
return db_obj
|
||||||
|
|
||||||
|
def remove(self, db: Session, *, id: int) -> ModelType:
|
||||||
|
obj = db.query(self.model).get(id)
|
||||||
|
db.delete(obj)
|
||||||
|
db.commit()
|
||||||
|
return obj
|
11
app/crud/category.py
Normal file
11
app/crud/category.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.crud.base import CRUDBase
|
||||||
|
from app.models.category import Category
|
||||||
|
from app.schemas.category import CategoryCreate, CategoryUpdate
|
||||||
|
|
||||||
|
class CRUDCategory(CRUDBase[Category, CategoryCreate, CategoryUpdate]):
|
||||||
|
def get_by_name(self, db: Session, *, name: str) -> Optional[Category]:
|
||||||
|
return db.query(Category).filter(Category.name == name).first()
|
||||||
|
|
||||||
|
category = CRUDCategory(Category)
|
34
app/crud/inventory_item.py
Normal file
34
app/crud/inventory_item.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.crud.base import CRUDBase
|
||||||
|
from app.models.inventory_item import InventoryItem
|
||||||
|
from app.schemas.inventory_item import InventoryItemCreate, InventoryItemUpdate
|
||||||
|
|
||||||
|
class CRUDInventoryItem(CRUDBase[InventoryItem, InventoryItemCreate, InventoryItemUpdate]):
|
||||||
|
def get_by_sku(self, db: Session, *, sku: str) -> Optional[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(InventoryItem.sku == sku).first()
|
||||||
|
|
||||||
|
def get_by_barcode(self, db: Session, *, barcode: str) -> Optional[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(InventoryItem.barcode == barcode).first()
|
||||||
|
|
||||||
|
def get_low_stock_items(self, db: Session) -> List[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(
|
||||||
|
InventoryItem.quantity_in_stock <= InventoryItem.minimum_stock_level
|
||||||
|
).all()
|
||||||
|
|
||||||
|
def search_by_name(self, db: Session, *, name: str, skip: int = 0, limit: int = 100) -> List[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(
|
||||||
|
InventoryItem.name.contains(name)
|
||||||
|
).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def get_by_category(self, db: Session, *, category_id: int, skip: int = 0, limit: int = 100) -> List[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(
|
||||||
|
InventoryItem.category_id == category_id
|
||||||
|
).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def get_by_supplier(self, db: Session, *, supplier_id: int, skip: int = 0, limit: int = 100) -> List[InventoryItem]:
|
||||||
|
return db.query(InventoryItem).filter(
|
||||||
|
InventoryItem.supplier_id == supplier_id
|
||||||
|
).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
inventory_item = CRUDInventoryItem(InventoryItem)
|
28
app/crud/inventory_transaction.py
Normal file
28
app/crud/inventory_transaction.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from typing import List
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.crud.base import CRUDBase
|
||||||
|
from app.models.inventory_transaction import InventoryTransaction, TransactionType
|
||||||
|
from app.schemas.inventory_transaction import InventoryTransactionCreate
|
||||||
|
|
||||||
|
class CRUDInventoryTransaction(CRUDBase[InventoryTransaction, InventoryTransactionCreate, InventoryTransactionCreate]):
|
||||||
|
def get_by_item(self, db: Session, *, item_id: int, skip: int = 0, limit: int = 100) -> List[InventoryTransaction]:
|
||||||
|
return db.query(InventoryTransaction).filter(
|
||||||
|
InventoryTransaction.item_id == item_id
|
||||||
|
).order_by(InventoryTransaction.created_at.desc()).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def get_by_type(self, db: Session, *, transaction_type: TransactionType, skip: int = 0, limit: int = 100) -> List[InventoryTransaction]:
|
||||||
|
return db.query(InventoryTransaction).filter(
|
||||||
|
InventoryTransaction.transaction_type == transaction_type
|
||||||
|
).order_by(InventoryTransaction.created_at.desc()).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create_with_user(self, db: Session, *, obj_in: InventoryTransactionCreate, user_id: int) -> InventoryTransaction:
|
||||||
|
db_obj = InventoryTransaction(
|
||||||
|
**obj_in.dict(),
|
||||||
|
user_id=user_id
|
||||||
|
)
|
||||||
|
db.add(db_obj)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_obj)
|
||||||
|
return db_obj
|
||||||
|
|
||||||
|
inventory_transaction = CRUDInventoryTransaction(InventoryTransaction)
|
11
app/crud/supplier.py
Normal file
11
app/crud/supplier.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.crud.base import CRUDBase
|
||||||
|
from app.models.supplier import Supplier
|
||||||
|
from app.schemas.supplier import SupplierCreate, SupplierUpdate
|
||||||
|
|
||||||
|
class CRUDSupplier(CRUDBase[Supplier, SupplierCreate, SupplierUpdate]):
|
||||||
|
def get_by_name(self, db: Session, *, name: str) -> Optional[Supplier]:
|
||||||
|
return db.query(Supplier).filter(Supplier.name == name).first()
|
||||||
|
|
||||||
|
supplier = CRUDSupplier(Supplier)
|
38
app/crud/user.py
Normal file
38
app/crud/user.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.crud.base import CRUDBase
|
||||||
|
from app.models.user import User
|
||||||
|
from app.schemas.user import UserCreate, UserUpdate
|
||||||
|
from app.auth.auth_handler import get_password_hash, verify_password
|
||||||
|
|
||||||
|
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
||||||
|
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
||||||
|
return db.query(User).filter(User.email == email).first()
|
||||||
|
|
||||||
|
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
||||||
|
db_obj = User(
|
||||||
|
email=obj_in.email,
|
||||||
|
hashed_password=get_password_hash(obj_in.password),
|
||||||
|
full_name=obj_in.full_name,
|
||||||
|
is_active=obj_in.is_active,
|
||||||
|
)
|
||||||
|
db.add(db_obj)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_obj)
|
||||||
|
return db_obj
|
||||||
|
|
||||||
|
def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]:
|
||||||
|
user = self.get_by_email(db, email=email)
|
||||||
|
if not user:
|
||||||
|
return None
|
||||||
|
if not verify_password(password, user.hashed_password):
|
||||||
|
return None
|
||||||
|
return user
|
||||||
|
|
||||||
|
def is_active(self, user: User) -> bool:
|
||||||
|
return user.is_active
|
||||||
|
|
||||||
|
def is_admin(self, user: User) -> bool:
|
||||||
|
return user.is_admin
|
||||||
|
|
||||||
|
user = CRUDUser(User)
|
3
app/db/base.py
Normal file
3
app/db/base.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
Base = declarative_base()
|
17
app/db/session.py
Normal file
17
app/db/session.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
settings.SQLALCHEMY_DATABASE_URL,
|
||||||
|
connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
7
app/models/__init__.py
Normal file
7
app/models/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from .user import User
|
||||||
|
from .category import Category
|
||||||
|
from .supplier import Supplier
|
||||||
|
from .inventory_item import InventoryItem
|
||||||
|
from .inventory_transaction import InventoryTransaction
|
||||||
|
|
||||||
|
__all__ = ["User", "Category", "Supplier", "InventoryItem", "InventoryTransaction"]
|
16
app/models/category.py
Normal file
16
app/models/category.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Text, DateTime
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
class Category(Base):
|
||||||
|
__tablename__ = "categories"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, unique=True, index=True, nullable=False)
|
||||||
|
description = Column(Text)
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
|
||||||
|
# Relationship
|
||||||
|
items = relationship("InventoryItem", back_populates="category")
|
39
app/models/inventory_item.py
Normal file
39
app/models/inventory_item.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Text, Float, ForeignKey, DateTime
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
class InventoryItem(Base):
|
||||||
|
__tablename__ = "inventory_items"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, index=True, nullable=False)
|
||||||
|
description = Column(Text)
|
||||||
|
sku = Column(String, unique=True, index=True, nullable=False)
|
||||||
|
barcode = Column(String, unique=True, index=True)
|
||||||
|
|
||||||
|
# Pricing
|
||||||
|
cost_price = Column(Float, nullable=False, default=0.0)
|
||||||
|
selling_price = Column(Float, nullable=False, default=0.0)
|
||||||
|
|
||||||
|
# Stock management
|
||||||
|
quantity_in_stock = Column(Integer, nullable=False, default=0)
|
||||||
|
minimum_stock_level = Column(Integer, nullable=False, default=0)
|
||||||
|
maximum_stock_level = Column(Integer)
|
||||||
|
|
||||||
|
# Foreign Keys
|
||||||
|
category_id = Column(Integer, ForeignKey("categories.id"))
|
||||||
|
supplier_id = Column(Integer, ForeignKey("suppliers.id"))
|
||||||
|
|
||||||
|
# Timestamps
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
category = relationship("Category", back_populates="items")
|
||||||
|
supplier = relationship("Supplier", back_populates="items")
|
||||||
|
transactions = relationship("InventoryTransaction", back_populates="item")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_low_stock(self):
|
||||||
|
return self.quantity_in_stock <= self.minimum_stock_level
|
32
app/models/inventory_transaction.py
Normal file
32
app/models/inventory_transaction.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Text, Float, ForeignKey, DateTime, Enum
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.db.base import Base
|
||||||
|
import enum
|
||||||
|
|
||||||
|
class TransactionType(enum.Enum):
|
||||||
|
IN = "in"
|
||||||
|
OUT = "out"
|
||||||
|
ADJUSTMENT = "adjustment"
|
||||||
|
|
||||||
|
class InventoryTransaction(Base):
|
||||||
|
__tablename__ = "inventory_transactions"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
transaction_type = Column(Enum(TransactionType), nullable=False)
|
||||||
|
quantity = Column(Integer, nullable=False)
|
||||||
|
unit_cost = Column(Float)
|
||||||
|
total_cost = Column(Float)
|
||||||
|
reference_number = Column(String, index=True)
|
||||||
|
notes = Column(Text)
|
||||||
|
|
||||||
|
# Foreign Keys
|
||||||
|
item_id = Column(Integer, ForeignKey("inventory_items.id"), nullable=False)
|
||||||
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||||
|
|
||||||
|
# Timestamps
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
item = relationship("InventoryItem", back_populates="transactions")
|
||||||
|
user = relationship("User")
|
19
app/models/supplier.py
Normal file
19
app/models/supplier.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Text, DateTime
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
class Supplier(Base):
|
||||||
|
__tablename__ = "suppliers"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, unique=True, index=True, nullable=False)
|
||||||
|
contact_person = Column(String)
|
||||||
|
email = Column(String)
|
||||||
|
phone = Column(String)
|
||||||
|
address = Column(Text)
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
|
||||||
|
# Relationship
|
||||||
|
items = relationship("InventoryItem", back_populates="supplier")
|
15
app/models/user.py
Normal file
15
app/models/user.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Boolean, DateTime
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
email = Column(String, unique=True, index=True, nullable=False)
|
||||||
|
hashed_password = Column(String, nullable=False)
|
||||||
|
full_name = Column(String, nullable=False)
|
||||||
|
is_active = Column(Boolean, default=True)
|
||||||
|
is_admin = Column(Boolean, default=False)
|
||||||
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
15
app/schemas/__init__.py
Normal file
15
app/schemas/__init__.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from .user import User, UserCreate, UserUpdate
|
||||||
|
from .category import Category, CategoryCreate, CategoryUpdate
|
||||||
|
from .supplier import Supplier, SupplierCreate, SupplierUpdate
|
||||||
|
from .inventory_item import InventoryItem, InventoryItemCreate, InventoryItemUpdate
|
||||||
|
from .inventory_transaction import InventoryTransaction, InventoryTransactionCreate
|
||||||
|
from .auth import Token, TokenData
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"User", "UserCreate", "UserUpdate",
|
||||||
|
"Category", "CategoryCreate", "CategoryUpdate",
|
||||||
|
"Supplier", "SupplierCreate", "SupplierUpdate",
|
||||||
|
"InventoryItem", "InventoryItemCreate", "InventoryItemUpdate",
|
||||||
|
"InventoryTransaction", "InventoryTransactionCreate",
|
||||||
|
"Token", "TokenData"
|
||||||
|
]
|
13
app/schemas/auth.py
Normal file
13
app/schemas/auth.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class Token(BaseModel):
|
||||||
|
access_token: str
|
||||||
|
token_type: str
|
||||||
|
|
||||||
|
class TokenData(BaseModel):
|
||||||
|
email: Optional[str] = None
|
||||||
|
|
||||||
|
class UserLogin(BaseModel):
|
||||||
|
email: str
|
||||||
|
password: str
|
22
app/schemas/category.py
Normal file
22
app/schemas/category.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class CategoryBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
class CategoryCreate(CategoryBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CategoryUpdate(BaseModel):
|
||||||
|
name: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
class Category(CategoryBase):
|
||||||
|
id: int
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: Optional[datetime] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
45
app/schemas/inventory_item.py
Normal file
45
app/schemas/inventory_item.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
from app.schemas.category import Category
|
||||||
|
from app.schemas.supplier import Supplier
|
||||||
|
|
||||||
|
class InventoryItemBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
sku: str
|
||||||
|
barcode: Optional[str] = None
|
||||||
|
cost_price: float = 0.0
|
||||||
|
selling_price: float = 0.0
|
||||||
|
quantity_in_stock: int = 0
|
||||||
|
minimum_stock_level: int = 0
|
||||||
|
maximum_stock_level: Optional[int] = None
|
||||||
|
category_id: Optional[int] = None
|
||||||
|
supplier_id: Optional[int] = None
|
||||||
|
|
||||||
|
class InventoryItemCreate(InventoryItemBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InventoryItemUpdate(BaseModel):
|
||||||
|
name: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
sku: Optional[str] = None
|
||||||
|
barcode: Optional[str] = None
|
||||||
|
cost_price: Optional[float] = None
|
||||||
|
selling_price: Optional[float] = None
|
||||||
|
quantity_in_stock: Optional[int] = None
|
||||||
|
minimum_stock_level: Optional[int] = None
|
||||||
|
maximum_stock_level: Optional[int] = None
|
||||||
|
category_id: Optional[int] = None
|
||||||
|
supplier_id: Optional[int] = None
|
||||||
|
|
||||||
|
class InventoryItem(InventoryItemBase):
|
||||||
|
id: int
|
||||||
|
is_low_stock: bool
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: Optional[datetime] = None
|
||||||
|
category: Optional[Category] = None
|
||||||
|
supplier: Optional[Supplier] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
24
app/schemas/inventory_transaction.py
Normal file
24
app/schemas/inventory_transaction.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
from app.models.inventory_transaction import TransactionType
|
||||||
|
|
||||||
|
class InventoryTransactionBase(BaseModel):
|
||||||
|
transaction_type: TransactionType
|
||||||
|
quantity: int
|
||||||
|
unit_cost: Optional[float] = None
|
||||||
|
total_cost: Optional[float] = None
|
||||||
|
reference_number: Optional[str] = None
|
||||||
|
notes: Optional[str] = None
|
||||||
|
item_id: int
|
||||||
|
|
||||||
|
class InventoryTransactionCreate(InventoryTransactionBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InventoryTransaction(InventoryTransactionBase):
|
||||||
|
id: int
|
||||||
|
user_id: int
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
28
app/schemas/supplier.py
Normal file
28
app/schemas/supplier.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from pydantic import BaseModel, EmailStr
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class SupplierBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
contact_person: Optional[str] = None
|
||||||
|
email: Optional[EmailStr] = None
|
||||||
|
phone: Optional[str] = None
|
||||||
|
address: Optional[str] = None
|
||||||
|
|
||||||
|
class SupplierCreate(SupplierBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class SupplierUpdate(BaseModel):
|
||||||
|
name: Optional[str] = None
|
||||||
|
contact_person: Optional[str] = None
|
||||||
|
email: Optional[EmailStr] = None
|
||||||
|
phone: Optional[str] = None
|
||||||
|
address: Optional[str] = None
|
||||||
|
|
||||||
|
class Supplier(SupplierBase):
|
||||||
|
id: int
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: Optional[datetime] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
26
app/schemas/user.py
Normal file
26
app/schemas/user.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from pydantic import BaseModel, EmailStr
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
email: EmailStr
|
||||||
|
full_name: str
|
||||||
|
is_active: bool = True
|
||||||
|
|
||||||
|
class UserCreate(UserBase):
|
||||||
|
password: str
|
||||||
|
|
||||||
|
class UserUpdate(BaseModel):
|
||||||
|
email: Optional[EmailStr] = None
|
||||||
|
full_name: Optional[str] = None
|
||||||
|
password: Optional[str] = None
|
||||||
|
is_active: Optional[bool] = None
|
||||||
|
|
||||||
|
class User(UserBase):
|
||||||
|
id: int
|
||||||
|
is_admin: bool
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: Optional[datetime] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
53
main.py
Normal file
53
main.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from app.api.routes import api_router
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.db.session import engine
|
||||||
|
from app.db.base import Base
|
||||||
|
|
||||||
|
# Create database tables
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title=settings.PROJECT_NAME,
|
||||||
|
version=settings.VERSION,
|
||||||
|
description=settings.DESCRIPTION,
|
||||||
|
openapi_url="/openapi.json",
|
||||||
|
docs_url="/docs",
|
||||||
|
redoc_url="/redoc"
|
||||||
|
)
|
||||||
|
|
||||||
|
# CORS configuration
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Include API routes
|
||||||
|
app.include_router(api_router, prefix="/api/v1")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def root():
|
||||||
|
return JSONResponse(content={
|
||||||
|
"title": settings.PROJECT_NAME,
|
||||||
|
"version": settings.VERSION,
|
||||||
|
"description": settings.DESCRIPTION,
|
||||||
|
"documentation": "/docs",
|
||||||
|
"health_check": "/health"
|
||||||
|
})
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def health_check():
|
||||||
|
return JSONResponse(content={
|
||||||
|
"status": "healthy",
|
||||||
|
"service": settings.PROJECT_NAME,
|
||||||
|
"version": settings.VERSION
|
||||||
|
})
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
9
requirements.txt
Normal file
9
requirements.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fastapi==0.104.1
|
||||||
|
uvicorn[standard]==0.24.0
|
||||||
|
sqlalchemy==2.0.23
|
||||||
|
pydantic==2.5.0
|
||||||
|
python-multipart==0.0.6
|
||||||
|
python-jose[cryptography]==3.3.0
|
||||||
|
passlib[bcrypt]==1.7.4
|
||||||
|
alembic==1.12.1
|
||||||
|
ruff==0.1.6
|
Loading…
x
Reference in New Issue
Block a user