crud_nft.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
  2. from fastapi.encoders import jsonable_encoder
  3. from sqlalchemy.orm import Session
  4. from sqlalchemy import func
  5. from itertools import groupby
  6. from operator import attrgetter
  7. from pydantic import BaseModel
  8. from app.db.base_class import Base
  9. from app.crud.base import CRUDBase
  10. from app.models.nft import Nft
  11. from app.schemas.nft import NftBase, NftCreate, NftUpdate # noqa
  12. ModelType = TypeVar("ModelType", bound=Base)
  13. CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
  14. UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
  15. class CRUDNft(CRUDBase[Nft, NftBase, NftCreate]):
  16. def get_by_uid(
  17. self, db: Session, *, uid: int
  18. ) -> list[Nft]:
  19. return db.query(Nft).filter(Nft.uid == uid).filter(Nft.is_active == True).first()
  20. def get_by_title(
  21. self, db: Session, *, title: str
  22. ) -> list[Nft]:
  23. return db.query(Nft).filter(Nft.title == title).all()
  24. def get_multi_by_owner(
  25. self, db: Session, *, skip: int = 0, limit: int = 100, owner_id: str
  26. ) -> List[Nft]:
  27. return db.query(Nft).filter(
  28. Nft.userid == owner_id).offset(skip).limit(limit).all()
  29. def get_group_by_title(
  30. self, db: Session, *, skip: int = 0, limit: int = 100
  31. ) -> List[Nft]:
  32. # nft_list = db.query(Nft).order_by(Nft.title).all()
  33. # return {k: {"Nft": list(g), "count": db.query(Nft.title, func.count(Nft.userid)
  34. # .label('count')).group_by(Nft.title).filter(Nft.title == k).first()[1]}
  35. # for k, g in groupby(nft_list, attrgetter('title'))}
  36. return db.query(Nft, func.count(Nft.userid).label('count'))\
  37. .group_by(Nft.title).offset(skip).limit(limit).all()
  38. def get_user_by_title(
  39. self, db: Session, *, skip: int = 0, limit: int = 100, title: str
  40. ) -> List[Nft]:
  41. return db.query(Nft.userid).filter(Nft.title == title, Nft.userid != None)\
  42. .offset(skip).limit(limit).all() # noqa
  43. def create_with_owner(
  44. self, db: Session, *, obj_in: NftCreate, owner_id: str
  45. ) -> Nft:
  46. db_obj = Nft(
  47. hash=obj_in.hash,
  48. imgurl=obj_in.imgurl,
  49. userid=owner_id,
  50. title=obj_in.title,
  51. context=obj_in.context,
  52. is_active=obj_in.is_active,
  53. category=obj_in.category,
  54. uid = obj_in.uid
  55. )
  56. db.add(db_obj)
  57. db.commit()
  58. db.refresh(db_obj)
  59. return db_obj
  60. def update_bulk_title(
  61. self, db: Session, *, db_obj_list: list[NftUpdate],
  62. obj_in: NftUpdate
  63. ) -> Nft:
  64. for db_obj in db_obj_list:
  65. obj_data = jsonable_encoder(db_obj)
  66. if isinstance(obj_in, dict):
  67. update_data = obj_in
  68. else:
  69. update_data = obj_in.dict(exclude_unset=True)
  70. for field in obj_data:
  71. if field in update_data:
  72. setattr(db_obj, field, update_data[field])
  73. db.add(db_obj)
  74. db.commit()
  75. db.refresh(db_obj)
  76. return db_obj
  77. nft = CRUDNft(Nft)