選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

route_auction.py 2.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from fastapi import Depends, APIRouter, HTTPException, status
  2. from sqlalchemy.orm import Session
  3. from db.session import get_db
  4. from schemas.auction import ShowAuction, CreateAuction
  5. from typing import List
  6. from db.repository.auction import (
  7. get_all_auctions,
  8. get_auction_by_id,
  9. create_new_auction,
  10. edit_auction_by_id,
  11. delete_auction_by_id,
  12. )
  13. from db.models.user import User
  14. from apis.v1.route_auth import get_current_user
  15. router = APIRouter()
  16. @router.get("/", response_model=List[ShowAuction], status_code=status.HTTP_200_OK)
  17. def getAllAuctions(
  18. db: Session = Depends(get_db),
  19. current_user: User = Depends(get_current_user),
  20. ):
  21. auctions = get_all_auctions(db)
  22. return auctions
  23. @router.get("/{id}", response_model=ShowAuction, status_code=status.HTTP_200_OK)
  24. def getAuction(
  25. id: int,
  26. db: Session = Depends(get_db),
  27. current_user: User = Depends(get_current_user),
  28. ):
  29. auction = get_auction_by_id(id, db)
  30. return auction
  31. @router.post("/", status_code=status.HTTP_201_CREATED)
  32. def create_auction(
  33. auction: CreateAuction,
  34. db: Session = Depends(get_db),
  35. current_user: User = Depends(get_current_user),
  36. ):
  37. if current_user.Role != "Admin":
  38. raise HTTPException(
  39. status_code=403, detail="You are not authorized to perform this action"
  40. )
  41. auction_res = create_new_auction(auction=auction, createId=current_user.Id, db=db)
  42. if auction_res == "novehicle":
  43. raise HTTPException(
  44. status_code=404, detail="Vehicle with this ID does not exist"
  45. )
  46. elif auction_res == "vehicleunavailable":
  47. raise HTTPException(
  48. status_code=404, detail="Vehicle with this ID is currently not up for sale"
  49. )
  50. return auction_res
  51. @router.patch("/{auc_id}", status_code=status.HTTP_200_OK)
  52. def editAuction(
  53. auc_id: int,
  54. auction: CreateAuction,
  55. db: Session = Depends(get_db),
  56. current_user: User = Depends(get_current_user),
  57. ):
  58. if current_user.Role != "Admin":
  59. raise HTTPException(
  60. status_code=403, detail="You are not authorized to perform this action"
  61. )
  62. auction_res = edit_auction_by_id(id=auc_id, auction=auction, db=db)
  63. if auction_res == "noauction":
  64. raise HTTPException(
  65. status_code=404, detail="Auction with this ID does not exist"
  66. )
  67. return auction_res
  68. @router.delete("/{auc_id}", status_code=status.HTTP_200_OK)
  69. def deleteAuction(
  70. auc_id: int,
  71. db: Session = Depends(get_db),
  72. current_user: User = Depends(get_current_user),
  73. ):
  74. if current_user.Role != "Admin":
  75. raise HTTPException(
  76. status_code=403, detail="You are not authorized to perform this action"
  77. )
  78. auction_res = delete_auction_by_id(id=auc_id, db=db)
  79. if auction_res == "noauction":
  80. raise HTTPException(
  81. status_code=404, detail="Auction with this ID does not exist"
  82. )
  83. return auction_res