Skip to content

Commit

Permalink
feat(collections): add quotes count to collection model and schema
Browse files Browse the repository at this point in the history
zobweyt committed Jan 11, 2025
1 parent 67abd11 commit d45e090
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/api/collections/models.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions src/api/collections/schemas.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d45e090

Please sign in to comment.