38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
from models import DealTag
|
||
from repositories import DealTagRepository, DealRepository
|
||
from schemas.deal_tag import *
|
||
from services.mixins import *
|
||
|
||
|
||
class DealTagService(
|
||
ServiceCrudMixin[DealTag, DealTagSchema, CreateDealTagRequest, UpdateDealTagRequest]
|
||
):
|
||
schema_class = DealTagSchema
|
||
entity_deleted_msg = "Тег успешно удален"
|
||
entity_updated_msg = "Тег успешно обновлен"
|
||
entity_created_msg = "Тег успешно создан"
|
||
|
||
def __init__(self, session: AsyncSession):
|
||
self.repository = DealTagRepository(session)
|
||
|
||
async def switch_tag(self, request: SwitchDealTagRequest) -> SwitchDealTagResponse:
|
||
tag: DealTag = await self.repository.get_by_id(request.tag_id)
|
||
if request.deal_id:
|
||
deal = await DealRepository(self.repository.session).get_by_id(
|
||
request.deal_id
|
||
)
|
||
await self.repository.switch_tag_in_deal(tag, deal)
|
||
else:
|
||
deals = await DealRepository(self.repository.session).get_by_group_id(
|
||
request.group_id
|
||
)
|
||
await self.repository.switch_tag_in_deals(tag, deals)
|
||
|
||
return SwitchDealTagResponse(ok=True, message="Успешно")
|
||
|
||
async def get_tag_colors(self) -> GetTagColorsResponse:
|
||
colors = await self.repository.get_tag_colors()
|
||
return GetTagColorsResponse(
|
||
items=[DealTagColorSchema.model_validate(color) for color in colors]
|
||
)
|