def start(self, db: Session, db_obj: Auction, starting_date=None) -> Auction: auction_id = db_obj.id if starting_date: # schedule start of auction sched.add_job(EnglishAuction.start_auction, 'date', run_date=starting_date, args=[auction_id]) # schedule ending sched.add_job(EnglishAuction.end_auction, 'date', run_date=db_obj.ending_date, args=[auction_id]) db_obj.current_bid_amount = db_obj.starting_amount db.add(db_obj) db.commit() db.refresh(db_obj) auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.ONGOING) return db_obj
def end(self, db: Session, db_obj: Auction) -> Auction: if db_obj.reserve and db_obj.current_bid_amount < db_obj.reserve: auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.CANCLED) else: if db_obj.winning_bid: db_obj.final_winner_id = db_obj.winning_bid.bidder_id db.add(db_obj) db.commit() db.refresh(db_obj) auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.ENDED) return db_obj
def end(self, db: Session, db_obj: Auction) -> Auction: if db_obj.reserve and db_obj.current_bid_amount < db_obj.reserve: # when reserve not reached auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.CANCLED) else: if db_obj.winning_bid: # when someone bid in the auction db_obj.final_winner_id = db_obj.winning_bid.bidder_id db.add(db_obj) db.commit() db.refresh(db_obj) auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.ENDED) notification_data = { 'title': f'You have won the auction of {db_obj.product.name}', 'sender_id': db_obj.owner_id, 'reciever_id': db_obj.final_winner_id } notification_repo.create(db=db, obj_in=notification_data) else: # when no one bid in the auction auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.CANCLED) return db_obj
def cancel(self, db: Session, db_obj: Auction): return auction_repo.change_state(db, db_obj=db_obj, state=AuctionState.CANCLED)