Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

auction.py 2.1 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. print("Retrieving all auctions")
  8. auctions = db.query(Auction).all()
  9. for auction in auctions:
  10. auction.car = auction.vehicle
  11. return auctions
  12. def get_auction_by_id(id: int, db: Session):
  13. print("Retrieving auction by id: " + str(id))
  14. auction = db.query(Auction).filter(Auction.Id == id).first()
  15. if not auction:
  16. return None
  17. auction.car = auction.vehicle
  18. return auction
  19. def create_new_auction(auction: CreateAuction, createId: int, db: Session):
  20. print("Creating new auction" + str(auction) + " by user " + str(createId))
  21. vehicle = get_vehicle_by_id(auction.carId, db)
  22. if vehicle is None:
  23. return "novehicle"
  24. auction_object = Auction(
  25. initialPrice=auction.initialPrice,
  26. minimalBet=auction.minimalBet,
  27. carID=auction.carId,
  28. creatorId=createId,
  29. dateStart=auction.dateStart,
  30. dateEnd=auction.dateEnd,
  31. )
  32. db.add(auction_object)
  33. db.commit()
  34. db.refresh(auction_object)
  35. return auction_object
  36. def delete_auction_by_id(id: int, db: Session):
  37. print("Deleting auction by id: " + str(id))
  38. auction = db.query(Auction).filter(Auction.Id == id).first()
  39. if not auction:
  40. return "noauction"
  41. db.delete(auction)
  42. db.commit()
  43. return auction
  44. def edit_auction_by_id(id: int, auction: CreateAuction, db: Session):
  45. print("Editing auction by id: " + str(id) + " with data: " + str(auction))
  46. auction_db = db.query(Auction).filter(Auction.Id == id)
  47. auction_object = auction_db.first()
  48. if not auction_object:
  49. return "noauction"
  50. auction_object.initialPrice = auction.initialPrice
  51. auction_object.minimalBet = auction.minimalBet
  52. auction_object.carID = auction.carId
  53. auction_object.dateStart = auction.dateStart
  54. auction_object.dateEnd = auction.dateEnd
  55. db.add(auction_object)
  56. db.commit()
  57. db.refresh(auction_object)
  58. return auction_object