
- Set up FastAPI project structure with API versioning - Create database models for users and tasks - Implement SQLAlchemy ORM with SQLite database - Initialize Alembic for database migrations - Create API endpoints for task management (CRUD) - Create API endpoints for user management - Add JWT authentication and authorization - Add health check endpoint - Add comprehensive README.md with API documentation
102 lines
2.6 KiB
Python
102 lines
2.6 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_user
|
|
from app.crud import task as crud_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])
|
|
def read_tasks(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve tasks for the current user.
|
|
"""
|
|
tasks = crud_task.get_multi(
|
|
db=db, user_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return tasks
|
|
|
|
@router.post("", response_model=Task, status_code=status.HTTP_201_CREATED)
|
|
def create_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_in: TaskCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create a new task.
|
|
"""
|
|
task = crud_task.create(db=db, obj_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 task by ID.
|
|
"""
|
|
task = crud_task.get(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(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
task_in: TaskUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task = crud_task.get(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 = crud_task.update(db=db, db_obj=task, obj_in=task_in)
|
|
return task
|
|
|
|
@router.delete(
|
|
"/{task_id}",
|
|
response_model=None,
|
|
status_code=status.HTTP_204_NO_CONTENT
|
|
)
|
|
def delete_task(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
task_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task = crud_task.get(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"
|
|
)
|
|
crud_task.delete(db=db, task_id=task_id, user_id=current_user.id)
|
|
return None
|