76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.deps import get_db
|
|
from app.schemas.todo import Todo as TodoSchema
|
|
from app.schemas.todo import TodoCreate, TodoUpdate
|
|
from app.services.todo import (
|
|
get_todos,
|
|
get_todo,
|
|
create_todo,
|
|
update_todo,
|
|
delete_todo,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[TodoSchema])
|
|
def read_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
"""
|
|
Retrieve todos.
|
|
"""
|
|
todos = get_todos(db, skip=skip, limit=limit)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=TodoSchema, status_code=status.HTTP_201_CREATED)
|
|
def create_new_todo(todo_in: TodoCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = create_todo(db=db, todo=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=TodoSchema)
|
|
def read_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Get a specific todo by id.
|
|
"""
|
|
todo = get_todo(db=db, todo_id=todo_id)
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.put("/{todo_id}", response_model=TodoSchema)
|
|
def update_todo_item(todo_id: int, todo_in: TodoUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = update_todo(db=db, todo_id=todo_id, todo_update=todo_in)
|
|
if todo is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return todo
|
|
|
|
|
|
@router.delete(
|
|
"/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None
|
|
)
|
|
def delete_todo_item(todo_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
deleted = delete_todo(db=db, todo_id=todo_id)
|
|
if not deleted:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found"
|
|
)
|
|
return None
|