Spaces:
Running
Running
sghorbal commited on
Commit ·
d1bf220
1
Parent(s): 15c5f17
add repositories
Browse files- src/repository/match_repo.py +38 -0
- src/repository/player_repo.py +11 -0
src/repository/match_repo.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from src.entity.match import Match
|
| 4 |
+
from src.repository import player_repo
|
| 5 |
+
|
| 6 |
+
def insert_match(db: Session, match: Match) -> Optional[Match]:
|
| 7 |
+
"""
|
| 8 |
+
Insert a match into the database
|
| 9 |
+
"""
|
| 10 |
+
# Check if players exist
|
| 11 |
+
winner = player_repo.find_player_by_name(db, match.winner.name)
|
| 12 |
+
loser = player_repo.find_player_by_name(db, match.loser.name)
|
| 13 |
+
|
| 14 |
+
# Create new players if they don't exist
|
| 15 |
+
if not winner:
|
| 16 |
+
winner = match.winner
|
| 17 |
+
db.add(winner)
|
| 18 |
+
else:
|
| 19 |
+
match.winner = winner
|
| 20 |
+
|
| 21 |
+
if not loser:
|
| 22 |
+
loser = match.loser
|
| 23 |
+
db.add(loser)
|
| 24 |
+
else:
|
| 25 |
+
match.loser = loser
|
| 26 |
+
|
| 27 |
+
# Add the match
|
| 28 |
+
db.add(match)
|
| 29 |
+
|
| 30 |
+
# Add the odds
|
| 31 |
+
for odds in match.odds:
|
| 32 |
+
odds.match = match
|
| 33 |
+
db.add(odds)
|
| 34 |
+
|
| 35 |
+
db.commit()
|
| 36 |
+
db.refresh(match)
|
| 37 |
+
|
| 38 |
+
return match
|
src/repository/player_repo.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sqlalchemy.orm import Session
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from src.entity.match import Match
|
| 4 |
+
from src.entity.player import Player
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def find_player_by_name(db: Session, name: str) -> Optional[Match]:
|
| 8 |
+
"""
|
| 9 |
+
Find a player by his name
|
| 10 |
+
"""
|
| 11 |
+
return db.query(Player).filter(Player.name == name).first()
|