Files
Fulfillment-Backend/routers/client.py
2024-04-10 03:45:52 +03:00

88 lines
2.4 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 *
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()
@client_router.post(
'/create',
operation_id='create_client',
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)