Files

63 lines
1.8 KiB
Python
Raw Permalink Normal View History

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import List
import psycopg2
from psycopg2.extras import RealDictCursor
router = APIRouter()
def get_db_connection():
return psycopg2.connect(
host="localhost",
port=5432,
database="korobka_db",
user="postgres",
password="postgres"
)
class HeroDetail(BaseModel):
hero_id: int
team: int
order: int
class MatchData(BaseModel):
id: int
start_time: int
leagueid: int
radiant_team_id: int
dire_team_id: int
radiant_win: bool
heroes: List[HeroDetail]
@router.post("/match/pro/add")
def add_pro_match(match: MatchData):
conn = get_db_connection()
cursor = conn.cursor()
try:
# Добавляем матч в pro_matches
cursor.execute("""
INSERT INTO pro_matches (id, start_time, leagueid, radiant_team_id, dire_team_id, radiant_win)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT (id) DO NOTHING
""", (match.id, match.start_time, match.leagueid, match.radiant_team_id, match.dire_team_id, match.radiant_win))
# Добавляем детали героев в pro_details_match
for hero in match.heroes:
cursor.execute("""
INSERT INTO pro_details_match (match_id, hero_id, team, "order")
VALUES (%s, %s, %s, %s)
""", (match.id, hero.hero_id, hero.team, hero.order))
conn.commit()
cursor.close()
conn.close()
return {"status": "success", "message": f"Match {match.id} added successfully"}
except Exception as e:
conn.rollback()
cursor.close()
conn.close()
raise HTTPException(status_code=500, detail=str(e))