- FastAPI REST API with JWT auth - aiogram 3 Telegram bot with admin middleware - APScheduler daily tasks (expiry, reminders, revoke, sync) - SQLAlchemy 2 async ORM with Alembic migrations - Jinja2 admin panel (Dashboard, Users, Payments, Servers, Tariffs) - VPN provider abstraction with MockProvider - Stats service with revenue/subscription analytics - Docker Compose (PostgreSQL + Redis + app) - Healthcheck endpoint
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
from sqlalchemy import select, update
|
|
|
|
from app.models.notification import Notification, NotificationType
|
|
from app.repositories.base import BaseRepository
|
|
|
|
|
|
class NotificationRepository(BaseRepository[Notification]):
|
|
def __init__(self, session):
|
|
super().__init__(session, Notification)
|
|
|
|
async def get_by_user_id(self, user_id: int) -> list[Notification]:
|
|
stmt = (
|
|
select(Notification)
|
|
.where(Notification.user_id == user_id)
|
|
.order_by(Notification.created_at.desc())
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
return list(result.scalars().all())
|
|
|
|
async def get_unread(self, user_id: int) -> list[Notification]:
|
|
stmt = (
|
|
select(Notification)
|
|
.where(Notification.user_id == user_id)
|
|
.where(Notification.is_read.is_(False))
|
|
.order_by(Notification.created_at.desc())
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
return list(result.scalars().all())
|
|
|
|
async def get_by_type(
|
|
self, user_id: int, type: NotificationType
|
|
) -> list[Notification]:
|
|
stmt = (
|
|
select(Notification)
|
|
.where(Notification.user_id == user_id)
|
|
.where(Notification.type == type)
|
|
.order_by(Notification.created_at.desc())
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
return list(result.scalars().all())
|
|
|
|
async def mark_as_read(self, notification_id: int) -> bool:
|
|
stmt = (
|
|
update(Notification)
|
|
.where(Notification.id == notification_id)
|
|
.values(is_read=True)
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
await self.session.flush()
|
|
return result.rowcount > 0
|
|
|
|
async def mark_all_as_read(self, user_id: int) -> int:
|
|
stmt = (
|
|
update(Notification)
|
|
.where(Notification.user_id == user_id)
|
|
.where(Notification.is_read.is_(False))
|
|
.values(is_read=True)
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
await self.session.flush()
|
|
return result.rowcount
|
|
|
|
async def count_unread(self, user_id: int) -> int:
|
|
stmt = (
|
|
select(Notification)
|
|
.where(Notification.user_id == user_id)
|
|
.where(Notification.is_read.is_(False))
|
|
)
|
|
result = await self.session.execute(stmt)
|
|
return len(result.scalars().all())
|