2023-05-21 01:50:44 +01:00
|
|
|
"""This module contains endpoints for operations related to users."""
|
|
|
|
|
2023-05-30 15:27:29 +01:00
|
|
|
from typing import Annotated
|
|
|
|
|
2023-05-21 01:50:44 +01:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
from todo.database.engine import get_db
|
|
|
|
from todo.schemas import todos as todoschema
|
|
|
|
from todo.crud import todos as todocrud
|
2023-05-21 09:38:27 +01:00
|
|
|
from todo.utils.exceptions import NotFoundException, InvalidFilterParameterException
|
|
|
|
from todo.utils.exceptions import create_exception_dict as fmt
|
2023-05-30 15:27:29 +01:00
|
|
|
from todo.dependencies.todos import TodoItemSortablePaginationParams
|
2023-05-21 01:50:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter(
|
2023-05-30 15:27:29 +01:00
|
|
|
prefix="/todos",
|
2023-05-21 01:50:44 +01:00
|
|
|
tags=["todo-items"]
|
|
|
|
)
|
|
|
|
|
|
|
|
tag_metadata = {
|
|
|
|
"name": "todo-items",
|
|
|
|
"description": "Operations related to todo items."
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/user/{user_id}", response_model=todoschema.TodoItem)
|
|
|
|
def create_todo(todo: todoschema.TodoItemCreate, user_id: int, db: Session = Depends(get_db)):
|
|
|
|
try:
|
|
|
|
return todocrud.create_todo(db=db, todo=todo, user_id=user_id)
|
|
|
|
except NotFoundException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(404, fmt(str(e)))
|
2023-05-21 01:50:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{todo_id}", response_model=todoschema.TodoItem)
|
|
|
|
def read_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
|
|
try:
|
|
|
|
return todocrud.read_todo(db=db, todo_id=todo_id)
|
|
|
|
except NotFoundException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(404, fmt(str(e)))
|
2023-05-21 01:50:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/user/{user_id}", response_model=list[todoschema.TodoItem])
|
2023-05-22 12:44:10 +01:00
|
|
|
def read_todos(
|
|
|
|
user_id: int,
|
2023-05-30 15:27:29 +01:00
|
|
|
commons: Annotated[TodoItemSortablePaginationParams, Depends(TodoItemSortablePaginationParams)],
|
2023-05-22 12:44:10 +01:00
|
|
|
db: Session = Depends(get_db)
|
|
|
|
):
|
2023-05-21 01:50:44 +01:00
|
|
|
try:
|
2023-05-30 15:27:29 +01:00
|
|
|
return todocrud.read_todos_for_user(
|
|
|
|
db=db,
|
|
|
|
user_id=user_id,
|
|
|
|
skip=commons.skip, limit=commons.limit,
|
|
|
|
sortby=commons.sortby, sortorder=commons.sortorder,
|
|
|
|
)
|
2023-05-21 01:50:44 +01:00
|
|
|
except InvalidFilterParameterException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(400, fmt(str(e)))
|
2023-05-21 01:50:44 +01:00
|
|
|
except NotFoundException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(404, fmt(str(e)))
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/user/{user_id}/total", response_model=dict[str, int])
|
|
|
|
def read_todos_count(user_id: int, db: Session = Depends(get_db)):
|
|
|
|
try:
|
|
|
|
return {"total": todocrud.read_todos_count_for_user(db=db, user_id=user_id)}
|
|
|
|
except NotFoundException as e:
|
|
|
|
raise HTTPException(404, fmt(str(e)))
|
2023-05-21 01:50:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{todo_id}", response_model=todoschema.TodoItem)
|
|
|
|
def update_todo(todo_id: int, todo: todoschema.TodoItemUpdate, db: Session = Depends(get_db)):
|
|
|
|
try:
|
|
|
|
return todocrud.update_todo(db=db, todo=todo, todo_id=todo_id)
|
|
|
|
except NotFoundException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(404, fmt(str(e)))
|
2023-05-21 01:50:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{todo_id}", response_model=todoschema.TodoItem)
|
|
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db)):
|
|
|
|
try:
|
|
|
|
return todocrud.delete_todo(db=db, todo_id=todo_id)
|
|
|
|
except NotFoundException as e:
|
2023-05-21 09:38:27 +01:00
|
|
|
raise HTTPException(404, fmt(str(e)))
|