93 lines
2.0 KiB
Python
93 lines
2.0 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.db.deps import get_db
|
|
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Todo])
|
|
def read_todos(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve todos.
|
|
"""
|
|
todos = crud.get_todos(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_in: TodoCreate,
|
|
) -> Any:
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = crud.create_todo(db=db, todo_in=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.get("/{id}", response_model=Todo)
|
|
def read_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo = crud.get_todo(db=db, todo_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=Todo)
|
|
def update_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
todo_in: TodoUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = crud.get_todo(db=db, todo_id=id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found",
|
|
)
|
|
todo = crud.update_todo(db=db, todo_id=id, todo_in=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.delete("/{id}", response_model=Todo)
|
|
def delete_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
todo = crud.get_todo(db=db, todo_id=id)
|
|
if not todo:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Todo not found",
|
|
)
|
|
todo = crud.delete_todo(db=db, todo_id=id)
|
|
return todo |