from typing import List, Optional from sqlalchemy.orm import Session, joinedload from app.crud.base import CRUDBase from app.models.comment import Comment from app.schemas.comment import CommentCreate, CommentUpdate class CRUDComment(CRUDBase[Comment, CommentCreate, CommentUpdate]): def create_with_author( self, db: Session, *, obj_in: CommentCreate, author_id: int ) -> Comment: obj_in_data = obj_in.model_dump() db_obj = Comment(**obj_in_data, author_id=author_id) db.add(db_obj) db.commit() db.refresh(db_obj) return db_obj def get_multi_by_author( self, db: Session, *, author_id: int, skip: int = 0, limit: int = 100 ) -> List[Comment]: return ( db.query(Comment) .filter(Comment.author_id == author_id) .offset(skip) .limit(limit) .all() ) def get_multi_by_post( self, db: Session, *, post_id: int, skip: int = 0, limit: int = 100 ) -> List[Comment]: return ( db.query(Comment) .filter(Comment.post_id == post_id) .offset(skip) .limit(limit) .all() ) def get_with_author(self, db: Session, *, id: int) -> Optional[Comment]: return ( db.query(Comment) .options(joinedload(Comment.author)) .filter(Comment.id == id) .first() ) def get_multi_with_author( self, db: Session, *, skip: int = 0, limit: int = 100 ) -> List[Comment]: return ( db.query(Comment) .options(joinedload(Comment.author)) .offset(skip) .limit(limit) .all() ) comment = CRUDComment(Comment)