86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from database import get_db
|
|
from app.core.auth import get_current_user
|
|
from app.modules.clients.models import Client
|
|
from app.modules.clients.repository import ClientRepository
|
|
from app.modules.clients.schema import ClientCreate, ClientUpdate, ClientResponse
|
|
from app.modules.clients.service import ClientService
|
|
from app.pipelines.crud import CreatePipe, ReadPipe, UpdatePipe, SoftDelete, ListPipe
|
|
from typing import List
|
|
|
|
router = APIRouter(prefix="/clients", tags=["clients"])
|
|
|
|
@router.post("/", response_model=ClientResponse)
|
|
async def create_client(
|
|
client_data: ClientCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
result = await ClientService.create_client(db, client_data, current_user)
|
|
if result["success"]:
|
|
return result["data"]
|
|
raise HTTPException(status_code=400, detail=result["error"])
|
|
|
|
@router.get("/{client_id}", response_model=ClientResponse)
|
|
async def get_client(
|
|
client_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
repo = ClientRepository(db)
|
|
pipeline = ReadPipe(db, current_user, Client, repo)
|
|
pipeline.set_input_data({"id": client_id})
|
|
result = await pipeline.execute()
|
|
if result["success"]:
|
|
return result["data"]
|
|
else:
|
|
raise HTTPException(status_code=404, detail=result["error"])
|
|
|
|
@router.put("/{client_id}", response_model=ClientResponse)
|
|
async def update_client(
|
|
client_id: int,
|
|
client_data: ClientUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
repo = ClientRepository(db)
|
|
pipeline = UpdatePipe(db, current_user, Client, repo)
|
|
pipeline.set_input_data({"id": client_id, **client_data.dict(exclude_unset=True)})
|
|
result = await pipeline.execute()
|
|
if result["success"]:
|
|
return result["data"]
|
|
else:
|
|
raise HTTPException(status_code=400, detail=result["error"])
|
|
|
|
@router.delete("/{client_id}")
|
|
async def delete_client(
|
|
client_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
repo = ClientRepository(db)
|
|
pipeline = SoftDelete(db, current_user, Client, repo)
|
|
pipeline.set_input_data({"id": client_id})
|
|
result = await pipeline.execute()
|
|
if result["success"]:
|
|
return result["data"]
|
|
else:
|
|
raise HTTPException(status_code=400, detail=result["error"])
|
|
|
|
@router.get("/", response_model=List[ClientResponse])
|
|
async def list_clients(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
repo = ClientRepository(db)
|
|
pipeline = ListPipe(db, current_user, Client, repo)
|
|
pipeline.set_input_data({"skip": skip, "limit": limit})
|
|
result = await pipeline.execute()
|
|
if result["success"]:
|
|
return result["data"]
|
|
else:
|
|
raise HTTPException(status_code=400, detail=result["error"])
|