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 * from services.auth import get_current_user, authorized_user from services.client import ClientService client_router = APIRouter( prefix="/client", tags=['client'], dependencies=[Depends(authorized_user)] ) @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() @client_router.get( '/get/{client_id}', operation_id='get_client', response_model=ClientGetResponse ) async def get_client( session: Annotated[AsyncSession, Depends(get_session)], client_id: int, ): return await ClientService(session).get_by_id(client_id) @client_router.post( '/create', operation_id='create_client_api', response_model=ClientCreateResponse ) async def create_client( request: ClientCreateRequest, user: Annotated[User, Depends(get_current_user)], session: Annotated[AsyncSession, Depends(get_session)] ): return await ClientService(session).create(request, user) @client_router.post( '/update', operation_id='update_client', response_model=ClientUpdateResponse ) async def update_client( request: ClientUpdateRequest, user: Annotated[User, Depends(get_current_user)], session: Annotated[AsyncSession, Depends(get_session)] ): return await ClientService(session).update(request, user) @client_router.post( '/delete', operation_id='delete_client', response_model=ClientDeleteResponse ) async def delete_client( request: ClientDeleteRequest, session: Annotated[AsyncSession, Depends(get_session)] ): return await ClientService(session).delete(request) @client_router.post( '/link/{client_id}', operation_id='client_link', response_model=CreateGuestUrlResponse, ) async def client_link( session: Annotated[AsyncSession, Depends(get_session)], client_id: int, ): return await ClientService(session).create_guest_url(client_id)