47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from backend.session import get_session
|
|
from models import User
|
|
from schemas.client import ClientSearchRequest, ClientUpdateDetailsRequest, ClientUpdateDetailsResponse, \
|
|
ClientGetAllResponse
|
|
from services.auth import get_current_user
|
|
from services.client import ClientService
|
|
|
|
client_router = APIRouter(
|
|
prefix="/client",
|
|
tags=['client']
|
|
)
|
|
|
|
|
|
@client_router.get('/search', operation_id='search_clients')
|
|
async def search_clients(
|
|
name: str,
|
|
session: Annotated[AsyncSession, Depends(get_session)]
|
|
):
|
|
return await ClientService(session).search_clients(ClientSearchRequest(name=name))
|
|
|
|
|
|
@client_router.post('/update-details')
|
|
async def update_client_details(
|
|
session: Annotated[AsyncSession, Depends(get_session)],
|
|
user: Annotated[User, Depends(get_current_user)],
|
|
request: ClientUpdateDetailsRequest,
|
|
):
|
|
service = ClientService(session)
|
|
client = await service.get_by_id(request.client_id)
|
|
if not client:
|
|
return ClientUpdateDetailsResponse(ok=False)
|
|
await service.update_details(user, client, request.details)
|
|
await session.commit()
|
|
return ClientUpdateDetailsResponse(ok=True)
|
|
|
|
|
|
@client_router.get('/get-all', operation_id='get_all_clients', response_model=ClientGetAllResponse)
|
|
async def get_all_clients(
|
|
session: Annotated[AsyncSession, Depends(get_session)],
|
|
):
|
|
return await ClientService(session).get_all()
|