items.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. encoding: UTF-8
  3. @author:clx
  4. @file:items.py
  5. @time:2023/5/30 6:08
  6. """
  7. from typing import Dict
  8. from fastapi import APIRouter, HTTPException
  9. from models.model import Item
  10. router = APIRouter()
  11. res = [
  12. {"id": 1, "name": "jack"},
  13. {"id": 2, "name": "mark"},
  14. {"id": 3, "name": "tom"},
  15. {"id": 4, "name": "sery"}
  16. ]
  17. @router.get("/item/res")
  18. async def get_res():
  19. return res
  20. @router.get("/item/res/{res_id}")
  21. async def get_res_id(res_id:int):
  22. for data in res:
  23. if data["id"] == res_id:
  24. return data
  25. @router.post("/item/res")
  26. async def create_res(item:Item):
  27. res.append(item.dict())
  28. return res
  29. @router.put("/item/res/{res_id}")
  30. async def update_res(res_id:int,item:Item):
  31. for data in res:
  32. print(data["id"])
  33. print(res_id)
  34. if data["id"] == res_id:
  35. res.remove(data)
  36. data = item.dict()
  37. res.append(data)
  38. return {"status":True}
  39. else:
  40. return HTTPException(status_code=404, detail="id:{} not found.".format(res_id))
  41. @router.delete("/item/res/{res_id}")
  42. async def delete_res(res_id:int):
  43. for data in res:
  44. if data["id"] == res_id:
  45. res.remove(data)
  46. return {"status":True}