
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
122 lines
3.0 KiB
Python
122 lines
3.0 KiB
Python
from typing import Any, List, Optional
|
|
|
|
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.task import (
|
|
create_task,
|
|
delete_task,
|
|
get_task_by_user,
|
|
get_tasks,
|
|
update_task,
|
|
)
|
|
from app.models.user import User
|
|
from app.schemas.task import Task, TaskCreate, TaskUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[Task])
|
|
def read_tasks(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
completed: Optional[bool] = None,
|
|
category_id: Optional[int] = None,
|
|
search: Optional[str] = None,
|
|
priority: Optional[str] = None,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve tasks for the current user.
|
|
"""
|
|
tasks = get_tasks(
|
|
db=db,
|
|
user_id=current_user.id,
|
|
skip=skip,
|
|
limit=limit,
|
|
completed=completed,
|
|
category_id=category_id,
|
|
search=search,
|
|
priority=priority,
|
|
)
|
|
return tasks
|
|
|
|
|
|
@router.post("", response_model=Task)
|
|
def create_task_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_in: TaskCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new task.
|
|
"""
|
|
task = create_task(db=db, task_in=task_in, user_id=current_user.id)
|
|
return task
|
|
|
|
|
|
@router.get("/{task_id}", response_model=Task)
|
|
def read_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get specific task by ID.
|
|
"""
|
|
task = get_task_by_user(db=db, task_id=task_id, user_id=current_user.id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found",
|
|
)
|
|
return task
|
|
|
|
|
|
@router.put("/{task_id}", response_model=Task)
|
|
def update_task_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
task_in: TaskUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task = get_task_by_user(db=db, task_id=task_id, user_id=current_user.id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found",
|
|
)
|
|
task = update_task(
|
|
db=db, task_id=task_id, task_in=task_in, user_id=current_user.id
|
|
)
|
|
return task
|
|
|
|
|
|
@router.delete("/{task_id}", response_model=Task)
|
|
def delete_task_endpoint(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task = get_task_by_user(db=db, task_id=task_id, user_id=current_user.id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found",
|
|
)
|
|
task = delete_task(db=db, task_id=task_id, user_id=current_user.id)
|
|
return task
|