You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.8 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.auction import CreateAuction
  3. from db.models.auction import Auction
  4. from db.repository.user import get_user_by_id
  5. from db.repository.vehicle import get_vehicle_by_id
  6. def get_all_auctions(db: Session):
  7. auctions = db.query(Auction).all()
  8. for auction in auctions:
  9. auction.car = auction.vehicle
  10. return auctions
  11. def get_auction_by_id(id: int, db: Session):
  12. auction = db.query(Auction).filter(Auction.Id == id).first()
  13. auction.car = auction.vehicle
  14. return auction
  15. def create_new_auction(auction: CreateAuction, createId: int, db: Session):
  16. vehicle = get_vehicle_by_id(auction.carId, db)
  17. if vehicle is None:
  18. return "novehicle"
  19. auction_object = Auction(
  20. initialPrice=auction.initialPrice,
  21. minimalBet=auction.minimalBet,
  22. carID=auction.carId,
  23. creatorId=createId,
  24. dateStart=auction.dateStart,
  25. dateEnd=auction.dateEnd,
  26. )
  27. db.add(auction_object)
  28. db.commit()
  29. db.refresh(auction_object)
  30. return auction_object
  31. def delete_auction_by_id(id: int, db: Session):
  32. auction = db.query(Auction).filter(Auction.Id == id).first()
  33. if not auction:
  34. return "noauction"
  35. db.delete(auction)
  36. db.commit()
  37. return auction
  38. def edit_auction_by_id(id: int, auction: CreateAuction, db: Session):
  39. auction_db = db.query(Auction).filter(Auction.Id == id)
  40. auction_object = auction_db.first()
  41. if not auction_object:
  42. return "noauction"
  43. auction_object.initialPrice = auction.initialPrice
  44. auction_object.minimalBet = auction.minimalBet
  45. auction_object.carID = auction.carId
  46. auction_object.dateStart = auction.dateStart
  47. auction_object.dateEnd = auction.dateEnd
  48. db.add(auction_object)
  49. db.commit()
  50. db.refresh(auction_object)
  51. return auction_object