Files
Crm-Backend/services/deal.py

56 lines
1.6 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:
rows, 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)
deals = [
DealSchema(
**deal.__dict__,
products_quantity=products_quantity,
total_price=total_price,
)
for deal, total_price, products_quantity in rows
]
return GetDealsResponse(
items=deals,
pagination_info=PaginationInfoSchema(
total_pages=total_pages, total_items=total_items
),
)