Spaces:
Sleeping
Sleeping
samar m commited on
Commit ·
81f4fd4
1
Parent(s): 4c73db5
feat: topics router — create, list, unlock/lock
Browse files- backend/routers/topics.py +69 -1
backend/routers/topics.py
CHANGED
|
@@ -1,3 +1,71 @@
|
|
| 1 |
-
from fastapi import APIRouter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
from backend.auth.deps import require_instructor
|
| 5 |
+
from backend.db import queries
|
| 6 |
|
| 7 |
router = APIRouter()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class CreateTopicRequest(BaseModel):
|
| 11 |
+
batch_id: str
|
| 12 |
+
name: str
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class UpdateTopicRequest(BaseModel):
|
| 16 |
+
is_unlocked: bool
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _topic_out(topic, question_count: int = 0) -> dict:
|
| 20 |
+
return {
|
| 21 |
+
"id": str(topic["id"]),
|
| 22 |
+
"batch_id": str(topic["batch_id"]),
|
| 23 |
+
"name": topic["name"],
|
| 24 |
+
"is_unlocked": topic["is_unlocked"],
|
| 25 |
+
"order_index": topic["order_index"],
|
| 26 |
+
"question_count": question_count,
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@router.post("")
|
| 31 |
+
async def create_topic(
|
| 32 |
+
body: CreateTopicRequest,
|
| 33 |
+
user: dict = Depends(require_instructor),
|
| 34 |
+
):
|
| 35 |
+
batch = await queries.get_batch_by_id(body.batch_id)
|
| 36 |
+
if not batch or str(batch["instructor_id"]) != user["user_id"]:
|
| 37 |
+
raise HTTPException(403, "Batch not found or not yours")
|
| 38 |
+
existing = await queries.get_topics_by_batch_id(body.batch_id)
|
| 39 |
+
order_index = len(existing)
|
| 40 |
+
topic = await queries.create_topic(body.batch_id, body.name, order_index)
|
| 41 |
+
return _topic_out(topic)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@router.get("")
|
| 45 |
+
async def list_topics(
|
| 46 |
+
batch_id: str,
|
| 47 |
+
user: dict = Depends(require_instructor),
|
| 48 |
+
):
|
| 49 |
+
topics = await queries.get_topics_by_batch_id(batch_id)
|
| 50 |
+
result = []
|
| 51 |
+
for t in topics:
|
| 52 |
+
count = await queries.get_question_count_by_topic(str(t["id"]))
|
| 53 |
+
result.append(_topic_out(t, count))
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@router.patch("/{topic_id}")
|
| 58 |
+
async def update_topic(
|
| 59 |
+
topic_id: str,
|
| 60 |
+
body: UpdateTopicRequest,
|
| 61 |
+
user: dict = Depends(require_instructor),
|
| 62 |
+
):
|
| 63 |
+
topic = await queries.get_topic_by_id(topic_id)
|
| 64 |
+
if not topic:
|
| 65 |
+
raise HTTPException(404, "Topic not found")
|
| 66 |
+
batch = await queries.get_batch_by_id(str(topic["batch_id"]))
|
| 67 |
+
if not batch or str(batch["instructor_id"]) != user["user_id"]:
|
| 68 |
+
raise HTTPException(403, "Topic does not belong to your batch")
|
| 69 |
+
updated = await queries.set_topic_unlock(topic_id, body.is_unlocked)
|
| 70 |
+
count = await queries.get_question_count_by_topic(topic_id)
|
| 71 |
+
return _topic_out(updated, count)
|