
- 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
101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from typing import List
|
|
from sqlalchemy.orm import Session
|
|
from app.api.deps import get_db, get_current_user
|
|
from app.models.user import User
|
|
from app.schemas.client import Client, ClientCreate, ClientUpdate
|
|
from app.crud import crud_client
|
|
from app.core.logging import log_activity
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Client])
|
|
async def get_clients(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get all clients for the current user.
|
|
"""
|
|
clients = crud_client.get_multi_by_user(
|
|
db, user_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return clients
|
|
|
|
|
|
@router.post("/", response_model=Client, status_code=status.HTTP_201_CREATED)
|
|
async def create_client(
|
|
client_in: ClientCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Create a new client.
|
|
"""
|
|
client = crud_client.create(db, obj_in=client_in, user_id=current_user.id)
|
|
log_activity(current_user.id, "create", "client", client.id)
|
|
return client
|
|
|
|
|
|
@router.get("/{client_id}", response_model=Client)
|
|
async def get_client(
|
|
client_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get a client by ID.
|
|
"""
|
|
client = crud_client.get_by_id(db, client_id=client_id, user_id=current_user.id)
|
|
if not client:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Client not found",
|
|
)
|
|
return client
|
|
|
|
|
|
@router.put("/{client_id}", response_model=Client)
|
|
async def update_client(
|
|
client_id: str,
|
|
client_in: ClientUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Update a client.
|
|
"""
|
|
client = crud_client.get_by_id(db, client_id=client_id, user_id=current_user.id)
|
|
if not client:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Client not found",
|
|
)
|
|
|
|
client = crud_client.update(db, db_obj=client, obj_in=client_in)
|
|
log_activity(current_user.id, "update", "client", client.id)
|
|
return client
|
|
|
|
|
|
@router.delete("/{client_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
async def delete_client(
|
|
client_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Delete a client.
|
|
"""
|
|
client = crud_client.get_by_id(db, client_id=client_id, user_id=current_user.id)
|
|
if not client:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Client not found",
|
|
)
|
|
|
|
crud_client.remove(db, client_id=client_id, user_id=current_user.id)
|
|
log_activity(current_user.id, "delete", "client", client_id)
|
|
return None |