crud_nft.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from typing import List
  2. from sqlalchemy.orm import Session
  3. from sqlalchemy import func
  4. from sqlalchemy.sql.elements import Null
  5. from app.crud.base import CRUDBase
  6. from app.models.nft import Nft
  7. from app.schemas.nft import NftBase, NftCreate, NftUpdate # noqa
  8. class CRUDNft(CRUDBase[Nft, NftBase, NftCreate]):
  9. def get_multi_by_owner(
  10. self, db: Session, *, skip: int = 0, limit: int = 100, owner_id: str
  11. ) -> List[Nft]:
  12. return db.query(Nft).filter(
  13. Nft.userid == owner_id).offset(skip).limit(limit).all()
  14. def get_group_by_title(
  15. self, db: Session, *, skip: int = 0, limit: int = 100
  16. ) -> List[Nft]:
  17. return db.query(Nft, func.count(Nft.userid).label('count'))\
  18. .group_by(Nft.title).offset(skip).limit(limit).all()
  19. def get_user_by_title(
  20. self, db: Session, *, skip: int = 0, limit: int = 100, title: str
  21. ) -> List[Nft]:
  22. return db.query(Nft.userid).filter(Nft.title == title, Nft.userid != None)\
  23. .offset(skip).limit(limit).all() # noqa
  24. def create_with_owner(
  25. self, db: Session, *, obj_in: NftCreate, owner_id: str
  26. ) -> Nft:
  27. db_obj = Nft(
  28. hash=obj_in.hash,
  29. imgurl=obj_in.imgurl,
  30. userid=owner_id,
  31. title=obj_in.title,
  32. context=obj_in.context,
  33. is_active=obj_in.is_active,
  34. category=obj_in.category
  35. )
  36. db.add(db_obj)
  37. db.commit()
  38. db.refresh(db_obj)
  39. return db_obj
  40. nft = CRUDNft(Nft)