
- Setup project structure and FastAPI application - Create SQLite database with SQLAlchemy - Implement user authentication with JWT - Create task and user models - Add CRUD operations for tasks and users - Configure Alembic for database migrations - Implement API endpoints for task management - Add error handling and validation - Configure CORS middleware - Create health check endpoint - Add comprehensive documentation
103 lines
2.9 KiB
Python
103 lines
2.9 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core import auth
|
|
from app.core.exceptions import ForbiddenException, NotFoundException
|
|
from app.crud.crud_task import task
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.task import Task, TaskCreate, TaskUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Task])
|
|
async def read_tasks(
|
|
db: AsyncSession = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(auth.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve tasks.
|
|
"""
|
|
if current_user.is_superuser:
|
|
tasks = await task.get_multi(db, skip=skip, limit=limit)
|
|
else:
|
|
tasks = await task.get_multi_by_owner(
|
|
db=db, owner_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return tasks
|
|
|
|
|
|
@router.post("/", response_model=Task)
|
|
async def create_task(
|
|
*,
|
|
db: AsyncSession = Depends(get_db),
|
|
task_in: TaskCreate,
|
|
current_user: User = Depends(auth.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new task.
|
|
"""
|
|
task_obj = await task.create_with_owner(db=db, obj_in=task_in, owner_id=current_user.id)
|
|
return task_obj
|
|
|
|
|
|
@router.put("/{task_id}", response_model=Task)
|
|
async def update_task(
|
|
*,
|
|
db: AsyncSession = Depends(get_db),
|
|
task_id: str,
|
|
task_in: TaskUpdate,
|
|
current_user: User = Depends(auth.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task_obj = await task.get(db=db, id_=task_id)
|
|
if not task_obj:
|
|
raise NotFoundException("Task not found")
|
|
if not current_user.is_superuser and (task_obj.user_id != current_user.id):
|
|
raise ForbiddenException("Not enough permissions")
|
|
task_obj = await task.update(db=db, db_obj=task_obj, obj_in=task_in)
|
|
return task_obj
|
|
|
|
|
|
@router.get("/{task_id}", response_model=Task)
|
|
async def read_task(
|
|
*,
|
|
db: AsyncSession = Depends(get_db),
|
|
task_id: str,
|
|
current_user: User = Depends(auth.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get task by ID.
|
|
"""
|
|
task_obj = await task.get(db=db, id_=task_id)
|
|
if not task_obj:
|
|
raise NotFoundException("Task not found")
|
|
if not current_user.is_superuser and (task_obj.user_id != current_user.id):
|
|
raise ForbiddenException("Not enough permissions")
|
|
return task_obj
|
|
|
|
|
|
@router.delete("/{task_id}", response_model=Task)
|
|
async def delete_task(
|
|
*,
|
|
db: AsyncSession = Depends(get_db),
|
|
task_id: str,
|
|
current_user: User = Depends(auth.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task_obj = await task.get(db=db, id_=task_id)
|
|
if not task_obj:
|
|
raise NotFoundException("Task not found")
|
|
if not current_user.is_superuser and (task_obj.user_id != current_user.id):
|
|
raise ForbiddenException("Not enough permissions")
|
|
task_obj = await task.remove(db=db, id_=task_id)
|
|
return task_obj |