items.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Item])
  8. def read_items(
  9. db: Session = Depends(deps.get_db),
  10. skip: int = 0,
  11. limit: int = 100,
  12. current_user: models.User = Depends(deps.get_current_active_user),
  13. ) -> Any:
  14. """
  15. Retrieve items.
  16. """
  17. if crud.user.is_superuser(current_user):
  18. items = crud.item.get_multi(db, skip=skip, limit=limit)
  19. else:
  20. items = crud.item.get_multi_by_owner(
  21. db=db, owner_id=current_user.id, skip=skip, limit=limit
  22. )
  23. return items
  24. @router.post("/", response_model=schemas.Item)
  25. def create_item(
  26. *,
  27. db: Session = Depends(deps.get_db),
  28. item_in: schemas.ItemCreate,
  29. current_user: models.User = Depends(deps.get_current_active_user),
  30. ) -> Any:
  31. """
  32. Create new item.
  33. """
  34. item = crud.item.create_with_owner(db=db, obj_in=item_in, owner_id=current_user.id)
  35. return item
  36. @router.put("/{id}", response_model=schemas.Item)
  37. def update_item(
  38. *,
  39. db: Session = Depends(deps.get_db),
  40. id: int,
  41. item_in: schemas.ItemUpdate,
  42. current_user: models.User = Depends(deps.get_current_active_user),
  43. ) -> Any:
  44. """
  45. Update an item.
  46. """
  47. item = crud.item.get(db=db, id=id)
  48. if not item:
  49. raise HTTPException(status_code=404, detail="Item not found")
  50. if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
  51. raise HTTPException(status_code=400, detail="Not enough permissions")
  52. item = crud.item.update(db=db, db_obj=item, obj_in=item_in)
  53. return item
  54. @router.get("/{id}", response_model=schemas.Item)
  55. def read_item(
  56. *,
  57. db: Session = Depends(deps.get_db),
  58. id: int,
  59. current_user: models.User = Depends(deps.get_current_active_user),
  60. ) -> Any:
  61. """
  62. Get item by ID.
  63. """
  64. item = crud.item.get(db=db, id=id)
  65. if not item:
  66. raise HTTPException(status_code=404, detail="Item not found")
  67. if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
  68. raise HTTPException(status_code=400, detail="Not enough permissions")
  69. return item
  70. @router.delete("/{id}", response_model=schemas.Item)
  71. def delete_item(
  72. *,
  73. db: Session = Depends(deps.get_db),
  74. id: int,
  75. current_user: models.User = Depends(deps.get_current_active_user),
  76. ) -> Any:
  77. """
  78. Delete an item.
  79. """
  80. item = crud.item.get(db=db, id=id)
  81. if not item:
  82. raise HTTPException(status_code=404, detail="Item not found")
  83. if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
  84. raise HTTPException(status_code=400, detail="Not enough permissions")
  85. item = crud.item.remove(db=db, id=id)
  86. return item