from typing import List from sqlalchemy.orm import Session from sqlalchemy import func from sqlalchemy.sql.elements import Null from app.crud.base import CRUDBase from app.models.nft import Nft from app.schemas.nft import NftBase, NftCreate, NftUpdate # noqa class CRUDNft(CRUDBase[Nft, NftBase, NftCreate]): def get_multi_by_owner( self, db: Session, *, skip: int = 0, limit: int = 100, owner_id: str ) -> List[Nft]: return db.query(Nft).filter( Nft.userid == owner_id).offset(skip).limit(limit).all() def get_group_by_title( self, db: Session, *, skip: int = 0, limit: int = 100 ) -> List[Nft]: return db.query(Nft, func.count(Nft.userid).label('count'))\ .group_by(Nft.title).offset(skip).limit(limit).all() def get_user_by_title( self, db: Session, *, skip: int = 0, limit: int = 100, title: str ) -> List[Nft]: return db.query(Nft.userid).filter(Nft.title == title, Nft.userid != None)\ .offset(skip).limit(limit).all() # noqa def create_with_owner( self, db: Session, *, obj_in: NftCreate, owner_id: str ) -> Nft: db_obj = Nft( hash=obj_in.hash, imgurl=obj_in.imgurl, userid=owner_id, title=obj_in.title, context=obj_in.context, is_active=obj_in.is_active, category=obj_in.category ) db.add(db_obj) db.commit() db.refresh(db_obj) return db_obj nft = CRUDNft(Nft)