Spaces:
Sleeping
Sleeping
samar m commited on
Commit ·
4c73db5
1
Parent(s): dfcb935
feat: batches router — create, get mine, get by id
Browse files- backend/routers/batches.py +49 -1
backend/routers/batches.py
CHANGED
|
@@ -1,3 +1,51 @@
|
|
| 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 CreateBatchRequest(BaseModel):
|
| 11 |
+
name: str
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _batch_out(batch) -> dict:
|
| 15 |
+
return {
|
| 16 |
+
"id": str(batch["id"]),
|
| 17 |
+
"name": batch["name"],
|
| 18 |
+
"instructor_id": str(batch["instructor_id"]),
|
| 19 |
+
"class_code": batch["class_code"],
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@router.post("")
|
| 24 |
+
async def create_batch(
|
| 25 |
+
body: CreateBatchRequest,
|
| 26 |
+
user: dict = Depends(require_instructor),
|
| 27 |
+
):
|
| 28 |
+
existing = await queries.get_batch_by_instructor_id(user["user_id"])
|
| 29 |
+
if existing:
|
| 30 |
+
raise HTTPException(400, "You already have a batch")
|
| 31 |
+
batch = await queries.create_batch(body.name, user["user_id"])
|
| 32 |
+
return _batch_out(batch)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@router.get("/mine")
|
| 36 |
+
async def get_my_batch(user: dict = Depends(require_instructor)):
|
| 37 |
+
batch = await queries.get_batch_by_instructor_id(user["user_id"])
|
| 38 |
+
if not batch:
|
| 39 |
+
raise HTTPException(404, "No batch found")
|
| 40 |
+
return _batch_out(batch)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@router.get("/{batch_id}")
|
| 44 |
+
async def get_batch(
|
| 45 |
+
batch_id: str,
|
| 46 |
+
user: dict = Depends(require_instructor),
|
| 47 |
+
):
|
| 48 |
+
batch = await queries.get_batch_by_id(batch_id)
|
| 49 |
+
if not batch:
|
| 50 |
+
raise HTTPException(404, "Batch not found")
|
| 51 |
+
return _batch_out(batch)
|