Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

99 wiersze
3.0 KiB

  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. if auction is None:
  31. raise HTTPException(
  32. status_code=404, detail="Auction with this ID does not exist"
  33. )
  34. return auction
  35. @router.post("/", status_code=status.HTTP_201_CREATED)
  36. def create_auction(
  37. auction: CreateAuction,
  38. db: Session = Depends(get_db),
  39. current_user: User = Depends(get_current_user),
  40. ):
  41. if current_user.Role != "Admin":
  42. raise HTTPException(
  43. status_code=403, detail="You are not authorized to perform this action"
  44. )
  45. auction_res = create_new_auction(auction=auction, createId=current_user.Id, db=db)
  46. if auction_res == "novehicle":
  47. raise HTTPException(
  48. status_code=404, detail="Vehicle with this ID does not exist"
  49. )
  50. elif auction_res == "vehicleunavailable":
  51. raise HTTPException(
  52. status_code=404, detail="Vehicle with this ID is currently not up for sale"
  53. )
  54. return auction_res
  55. @router.patch("/{auc_id}", status_code=status.HTTP_200_OK)
  56. def editAuction(
  57. auc_id: int,
  58. auction: CreateAuction,
  59. db: Session = Depends(get_db),
  60. current_user: User = Depends(get_current_user),
  61. ):
  62. if current_user.Role != "Admin":
  63. raise HTTPException(
  64. status_code=403, detail="You are not authorized to perform this action"
  65. )
  66. auction_res = edit_auction_by_id(id=auc_id, auction=auction, db=db)
  67. if auction_res == "noauction":
  68. raise HTTPException(
  69. status_code=404, detail="Auction with this ID does not exist"
  70. )
  71. return auction_res
  72. @router.delete("/{auc_id}", status_code=status.HTTP_200_OK)
  73. def deleteAuction(
  74. auc_id: int,
  75. db: Session = Depends(get_db),
  76. current_user: User = Depends(get_current_user),
  77. ):
  78. if current_user.Role != "Admin":
  79. raise HTTPException(
  80. status_code=403, detail="You are not authorized to perform this action"
  81. )
  82. auction_res = delete_auction_by_id(id=auc_id, db=db)
  83. if auction_res == "noauction":
  84. raise HTTPException(
  85. status_code=404, detail="Auction with this ID does not exist"
  86. )
  87. return auction_res