47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import math
|
|
|
|
from models import Deal
|
|
from repositories import DealRepository
|
|
from schemas.base import PaginationSchema, SortingSchema
|
|
from schemas.deal import *
|
|
from services.mixins import *
|
|
|
|
|
|
class DealService(
|
|
ServiceCreateMixin[Deal, CreateDealRequest, DealSchema],
|
|
ServiceUpdateMixin[Deal, UpdateDealRequest],
|
|
ServiceDeleteMixin[Deal],
|
|
):
|
|
schema_class = DealSchema
|
|
entity_deleted_msg = "Сделка успешно удалена"
|
|
entity_updated_msg = "Сделка успешно обновлена"
|
|
entity_created_msg = "Сделка успешно создана"
|
|
|
|
def __init__(self, session: AsyncSession):
|
|
self.repository = DealRepository(session)
|
|
|
|
async def get_all(
|
|
self,
|
|
pagination: PaginationSchema,
|
|
sorting: SortingSchema,
|
|
*filters,
|
|
) -> GetDealsResponse:
|
|
deals, total_items = await self.repository.get_all(
|
|
pagination.page,
|
|
pagination.items_per_page,
|
|
sorting.field,
|
|
sorting.direction,
|
|
*filters,
|
|
)
|
|
|
|
total_pages = 1
|
|
if pagination.items_per_page:
|
|
total_pages = math.ceil(total_items / pagination.items_per_page)
|
|
|
|
return GetDealsResponse(
|
|
items=[DealSchema.model_validate(deal) for deal in deals],
|
|
pagination_info=PaginationInfoSchema(
|
|
total_pages=total_pages, total_items=total_items
|
|
),
|
|
)
|