
- Set up FastAPI application with CORS support - Configure SQLite database connection - Create database models for users, clients, invoices, and line items - Set up Alembic for database migrations - Implement JWT-based authentication system - Create basic CRUD endpoints for users, clients, and invoices - Add PDF generation functionality - Implement activity logging - Update README with project information
66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
from typing import List, Optional, Dict, Any, Union
|
|
from sqlalchemy.orm import Session
|
|
from app.models.client import Client
|
|
from app.schemas.client import ClientCreate, ClientUpdate
|
|
|
|
|
|
def get_by_id(db: Session, client_id: str, user_id: str) -> Optional[Client]:
|
|
"""
|
|
Get a client by ID, ensuring it belongs to the specified user.
|
|
"""
|
|
return db.query(Client).filter(
|
|
Client.id == client_id, Client.user_id == user_id
|
|
).first()
|
|
|
|
|
|
def get_multi_by_user(
|
|
db: Session, user_id: str, skip: int = 0, limit: int = 100
|
|
) -> List[Client]:
|
|
"""
|
|
Get multiple clients for a user.
|
|
"""
|
|
return db.query(Client).filter(Client.user_id == user_id).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create(db: Session, *, obj_in: ClientCreate, user_id: str) -> Client:
|
|
"""
|
|
Create a new client.
|
|
"""
|
|
db_obj = Client(
|
|
**obj_in.dict(),
|
|
user_id=user_id,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def update(
|
|
db: Session, *, db_obj: Client, obj_in: Union[ClientUpdate, Dict[str, Any]]
|
|
) -> Client:
|
|
"""
|
|
Update a client.
|
|
"""
|
|
update_data = obj_in if isinstance(obj_in, dict) else obj_in.dict(exclude_unset=True)
|
|
|
|
for field, value in update_data.items():
|
|
if hasattr(db_obj, field) and value is not None:
|
|
setattr(db_obj, field, value)
|
|
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def remove(db: Session, *, client_id: str, user_id: str) -> Optional[Client]:
|
|
"""
|
|
Delete a client.
|
|
"""
|
|
client = get_by_id(db, client_id=client_id, user_id=user_id)
|
|
if not client:
|
|
return None
|
|
db.delete(client)
|
|
db.commit()
|
|
return client |