MCPcopy Create free account
hub / github.com/ImGoodBai/MapGoGoGo / create_challenge

Function create_challenge

backend/app/main.py:277–333  ·  view source on GitHub ↗
(
    payload: ChallengeCreate,
    response: Response,
    participant=Cookie(default=None, alias=COOKIE_NAME),
)

Source from the content-addressed store, hash-verified

275
276@app.post("/api/challenges")
277def create_challenge(
278 payload: ChallengeCreate,
279 response: Response,
280 participant=Cookie(default=None, alias=COOKIE_NAME),
281) -> dict:
282 creator = get_or_create_participant(response, participant)
283 challenge_id = new_id("ch")
284 current = now_iso()
285 ends_at = (datetime.now(UTC) + timedelta(hours=payload.duration_hours)).isoformat(
286 timespec="seconds"
287 )
288 cells = generate_circle_cells(
289 payload.center_lat,
290 payload.center_lng,
291 payload.radius_m,
292 payload.h3_resolution,
293 )
294 if not cells:
295 raise HTTPException(status_code=400, detail="No cells generated")
296
297 with connect() as conn:
298 conn.execute(
299 """
300 INSERT INTO challenges
301 (id, name, description, place_name, center_lat, center_lng, radius_m,
302 h3_resolution, status, starts_at, ends_at, created_by, created_at)
303 VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?, ?)
304 """,
305 (
306 challenge_id,
307 payload.name.strip(),
308 payload.description.strip(),
309 payload.place_name.strip(),
310 payload.center_lat,
311 payload.center_lng,
312 payload.radius_m,
313 payload.h3_resolution,
314 current,
315 ends_at,
316 creator["id"],
317 current,
318 ),
319 )
320 conn.executemany(
321 """
322 INSERT INTO challenge_cells (challenge_id, cell_id, lat, lng)
323 VALUES (?, ?, ?, ?)
324 """,
325 [
326 (challenge_id, cell["cell_id"], cell["lat"], cell["lng"])
327 for cell in cells
328 ],
329 )
330 row = conn.execute(
331 "SELECT * FROM challenges WHERE id = ?", (challenge_id,)
332 ).fetchone()
333 return {"challenge": public_challenge(conn, row)}
334

Callers

nothing calls this directly

Calls 6

new_idFunction · 0.85
now_isoFunction · 0.85
generate_circle_cellsFunction · 0.85
connectFunction · 0.85
public_challengeFunction · 0.85

Tested by

no test coverage detected