Spaces:
Running
Running
sghorbal commited on
Commit ·
3869c24
1
Parent(s): 4d68178
expose api for insertion of a batch of matches in db
Browse files- src/main.py +31 -0
src/main.py
CHANGED
|
@@ -266,6 +266,37 @@ async def insert_match(
|
|
| 266 |
|
| 267 |
return JSONResponse(content=output, status_code=HTTP_200_OK)
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
# ------------------------------------------------------------------------------
|
| 270 |
@app.get("/check_health", tags=["general"], description="Check the health of the API")
|
| 271 |
async def check_health(session: Annotated[Session, Depends(get_session)]):
|
|
|
|
| 266 |
|
| 267 |
return JSONResponse(content=output, status_code=HTTP_200_OK)
|
| 268 |
|
| 269 |
+
|
| 270 |
+
@app.post("/batch/match/insert", tags=["match"], description="Insert a batch of matches into the database")
|
| 271 |
+
async def insert_batch_match(
|
| 272 |
+
raw_matches: list[RawMatch],
|
| 273 |
+
session: Annotated[Session, Depends(get_session)]
|
| 274 |
+
):
|
| 275 |
+
"""
|
| 276 |
+
Insert a batch of matches into the database
|
| 277 |
+
"""
|
| 278 |
+
matches = []
|
| 279 |
+
for raw_match in raw_matches:
|
| 280 |
+
try:
|
| 281 |
+
match = insert_new_match(
|
| 282 |
+
db=session,
|
| 283 |
+
raw_match=raw_match.model_dump(exclude_unset=True)
|
| 284 |
+
)
|
| 285 |
+
matches.append(match)
|
| 286 |
+
except IntegrityError as e:
|
| 287 |
+
logger.error(f"Error inserting match: {e}")
|
| 288 |
+
raise HTTPException(
|
| 289 |
+
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
| 290 |
+
detail="Entity already exists in the database"
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
output = {
|
| 294 |
+
"status": "ok",
|
| 295 |
+
"match_ids": [match.id for match in matches],
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
return JSONResponse(content=output, status_code=HTTP_200_OK)
|
| 299 |
+
|
| 300 |
# ------------------------------------------------------------------------------
|
| 301 |
@app.get("/check_health", tags=["general"], description="Check the health of the API")
|
| 302 |
async def check_health(session: Annotated[Session, Depends(get_session)]):
|