
- Created project structure with FastAPI setup - Added SQLite database connection with SQLAlchemy ORM - Implemented Todo model and schemas - Added CRUD operations for Todo items - Created API endpoints for Todo management - Added health check endpoint - Configured Alembic for database migrations - Updated project documentation in README.md
99 lines
2.3 KiB
Python
99 lines
2.3 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.db.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.todo.Todo])
|
|
def read_todos(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
completed: bool = None,
|
|
) -> Any:
|
|
"""
|
|
Retrieve todos.
|
|
"""
|
|
if completed is not None:
|
|
if completed:
|
|
todos = crud.todo.get_completed(db, skip=skip, limit=limit)
|
|
else:
|
|
todos = crud.todo.get_incomplete(db, skip=skip, limit=limit)
|
|
else:
|
|
todos = crud.todo.get_multi(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=schemas.todo.Todo, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_in: schemas.todo.TodoCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = crud.todo.create(db=db, obj_in=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.todo.Todo)
|
|
def read_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo = crud.todo.get(db=db, id=id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found",
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.todo.Todo)
|
|
def update_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
todo_in: schemas.todo.TodoUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = crud.todo.get(db=db, id=id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found",
|
|
)
|
|
todo = crud.todo.update(db=db, db_obj=todo, obj_in=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
todo = crud.todo.get(db=db, id=id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found",
|
|
)
|
|
crud.todo.remove(db=db, id=id)
|
|
return None |