Automated Action d1c05cbd6e Implement Task Manager API with FastAPI and SQLite
- Set up project structure and dependencies
- Create database models for tasks and users with SQLAlchemy
- Configure Alembic for database migrations
- Implement authentication system with JWT tokens
- Create CRUD API endpoints for tasks and users
- Add health check endpoint
- Update README with documentation
2025-06-12 18:14:56 +00:00

98 lines
2.6 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app import crud, schemas
from app.core.security import get_current_active_user
from app.db.session import get_db
from app.models.user import User
router = APIRouter()
@router.get("/", response_model=List[schemas.Task])
async def read_tasks(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Retrieve tasks.
"""
tasks = crud.task.get_multi_by_owner(
db=db, owner_id=current_user.id, skip=skip, limit=limit
)
return tasks
@router.post("/", response_model=schemas.Task)
async def create_task(
*,
db: Session = Depends(get_db),
task_in: schemas.TaskCreate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Create new task.
"""
task = crud.task.create_with_owner(db=db, obj_in=task_in, owner_id=current_user.id)
return task
@router.get("/{task_id}", response_model=schemas.Task)
async 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_by_id_and_owner(db=db, task_id=task_id, owner_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=schemas.Task)
async def update_task(
*,
db: Session = Depends(get_db),
task_id: int,
task_in: schemas.TaskUpdate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Update a task.
"""
task = crud.task.get_by_id_and_owner(db=db, task_id=task_id, owner_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=schemas.Task)
async 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_by_id_and_owner(db=db, task_id=task_id, owner_id=current_user.id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Task not found"
)
task = crud.task.remove(db=db, id=task_id)
return task