77 lines
1.9 KiB
Python
77 lines
1.9 KiB
Python
from typing import List, Any
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database.deps import get_db
|
|
from app.schemas.todo import TodoCreate, TodoUpdate, TodoResponse
|
|
from app.services import todo_service
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[TodoResponse])
|
|
def read_todos(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Retrieve todos with pagination.
|
|
"""
|
|
todos = todo_service.get_todos(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=TodoResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(
|
|
todo_in: TodoCreate,
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = todo_service.create_todo(db=db, todo=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=TodoResponse)
|
|
def read_todo(
|
|
todo_id: int,
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo = todo_service.get_todo(db=db, todo_id=todo_id)
|
|
if todo is None:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return todo
|
|
|
|
|
|
@router.put("/{todo_id}", response_model=TodoResponse)
|
|
def update_todo(
|
|
todo_id: int,
|
|
todo_in: TodoUpdate,
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = todo_service.update_todo(db=db, todo_id=todo_id, todo_update=todo_in)
|
|
if todo is None:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return todo
|
|
|
|
|
|
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_todo(
|
|
todo_id: int,
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
success = todo_service.delete_todo(db=db, todo_id=todo_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
return None |