Automated Action 4cfc9775ae Create betting application API with FastAPI and SQLite
- Set up project structure with FastAPI and SQLite
- Implement user authentication with JWT
- Create database models for users, events, bets, and transactions
- Add API endpoints for user management
- Add API endpoints for events and betting functionality
- Add wallet management for deposits and withdrawals
- Configure Alembic for database migrations
- Add linting with Ruff
- Add documentation in README
2025-06-02 15:02:41 +00:00

109 lines
3.4 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.event import Event, EventStatus, Market, Outcome
from app.schemas.event import EventCreate, EventUpdate
def get_event(db: Session, event_id: int) -> Optional[Event]:
return db.query(Event).filter(Event.id == event_id).first()
def get_events(
db: Session,
skip: int = 0,
limit: int = 100,
status: Optional[EventStatus] = None,
) -> List[Event]:
query = db.query(Event)
if status:
query = query.filter(Event.status == status)
return query.offset(skip).limit(limit).all()
def create_event(db: Session, event_in: EventCreate) -> Event:
event_data = event_in.model_dump(exclude={"markets"})
db_event = Event(**event_data)
db.add(db_event)
db.commit()
db.refresh(db_event)
# Add markets
for market_in in event_in.markets:
market_data = market_in.model_dump(exclude={"outcomes"})
db_market = Market(**market_data, event_id=db_event.id)
db.add(db_market)
db.commit()
db.refresh(db_market)
# Add outcomes
for outcome_in in market_in.outcomes:
db_outcome = Outcome(**outcome_in.model_dump(), market_id=db_market.id)
db.add(db_outcome)
db.commit()
db.refresh(db_event)
return db_event
def update_event(db: Session, db_event: Event, event_in: EventUpdate) -> Event:
update_data = event_in.model_dump(exclude={"markets"}, exclude_unset=True)
for field, value in update_data.items():
setattr(db_event, field, value)
db.add(db_event)
db.commit()
db.refresh(db_event)
# Update markets if provided
if event_in.markets:
for market_update in event_in.markets:
market = next((m for m in db_event.markets if m.id == market_update.id), None)
if market:
market_data = market_update.model_dump(exclude={"outcomes"}, exclude_unset=True)
for field, value in market_data.items():
setattr(market, field, value)
db.add(market)
# Update outcomes if provided
if market_update.outcomes:
for outcome_update in market_update.outcomes:
outcome = next((o for o in market.outcomes if o.id == outcome_update.id), None)
if outcome:
for field, value in outcome_update.model_dump(exclude_unset=True).items():
setattr(outcome, field, value)
db.add(outcome)
db.commit()
db.refresh(db_event)
return db_event
def delete_event(db: Session, event_id: int) -> bool:
event = db.query(Event).filter(Event.id == event_id).first()
if not event:
return False
db.delete(event)
db.commit()
return True
def get_market(db: Session, market_id: int) -> Optional[Market]:
return db.query(Market).filter(Market.id == market_id).first()
def get_outcome(db: Session, outcome_id: int) -> Optional[Outcome]:
return db.query(Outcome).filter(Outcome.id == outcome_id).first()
def settle_outcome(
db: Session, outcome_id: int, is_winner: bool,
) -> Optional[Outcome]:
outcome = get_outcome(db, outcome_id)
if outcome:
outcome.is_winner = is_winner
db.add(outcome)
db.commit()
db.refresh(outcome)
return outcome