def buy_it_now(self, db: Session, db_obj: Auction, buyer_id: int):
     db_obj.current_bid_amount = db_obj.bid_cap
     db_obj.final_winner_id = buyer_id
     db.add(db_obj)
     db.commit()
     db.refresh(db_obj)
     self.end(db, db_obj=db_obj)
    def start(self,
              db: Session,
              db_obj: Auction,
              ending_date,
              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])
            db_obj.starting_date = starting_date
        else:
            db_obj.starting_date = datetime.now()

        # schedule ending
        sched.add_job(EnglishAuction.end_auction,
                      'date',
                      run_date=ending_date,
                      args=[auction_id])

        db_obj.current_bid_amount = db_obj.starting_amount
        db_obj.ending_date = ending_date
        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 bid(self, db: Session, db_obj: Auction, amount: float, bidder_id: int):
        if not self.is_valid_bid(db_obj=db_obj, amount=amount):
            raise INVALID_BID_EXCEPTION

        if db_obj.state != AuctionState.ONGOING:
            raise AUCTION_NOT_STARTED_EXCEPTION

        # creating new bid object
        new_bid = bid_repo.create(db,
                                  obj_in=BidCreate(amount=amount,
                                                   bidder_id=bidder_id))

        db_obj.bids.append(new_bid)
        db_obj.last_bid_at = datetime.now()
        db_obj.winning_bid_id = new_bid.id
        db_obj.bid_count += 1

        if db_obj.bid_cap and amount >= db_obj.bid_cap:
            db_obj.current_bid_amount = db_obj.bid_cap
            # db_obj.final_winner_id = bidder_id
            db.add(db_obj)
            db.commit()
            db.refresh(db_obj)
            self.end(db, db_obj=db_obj)
        else:
            db_obj.current_bid_amount = amount + INC_AMT
            db.add(db_obj)
            db.commit()
            db.refresh(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 bid(self, db: Session, db_obj: Auction, amount: float, bidder_id: int):
        if not self.is_valid_bid(db_obj=db_obj, amount=amount):
            raise INVALID_BID_EXCEPTION

        new_bid = bid_repo.create(db,
                                  obj_in=BidCreate(amount=amount,
                                                   bidder_id=bidder_id))
        db_obj.bids.append(new_bid)
        db_obj.last_bid_at = datetime.now()
        db_obj.winning_bid_id = new_bid.id

        if db_obj.bid_cap and amount >= db_obj.bid_cap:
            db_obj.current_bid_amount = db_obj.bid_cap
            db_obj.final_winner_id = bidder_id
            db.add(db_obj)
            db.commit()
            db.refresh(db_obj)
            self.end(db, db_obj=db_obj)
        else:
            db_obj.current_bid_amount = amount + INC_AMT
            db.add(db_obj)
            db.commit()
            db.refresh(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