Files
Crm-Backend/modules/fulfillment_base/services/product.py

51 lines
1.6 KiB
Python

import math
from modules.fulfillment_base.models import Product
from modules.fulfillment_base.repositories import ProductRepository
from modules.fulfillment_base.schemas.product import (
CreateProductRequest,
ProductSchema,
UpdateProductRequest, GetProductsResponse,
)
from schemas.base import PaginationSchema, PaginationInfoSchema
from services.mixins import *
class ProductService(
ServiceCreateMixin[Product, CreateProductRequest, ProductSchema],
ServiceUpdateMixin[Product, UpdateProductRequest],
ServiceDeleteMixin[Product],
):
schema_class = ProductSchema
entity_deleted_msg = "Товар успешно удален"
entity_updated_msg = "Товар успешно обновлен"
entity_created_msg = "Товар успешно создан"
def __init__(self, session: AsyncSession):
self.repository = ProductRepository(session)
async def get_all(
self,
pagination: PaginationSchema,
*filters,
) -> GetProductsResponse:
products, total_items = await self.repository.get_all(
pagination.page,
pagination.items_per_page,
*filters,
)
total_pages = 1
if pagination.items_per_page:
total_pages = math.ceil(total_items / pagination.items_per_page)
return GetProductsResponse(
items=[ProductSchema.model_validate(product) for product in products],
pagination_info=PaginationInfoSchema(
total_pages=total_pages, total_items=total_items
),
)
async def is_soft_delete(self, product: ProductSchema) -> bool:
return True