Files
mealie/mealie/routes/recipe/category_routes.py
Hayden 846d1eda5b feature/category-tag-crud (#354)
* update tag route

* search.and

* offset for mobile

* relative imports

* get settings

* new page

* category/tag CRUD

* bulk assign frontend

* Bulk assign

* debounce search

* remove dev data

* recipe store refactor

* fix mobile view

* fix failing tests

* commit test data

Co-authored-by: hay-kot <hay-kot@pm.me>
2021-04-27 11:17:00 -08:00

65 lines
2.2 KiB
Python

from fastapi import APIRouter, Depends
from mealie.db.database import db
from mealie.db.db_setup import generate_session
from mealie.routes.deps import get_current_user
from mealie.schema.category import CategoryIn, RecipeCategoryResponse
from mealie.schema.snackbar import SnackResponse
from sqlalchemy.orm.session import Session
router = APIRouter(
prefix="/api/categories",
tags=["Recipe Categories"],
)
@router.get("")
async def get_all_recipe_categories(session: Session = Depends(generate_session)):
""" Returns a list of available categories in the database """
return db.categories.get_all_limit_columns(session, ["slug", "name"])
@router.get("/empty")
def get_empty_categories(session: Session = Depends(generate_session)):
""" Returns a list of categories that do not contain any recipes"""
return db.categories.get_empty(session)
@router.get("/{category}", response_model=RecipeCategoryResponse)
def get_all_recipes_by_category(category: str, session: Session = Depends(generate_session)):
""" Returns a list of recipes associated with the provided category. """
return db.categories.get(session, category)
@router.post("")
async def create_recipe_category(
category: CategoryIn, session: Session = Depends(generate_session), current_user=Depends(get_current_user)
):
""" Creates a Category in the database """
return db.categories.create(session, category.dict())
@router.put("/{category}", response_model=RecipeCategoryResponse)
async def update_recipe_category(
category: str,
new_category: CategoryIn,
session: Session = Depends(generate_session),
current_user=Depends(get_current_user),
):
""" Updates an existing Tag in the database """
return db.categories.update(session, category, new_category.dict())
@router.delete("/{category}")
async def delete_recipe_category(
category: str, session: Session = Depends(generate_session), current_user=Depends(get_current_user)
):
"""Removes a recipe category from the database. Deleting a
category does not impact a recipe. The category will be removed
from any recipes that contain it"""
db.categories.delete(session, category)
return SnackResponse.error(f"Category Deleted: {category}")