Automated Action 980c575291 Finish up the Todo application
- Fixed database connection path to match BackendIM requirements
- Implemented filter by completion status in the todos endpoint
- Fixed updated_at timestamp to automatically update
- Updated alembic.ini with the correct database URL

generated with BackendIM... (backend.im)
2025-05-13 06:05:28 +00:00

43 lines
1.6 KiB
Python

from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.crud import todo as todo_crud
from app.db.session import get_db
from app.schemas.todo import TodoCreate, TodoResponse, TodoUpdate
router = APIRouter()
@router.get("/", response_model=List[TodoResponse])
def get_todos(
skip: int = 0,
limit: int = 100,
completed: Optional[bool] = None,
db: Session = Depends(get_db)
):
return todo_crud.get_todos(db, skip=skip, limit=limit, completed=completed)
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
def create_todo(todo: TodoCreate, db: Session = Depends(get_db)):
return todo_crud.create_todo(db, todo)
@router.get("/{todo_id}", response_model=TodoResponse)
def get_todo(todo_id: int, db: Session = Depends(get_db)):
db_todo = todo_crud.get_todo(db, todo_id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@router.put("/{todo_id}", response_model=TodoResponse)
def update_todo(todo_id: int, todo: TodoUpdate, db: Session = Depends(get_db)):
db_todo = todo_crud.update_todo(db, todo_id, todo)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
success = todo_crud.delete_todo(db, todo_id)
if not success:
raise HTTPException(status_code=404, detail="Todo not found")
return None