Files
new_lottery_bot/src/components/services.py
Trevor1985 cb35bb12e3
Some checks failed
continuous-integration/drone/push Build is failing
Stabilize staff concurrency and premium emoji; enable verified Drone deployment
2026-09-13 19:48:41 +09:00

67 lines
2.7 KiB
Python

from typing import List, Dict, Any, Optional
import random
from datetime import datetime, timezone
from src.interfaces.base import ILotteryService, IUserService
from src.interfaces.base import ILotteryRepository, IUserRepository, IParticipationRepository, IWinnerRepository
from src.core.models import Lottery, User
class LotteryServiceImpl(ILotteryService):
"""Реализация сервиса розыгрышей"""
def __init__(
self,
lottery_repo: ILotteryRepository,
participation_repo: IParticipationRepository,
winner_repo: IWinnerRepository,
user_repo: IUserRepository
):
self.lottery_repo = lottery_repo
self.participation_repo = participation_repo
self.winner_repo = winner_repo
self.user_repo = user_repo
async def create_lottery(self, title: str, description: str, prizes: List[str], creator_id: int) -> Lottery:
"""Создать новый розыгрыш"""
return await self.lottery_repo.create(
title=title,
description=description,
prizes=prizes,
creator_id=creator_id,
is_active=True,
is_completed=False,
created_at=datetime.now(timezone.utc)
)
async def conduct_draw(self, lottery_id):
from src.core.services import LotteryService
result = await LotteryService.conduct_draw(self.lottery_repo.session, lottery_id)
winners = await self.winner_repo.get_by_lottery(lottery_id) if result else []
return {str(place): dict(info, winner=next(w for w in winners if w.place == place))
for place, info in result.items()}
async def get_active_lotteries(self) -> List[Lottery]:
"""Получить активные розыгрыши"""
return await self.lottery_repo.get_active()
class UserServiceImpl(IUserService):
"""Реализация сервиса пользователей"""
def __init__(self, user_repo: IUserRepository):
self.user_repo = user_repo
async def get_or_create_user(self, telegram_id: int, **kwargs) -> User:
"""Получить или создать пользователя"""
from src.core.services import UserService
return await UserService.get_or_create_user(self.user_repo.session, telegram_id, **kwargs)
async def register_user(self, telegram_id: int, phone: str, club_card_number: str) -> bool:
from src.core.registration_services import RegistrationService
try:
await RegistrationService.register_user(self.user_repo.session, telegram_id, club_card_number, phone)
return True
except ValueError:
return False