
This commit includes: - Project structure and configuration - Database models for tasks, users, and categories - Authentication system with JWT - CRUD endpoints for tasks and categories - Search, filter, and sorting functionality - Health check endpoint - Alembic migration setup - Documentation
124 lines
3.1 KiB
Python
124 lines
3.1 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.core.deps import get_current_active_user
|
|
from app.crud.category import (
|
|
create_category,
|
|
delete_category,
|
|
get_categories,
|
|
get_category_by_user,
|
|
update_category,
|
|
)
|
|
from app.models.user import User
|
|
from app.schemas.category import Category, CategoryCreate, CategoryUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[Category])
|
|
def read_categories(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve categories for the current user.
|
|
"""
|
|
categories = get_categories(
|
|
db=db, user_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return categories
|
|
|
|
|
|
@router.post("", response_model=Category)
|
|
def create_category_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_in: CategoryCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new category.
|
|
"""
|
|
category = create_category(
|
|
db=db, category_in=category_in, user_id=current_user.id
|
|
)
|
|
return category
|
|
|
|
|
|
@router.get("/{category_id}", response_model=Category)
|
|
def read_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get specific category by ID.
|
|
"""
|
|
category = get_category_by_user(
|
|
db=db, category_id=category_id, user_id=current_user.id
|
|
)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
return category
|
|
|
|
|
|
@router.put("/{category_id}", response_model=Category)
|
|
def update_category_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: int,
|
|
category_in: CategoryUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a category.
|
|
"""
|
|
category = get_category_by_user(
|
|
db=db, category_id=category_id, user_id=current_user.id
|
|
)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
category = update_category(
|
|
db=db,
|
|
category_id=category_id,
|
|
category_in=category_in,
|
|
user_id=current_user.id,
|
|
)
|
|
return category
|
|
|
|
|
|
@router.delete("/{category_id}", response_model=Category)
|
|
def delete_category_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a category.
|
|
"""
|
|
category = get_category_by_user(
|
|
db=db, category_id=category_id, user_id=current_user.id
|
|
)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
category = delete_category(
|
|
db=db, category_id=category_id, user_id=current_user.id
|
|
)
|
|
return category
|