nft.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from typing import Any, List
  2. from fastapi import APIRouter, Depends, HTTPException
  3. from sqlalchemy.orm import Session
  4. from app import crud, models, schemas
  5. from app.api import deps
  6. router = APIRouter()
  7. @router.get("/", response_model=List[schemas.NftPrint])
  8. # @router.get("/")
  9. def read_items(
  10. db: Session = Depends(deps.get_db),
  11. skip: int = 0,
  12. limit: int = 100,
  13. current_user: models.users = Depends(deps.get_current_active_superuser),
  14. ) -> Any:
  15. """
  16. Retrieve items.
  17. """
  18. if crud.user.is_superuser(current_user):
  19. nfts = crud.nft.get_group_by_title(db, skip=skip, limit=limit)
  20. else:
  21. nfts = crud.nft.get_multi_by_owner(
  22. db=db, owner_id=current_user.userid, skip=skip, limit=limit
  23. )
  24. return nfts
  25. @router.post("/", response_model=schemas.NftBase)
  26. def create_item(
  27. *,
  28. db: Session = Depends(deps.get_db),
  29. item_in: schemas.NftCreate,
  30. current_user: models.users = Depends(deps.get_current_active_user),
  31. ) -> Any:
  32. """
  33. Create new item.
  34. """
  35. nft = crud.nft.create_with_owner(
  36. db=db, obj_in=item_in, owner_id=current_user.userid)
  37. return nft
  38. @router.put("/{id}", response_model=schemas.NftCreate)
  39. def update_item(
  40. *,
  41. db: Session = Depends(deps.get_db),
  42. id: int,
  43. item_in: schemas.NftUpdate,
  44. current_user: models.users = Depends(deps.get_current_active_user),
  45. ) -> Any:
  46. """
  47. Update an item.
  48. """
  49. nft = crud.nft.get(db=db, id=id)
  50. if not nft:
  51. raise HTTPException(status_code=404, detail="Item not found")
  52. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  53. raise HTTPException(status_code=400, detail="Not enough permissions")
  54. nft = crud.nft.update(db=db, db_obj=nft, obj_in=item_in)
  55. return nft
  56. @router.get("/{id}", response_model=schemas.NftCreate)
  57. def read_item(
  58. *,
  59. db: Session = Depends(deps.get_db),
  60. id: int,
  61. current_user: models.users = Depends(deps.get_current_active_user),
  62. ) -> Any:
  63. """
  64. Get item by ID.
  65. """
  66. nft = crud.nft.get(db=db, id=id)
  67. if not nft:
  68. raise HTTPException(status_code=404, detail="Item not found")
  69. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  70. raise HTTPException(status_code=400, detail="Not enough permissions")
  71. return nft
  72. # @router.post("/title", response_model=schemas.NftCreate)
  73. @router.post("/title")
  74. def read_user_by_title(
  75. *,
  76. db: Session = Depends(deps.get_db),
  77. title: str,
  78. skip: int = 0,
  79. limit: int = 100,
  80. current_user: models.users = Depends(deps.get_current_active_user),
  81. ) -> Any:
  82. """
  83. Get item by title.
  84. """
  85. nft = crud.nft.get_user_by_title(db=db, title=title, skip=skip, limit=limit)
  86. if not nft:
  87. raise HTTPException(status_code=404, detail="Item not found")
  88. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  89. raise HTTPException(status_code=400, detail="Not enough permissions")
  90. return nft
  91. @router.delete("/{id}", response_model=schemas.NftCreate)
  92. def delete_item(
  93. *,
  94. db: Session = Depends(deps.get_db),
  95. id: int,
  96. current_user: models.users = Depends(deps.get_current_active_user),
  97. ) -> Any:
  98. """
  99. Delete an item.
  100. """
  101. nft = crud.nft.get(db=db, id=id)
  102. if not nft:
  103. raise HTTPException(status_code=404, detail="Item not found")
  104. if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
  105. raise HTTPException(status_code=400, detail="Not enough permissions")
  106. nft = crud.nft.remove(db=db, id=id)
  107. return nft