
- Set up project structure and dependencies - Implement Todo model with SQLAlchemy - Configure SQLite database connection - Create Alembic migration scripts - Implement RESTful API endpoints for CRUD operations - Add health check endpoint - Update README with documentation generated with BackendIM... (backend.im)
87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
from typing import List, Any
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api.deps import get_db
|
|
from app.schemas.todo import TodoCreate, TodoResponse, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[TodoResponse])
|
|
def read_todos(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve todos.
|
|
"""
|
|
todos = crud.todo.get_todos(db=db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_in: TodoCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = crud.todo.create_todo(db=db, todo=todo_in)
|
|
return todo
|
|
|
|
@router.get("/{todo_id}", response_model=TodoResponse)
|
|
def read_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo = crud.todo.get_todo(db=db, todo_id=todo_id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
)
|
|
return todo
|
|
|
|
@router.put("/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
todo_in: TodoUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = crud.todo.get_todo(db=db, todo_id=todo_id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
)
|
|
todo = crud.todo.update_todo(db=db, todo_id=todo_id, todo=todo_in)
|
|
return todo
|
|
|
|
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
todo = crud.todo.get_todo(db=db, todo_id=todo_id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found"
|
|
)
|
|
crud.todo.delete_todo(db=db, todo_id=todo_id)
|
|
return None |