
- Setup project structure with FastAPI app - Create SQLAlchemy models for categories, questions, quizzes, and results - Implement API endpoints for all CRUD operations - Set up Alembic migrations for database schema management - Add comprehensive documentation in README.md
43 lines
2.0 KiB
Python
43 lines
2.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.db.session import get_db
|
|
from app.schemas.category import CategoryCreate, CategoryResponse
|
|
from app.services.category import create_category, get_category, get_categories, update_category, delete_category
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=CategoryResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_category_endpoint(category_data: CategoryCreate, db: Session = Depends(get_db)):
|
|
"""Create a new category"""
|
|
return create_category(db=db, category_data=category_data)
|
|
|
|
@router.get("/{category_id}", response_model=CategoryResponse)
|
|
def get_category_endpoint(category_id: int, db: Session = Depends(get_db)):
|
|
"""Get a specific category by ID"""
|
|
db_category = get_category(db=db, category_id=category_id)
|
|
if db_category is None:
|
|
raise HTTPException(status_code=404, detail="Category not found")
|
|
return db_category
|
|
|
|
@router.get("/", response_model=List[CategoryResponse])
|
|
def get_categories_endpoint(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
"""Get all categories"""
|
|
return get_categories(db=db, skip=skip, limit=limit)
|
|
|
|
@router.put("/{category_id}", response_model=CategoryResponse)
|
|
def update_category_endpoint(category_id: int, category_data: CategoryCreate, db: Session = Depends(get_db)):
|
|
"""Update a category"""
|
|
db_category = update_category(db=db, category_id=category_id, category_data=category_data)
|
|
if db_category is None:
|
|
raise HTTPException(status_code=404, detail="Category not found")
|
|
return db_category
|
|
|
|
@router.delete("/{category_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_category_endpoint(category_id: int, db: Session = Depends(get_db)):
|
|
"""Delete a category"""
|
|
success = delete_category(db=db, category_id=category_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Category not found")
|
|
return None |