Skip to content

Commit 74361e2

Browse files
committed
add confirm time endpoint
add match route to server.py add new line to match route
1 parent 7b096ae commit 74361e2

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

backend/app/routes/match.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from fastapi import APIRouter, Depends, HTTPException
2+
from sqlalchemy.orm import Session
3+
4+
from app.utilities.db_utils import get_db
5+
from app.services.implementations.match_service import MatchService
6+
from app.schemas.match import SubmitMatchRequest, SubmitMatchResponse
7+
8+
router = APIRouter(prefix="/matches", tags=["matches"])
9+
10+
11+
def get_match_service(db: Session = Depends(get_db)) -> MatchService:
12+
return MatchService(db)
13+
14+
15+
@router.post("/confirm-time", response_model=SubmitMatchResponse)
16+
async def confirm_time(
17+
payload: SubmitMatchRequest,
18+
match_service: MatchService = Depends(get_match_service),
19+
):
20+
try:
21+
confirmed_match = await match_service.submit_time(payload)
22+
return confirmed_match
23+
except HTTPException as http_ex:
24+
raise http_ex
25+
except Exception as e:
26+
print(e)
27+
raise HTTPException(status_code=500, detail=str(e))
28+

backend/app/schemas/availability.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from datetime import datetime, timedelta
22
from enum import Enum
3-
from typing import List, Optional
3+
from typing import List
44
from uuid import UUID
55

6-
from pydantic import BaseModel, ConfigDict
6+
from pydantic import BaseModel
77

88
from app.schemas.time_block import TimeBlockEntity, TimeRange
99

backend/app/schemas/match.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pydantic import BaseModel
2+
3+
from app.schemas.time_block import TimeBlockEntity
4+
5+
class SubmitMatchRequest(BaseModel):
6+
match_id: int
7+
time_block_id: int
8+
9+
10+
class SubmitMatchResponse(BaseModel):
11+
match_id: int
12+
time_block: TimeBlockEntity

backend/app/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from . import models
1010
from .middleware.auth_middleware import AuthMiddleware
11-
from .routes import auth, send_email, test, user, suggested_times, availability
11+
from .routes import auth, send_email, test, user, suggested_times, availability, match
1212
from .utilities.constants import LOGGER_NAME
1313
from .utilities.firebase_init import initialize_firebase
1414
# from .utilities.ses.ses_init import ensure_ses_templates
@@ -63,6 +63,7 @@ async def lifespan(_: FastAPI):
6363
app.include_router(user.router)
6464
app.include_router(availability.router)
6565
app.include_router(suggested_times.router)
66+
app.include_router(match.router)
6667
# app.include_router(send_email.router)
6768
app.include_router(test.router)
6869

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import logging
2+
from fastapi import HTTPException
3+
from sqlalchemy.orm import Session
4+
5+
from app.models import Match, TimeBlock, MatchStatus
6+
from app.schemas.match import SubmitMatchRequest, SubmitMatchResponse
7+
from app.schemas.time_block import TimeBlockEntity
8+
9+
10+
class MatchService:
11+
def __init__(self, db: Session):
12+
self.db = db
13+
self.logger = logging.getLogger(__name__)
14+
15+
async def submit_time(self, req: SubmitMatchRequest) -> SubmitMatchResponse:
16+
try:
17+
match = self.db.get(Match, req.match_id)
18+
if not match:
19+
raise HTTPException(404, f"Match {req.match_id} not found")
20+
21+
block = self.db.get(TimeBlock, req.time_block_id)
22+
if not block:
23+
raise HTTPException(404, f"TimeBlock {req.time_block_id} not found")
24+
25+
if block.confirmed_match and block.confirmed_match.id != match.id:
26+
raise HTTPException(400, "TimeBlock already confirmed for another match")
27+
28+
# confirm timeblock in match
29+
match.chosen_time_block_id = block.id
30+
match.confirmed_time = block
31+
match.match_status = self.db.get(MatchStatus, 6)
32+
33+
self.db.flush()
34+
35+
response = SubmitMatchResponse.model_validate({
36+
"match_id": match.id,
37+
"time_block": block,
38+
})
39+
40+
self.db.commit()
41+
return response
42+
43+
except HTTPException:
44+
self.db.rollback()
45+
raise
46+
except Exception as exc:
47+
self.db.rollback()
48+
self.logger.error(f"Error confirming time for match {req.match_id}: {exc}")
49+
raise HTTPException(status_code=500, detail="Failed to confirm time")

0 commit comments

Comments
 (0)