Add endpoints for initial release
This commit is contained in:
parent
1efada3146
commit
d04dcce8e4
135
main.py
135
main.py
|
@ -7,10 +7,10 @@ from db import get_db_connection
|
|||
app = FastAPI()
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # Replace "*" with your allowed origins
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # Allow all HTTP methods
|
||||
allow_headers=["*"], # Allow all HTTP headers
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
class RegisterRequest(BaseModel):
|
||||
email: str
|
||||
|
@ -35,6 +35,15 @@ class CreateMatchRequest(BaseModel):
|
|||
class JoinMatchRequest(BaseModel):
|
||||
token: str
|
||||
match_id: int
|
||||
|
||||
class ProfileRequest(BaseModel):
|
||||
token: str
|
||||
|
||||
class EndMatchRequest(BaseModel):
|
||||
match_id: int
|
||||
player1_score: int
|
||||
player2_score: int
|
||||
|
||||
|
||||
@app.post("/register")
|
||||
def register(request: RegisterRequest):
|
||||
|
@ -184,3 +193,123 @@ def join_match(request: JoinMatchRequest):
|
|||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
@app.post("/getprofile")
|
||||
def get_profile(request: ProfileRequest):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT uid, display_name, current_elo FROM users WHERE session_token = %s;", (request.token,))
|
||||
user = cursor.fetchone()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
uid, display_name, current_elo = user["uid"], user["display_name"], user["current_elo"]
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT
|
||||
m.match_id,
|
||||
u.display_name AS opponent_name,
|
||||
CASE
|
||||
WHEN m.player1_uid = %s AND m.player1_score > m.player2_score THEN 'Win'
|
||||
WHEN m.player2_uid = %s AND m.player2_score > m.player1_score THEN 'Win'
|
||||
ELSE 'Loss'
|
||||
END AS result,
|
||||
CASE
|
||||
WHEN m.player1_uid = %s THEN m.player1_elo_change
|
||||
WHEN m.player2_uid = %s THEN m.player2_elo_change
|
||||
END AS elo_change
|
||||
FROM matches m
|
||||
JOIN users u ON u.uid = CASE
|
||||
WHEN m.player1_uid = %s THEN m.player2_uid
|
||||
WHEN m.player2_uid = %s THEN m.player1_uid
|
||||
END
|
||||
WHERE %s IN (m.player1_uid, m.player2_uid)
|
||||
ORDER BY m.match_date DESC
|
||||
LIMIT 10;
|
||||
""",
|
||||
(uid, uid, uid, uid, uid, uid, uid)
|
||||
)
|
||||
matches = cursor.fetchall()
|
||||
|
||||
return {
|
||||
"name": display_name,
|
||||
"uid": uid,
|
||||
"elo": current_elo,
|
||||
"matches": [
|
||||
{
|
||||
"match_id": match["match_id"],
|
||||
"opponent_name": match["opponent_name"],
|
||||
"result": match["result"],
|
||||
"elo_change": match["elo_change"]
|
||||
}
|
||||
for match in matches
|
||||
]
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
@app.post("/getprofile")
|
||||
def get_profile(request: ProfileRequest):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT uid, display_name, current_elo FROM users WHERE token = %s;", (request.token,))
|
||||
user = cursor.fetchone()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
uid, display_name, current_elo = user["uid"], user["display_name"], user["current_elo"]
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT
|
||||
m.match_id,
|
||||
u.display_name AS opponent_name,
|
||||
m.match_date,
|
||||
CASE
|
||||
WHEN m.player1_uid = %s AND m.player1_score > m.player2_score THEN 'Win'
|
||||
WHEN m.player2_uid = %s AND m.player2_score > m.player1_score THEN 'Win'
|
||||
ELSE 'Loss'
|
||||
END AS result,
|
||||
CASE
|
||||
WHEN m.player1_uid = %s THEN m.player1_elo_change
|
||||
WHEN m.player2_uid = %s THEN m.player2_elo_change
|
||||
END AS elo_change
|
||||
FROM matches m
|
||||
JOIN users u ON u.uid = CASE
|
||||
WHEN m.player1_uid = %s THEN m.player2_uid
|
||||
WHEN m.player2_uid = %s THEN m.player1_uid
|
||||
END
|
||||
WHERE %s IN (m.player1_uid, m.player2_uid)
|
||||
ORDER BY m.match_date DESC
|
||||
LIMIT 10;
|
||||
""",
|
||||
(uid, uid, uid, uid, uid, uid, uid)
|
||||
)
|
||||
matches = cursor.fetchall()
|
||||
|
||||
return {
|
||||
"name": display_name,
|
||||
"uid": uid,
|
||||
"elo": current_elo,
|
||||
"matches": [
|
||||
{
|
||||
"match_id": match["match_id"],
|
||||
"opponent_name": match["opponent_name"],
|
||||
"result": match["result"],
|
||||
"elo_change": match["elo_change"],
|
||||
"match_date": match["match_date"].isoformat()
|
||||
}
|
||||
for match in matches
|
||||
]
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
Loading…
Reference in a new issue