diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2025-04-27 22:41:14 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2025-04-27 22:41:14 -0600 |
commit | f32142947b853076889801913d47b8c2c0f4f456 (patch) | |
tree | 20981c0c2b79c2ba9cc58eece69591cfbe5b21ff /server/api/bids.py | |
parent | ba700c31fceb4554ccbb3181f0e5747fcf5c3259 (diff) |
format using black
Diffstat (limited to 'server/api/bids.py')
-rw-r--r-- | server/api/bids.py | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/server/api/bids.py b/server/api/bids.py index 6139987..c08c74b 100644 --- a/server/api/bids.py +++ b/server/api/bids.py @@ -8,8 +8,6 @@ from datetime import datetime, timezone from zoneinfo import ZoneInfo from typing import List, Optional -# TODO: Can you submit a bid for 2AM the next day after 11AM? - router = APIRouter() MARKET_TIMEZONES = { @@ -18,6 +16,7 @@ MARKET_TIMEZONES = { "MISO": ZoneInfo("America/Chicago"), } + def get_db(): db = SessionLocal() try: @@ -25,16 +24,21 @@ def get_db(): finally: db.close() + class BidBase(BaseModel): timestamp: datetime quantity: float price: float - user_id: int # In production the user_id should be obtained from the authenticated user + user_id: ( + int # In production the user_id should be obtained from the authenticated user + ) market: str + class BidCreate(BidBase): pass + class BidResponse(BidBase): id: int status: str @@ -43,14 +47,19 @@ class BidResponse(BidBase): class Config: from_attributes = True + @router.get("/", response_model=List[BidResponse]) def get_bids(db: Session = Depends(get_db)): return db.query(BidModel).all() + @router.post("/", response_model=BidResponse) def submit_bid(bid: BidCreate, db: Session = Depends(get_db)): if bid.market not in MARKET_TIMEZONES: - raise HTTPException(status_code=400, detail=f"Invalid market. Supported markets: {list(MARKET_TIMEZONES.keys())}") + raise HTTPException( + status_code=400, + detail=f"Invalid market. Supported markets: {list(MARKET_TIMEZONES.keys())}", + ) market_tz = MARKET_TIMEZONES[bid.market] @@ -68,19 +77,29 @@ def submit_bid(bid: BidCreate, db: Session = Depends(get_db)): if bid_day == today: if now > cutoff_time: - raise HTTPException(status_code=400, detail="Cannot submit bids for today after 11AM local time.") + raise HTTPException( + status_code=400, + detail="Cannot submit bids for today after 11AM local time.", + ) start_of_hour = bid.timestamp.replace(minute=0, second=0, microsecond=0) end_of_hour = start_of_hour.replace(minute=59, second=59, microsecond=999999) - bid_count = db.query(BidModel).filter( - BidModel.timestamp >= start_of_hour, - BidModel.timestamp <= end_of_hour, - BidModel.market == bid.market - ).count() + bid_count = ( + db.query(BidModel) + .filter( + BidModel.timestamp >= start_of_hour, + BidModel.timestamp <= end_of_hour, + BidModel.market == bid.market, + ) + .count() + ) if bid_count >= 10: - raise HTTPException(status_code=400, detail="Cannot submit more than 10 bids for this hour in this market.") + raise HTTPException( + status_code=400, + detail="Cannot submit more than 10 bids for this hour in this market.", + ) db_bid = BidModel( timestamp=bid.timestamp, @@ -89,7 +108,7 @@ def submit_bid(bid: BidCreate, db: Session = Depends(get_db)): user_id=bid.user_id, market=bid.market, status="Submitted", - pnl=None + pnl=None, ) db.add(db_bid) db.commit() |