From d45e0909199a2915b2c2acdb49fbd7017a673810 Mon Sep 17 00:00:00 2001 From: zobweyt Date: Sat, 11 Jan 2025 22:12:36 +0300 Subject: [PATCH] feat(collections): add quotes count to collection model and schema --- src/api/collections/models.py | 8 +++++++- src/api/collections/schemas.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/api/collections/models.py b/src/api/collections/models.py index a5e5acb..0cb3324 100644 --- a/src/api/collections/models.py +++ b/src/api/collections/models.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, Optional from sqlalchemy import Enum, ForeignKey -from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.orm import Mapped, mapped_column, object_session, relationship from src.db.mixins import AuditMixin from src.db.models import Base @@ -35,4 +35,10 @@ class Visibility(enum.Enum): quotes: Mapped[list["Quote"]] = relationship(secondary="quote_collection", back_populates="collections") + @property + def quotes_count(self): + from src.api.quotes import Quote + session = object_session(self) + return session.query(Quote).with_parent(self).count() if session else 0 + __repr_attrs__ = ("id", "name", "visibility") diff --git a/src/api/collections/schemas.py b/src/api/collections/schemas.py index df01e1f..785662e 100644 --- a/src/api/collections/schemas.py +++ b/src/api/collections/schemas.py @@ -22,6 +22,7 @@ class CollectionResponse(BaseModel): description: str emote: str visibility: Collection.Visibility + quotes_count: int created_by_user_id: int created_at: datetime updated_at: datetime