from sqlalchemy.orm import Session from schemas.auction import CreateAuction from db.models.auction import Auction from db.repository.user import get_user_by_id from db.repository.vehicle import get_vehicle_by_id def get_all_auctions(db: Session): print("Retrieving all auctions") auctions = db.query(Auction).all() for auction in auctions: auction.car = auction.vehicle return auctions def get_auction_by_id(id: int, db: Session): print("Retrieving auction by id: " + str(id)) auction = db.query(Auction).filter(Auction.Id == id).first() if not auction: return None auction.car = auction.vehicle return auction def create_new_auction(auction: CreateAuction, createId: int, db: Session): print("Creating new auction" + str(auction) + " by user " + str(createId)) vehicle = get_vehicle_by_id(auction.carId, db) if vehicle is None: return "novehicle" auction_object = Auction( initialPrice=auction.initialPrice, minimalBet=auction.minimalBet, carID=auction.carId, creatorId=createId, dateStart=auction.dateStart, dateEnd=auction.dateEnd, ) db.add(auction_object) db.commit() db.refresh(auction_object) return auction_object def delete_auction_by_id(id: int, db: Session): print("Deleting auction by id: " + str(id)) auction = db.query(Auction).filter(Auction.Id == id).first() if not auction: return "noauction" db.delete(auction) db.commit() return auction def edit_auction_by_id(id: int, auction: CreateAuction, db: Session): print("Editing auction by id: " + str(id) + " with data: " + str(auction)) auction_db = db.query(Auction).filter(Auction.Id == id) auction_object = auction_db.first() if not auction_object: return "noauction" auction_object.initialPrice = auction.initialPrice auction_object.minimalBet = auction.minimalBet auction_object.carID = auction.carId auction_object.dateStart = auction.dateStart auction_object.dateEnd = auction.dateEnd db.add(auction_object) db.commit() db.refresh(auction_object) return auction_object