From a442119495fff5ad259f93362736d0fd51a6e530 Mon Sep 17 00:00:00 2001 From: Untone Date: Sun, 26 Nov 2023 15:07:33 +0300 Subject: [PATCH] total-fix --- resolvers/schema.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/resolvers/schema.py b/resolvers/schema.py index 3697c02..3092ffe 100644 --- a/resolvers/schema.py +++ b/resolvers/schema.py @@ -28,6 +28,7 @@ class Notification: class NotificationsQueryResult: notifications: list[Notification] unread: int + total: int @strawberry.type @@ -47,7 +48,6 @@ def notification_seen_by_viewer(viewer_id, notification_id, session): .filter(NotificationSeen.viewer == viewer_id, NotificationSeen.notification == notification_id) .first() ) - return seen is not None @@ -56,13 +56,12 @@ class Query: @strawberry.field @login_required async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult: - """загружаем уведомления""" user_id = info.context["user_id"] with local_session() as session: author = session.query(Author).filter(Author.user == user_id).first() - nslist = ( + query = ( session.query(Notification) .outerjoin( NotificationSeen, @@ -70,15 +69,13 @@ class Query: ) .limit(limit) .offset(offset) - .all() ) - for notification in nslist: - notification.seen_by_viewer = notification_seen_by_viewer(author.id, notification.id, session) - + nslist = query.all() + total = query.group_by(Notification.id).count() unread = sum(1 for n in nslist if not n.seen_by_viewer) - return NotificationsResult(notifications=nslist, unread=unread) + return NotificationsResult(notifications=nslist, unread=unread, total=total) @strawberry.type