Chore: Pushing changes to migrate from windows/wsl to fedora
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
"""
|
||||
The Client table
|
||||
The Clients endpoint
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from sqlmodel import Session, select
|
||||
from sqlmodel import select
|
||||
from app.api.deps import SessionDep, exists
|
||||
from app.schemas.models import Client
|
||||
from app.schemas.schemas import ClientCreate
|
||||
from typing import Sequence
|
||||
from app.schemas.schemas import ClientCreate, ClientUpdate
|
||||
from pydantic import ValidationError
|
||||
from typing import Sequence, List, Optional
|
||||
|
||||
router = APIRouter(prefix="/clients", tags=["clients"])
|
||||
|
||||
|
||||
@router.get("/", response_model=Client)
|
||||
def fetch_clients(client: Client, session: SessionDep) -> Sequence[Client]:
|
||||
@router.get("/", response_model=List[Client])
|
||||
def fetch_clients(session: SessionDep) -> Sequence[Client]:
|
||||
"""Fetch client list
|
||||
"""
|
||||
clients = session.exec(select(Client)).all()
|
||||
@@ -21,11 +22,66 @@ def fetch_clients(client: Client, session: SessionDep) -> Sequence[Client]:
|
||||
|
||||
@router.post("/", response_model=ClientCreate)
|
||||
def create_client(client_data: ClientCreate, session: SessionDep) -> Client:
|
||||
"""Create a client
|
||||
"""
|
||||
existing = exists(session, Client, tin_number=client_data.tin_number)
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Client with this tin number already exists")
|
||||
client = Client.model_validate(client_data)
|
||||
|
||||
try:
|
||||
client = Client.model_validate(client_data)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=e.errors())
|
||||
|
||||
session.add(client)
|
||||
session.commit()
|
||||
session.refresh(client)
|
||||
return client
|
||||
|
||||
|
||||
@router.get("/{client_id}", response_model=Client)
|
||||
def get_client(client_id: int, session: SessionDep) -> Optional[Client]:
|
||||
"""Returns a client by its ID
|
||||
"""
|
||||
stmt = select(Client).where(Client.id == client_id)
|
||||
result: Optional[Client] = session.exec(stmt).first()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/{client_id}", response_model=ClientCreate)
|
||||
def update_client(
|
||||
client_id: int,
|
||||
client_data: ClientUpdate,
|
||||
session: SessionDep) -> Optional[Client]:
|
||||
"""Updates a client using its ID
|
||||
"""
|
||||
client = session.get(Client, client_id)
|
||||
if not client:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
update_fields = client_data.model_dump(exclude_unset=True)
|
||||
|
||||
for key, value in update_fields.items():
|
||||
setattr(client, key, value)
|
||||
|
||||
session.add(client)
|
||||
session.commit()
|
||||
session.refresh(client)
|
||||
return client
|
||||
|
||||
|
||||
@router.delete("/{client_id}", status_code=204)
|
||||
def delete_client(client_id: int, session: SessionDep):
|
||||
"""Deletes a client
|
||||
"""
|
||||
client = session.get(Client, client_id)
|
||||
|
||||
if not client:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
session.delete(client)
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user