diff --git a/mealie/routes/recipe/all_recipe_routes.py b/mealie/routes/recipe/all_recipe_routes.py new file mode 100644 index 000000000..b4bd7f861 --- /dev/null +++ b/mealie/routes/recipe/all_recipe_routes.py @@ -0,0 +1,48 @@ +from typing import List, Optional + +from db.db_setup import generate_session +from fastapi import APIRouter, Depends, Query +from models.recipe_models import AllRecipeRequest +from services.recipe_services import read_requested_values +from sqlalchemy.orm.session import Session + +router = APIRouter(tags=["Recipes"]) + + +@router.get("/api/all-recipes/", response_model=List[dict]) +def get_all_recipes( + keys: Optional[List[str]] = Query(...), + num: Optional[int] = 100, + db: Session = Depends(generate_session), +): + """ + Returns key data for all recipes based off the query paramters provided. + For example, if slug, image, and name are provided you will recieve a list of + recipes containing the slug, image, and name property. By default, responses + are limited to 100. + + **Note:** You may experience problems with with query parameters. As an alternative + you may also use the post method and provide a body. + See the *Post* method for more details. + """ + + all_recipes = read_requested_values(db, keys, num) + return all_recipes + + +@router.post("/api/all-recipes/", response_model=List[dict]) +def get_all_recipes_post( + body: AllRecipeRequest, db: Session = Depends(generate_session) +): + """ + Returns key data for all recipes based off the body data provided. + For example, if slug, image, and name are provided you will recieve a list of + recipes containing the slug, image, and name property. + + Refer to the body example for data formats. + + """ + + all_recipes = read_requested_values(db, body.properties, body.limit) + + return all_recipes diff --git a/mealie/routes/recipe/category_routes.py b/mealie/routes/recipe/category_routes.py new file mode 100644 index 000000000..f3f9668f3 --- /dev/null +++ b/mealie/routes/recipe/category_routes.py @@ -0,0 +1,25 @@ +from db.database import db +from db.db_setup import generate_session +from fastapi import APIRouter, Depends +from models.category_models import RecipeCategoryResponse +from sqlalchemy.orm.session import Session + +router = APIRouter( + prefix="/api/recipes/categories", + tags=["Recipes"], +) + + +@router.get("/all/") +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_primary_keys(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) diff --git a/mealie/routes/recipe_routes.py b/mealie/routes/recipe/recipe_crud_routes.py similarity index 52% rename from mealie/routes/recipe_routes.py rename to mealie/routes/recipe/recipe_crud_routes.py index 9f0df9582..7bef39ee8 100644 --- a/mealie/routes/recipe_routes.py +++ b/mealie/routes/recipe/recipe_crud_routes.py @@ -1,58 +1,20 @@ -from typing import List, Optional - from db.db_setup import generate_session from fastapi import APIRouter, Depends, File, Form, HTTPException, Query from fastapi.responses import FileResponse -from models.recipe_models import AllRecipeRequest, RecipeURLIn +from models.recipe_models import RecipeURLIn from services.image_services import read_image, write_image -from services.recipe_services import Recipe, read_requested_values +from services.recipe_services import Recipe from services.scrape_services import create_from_url from sqlalchemy.orm.session import Session from utils.snackbar import SnackResponse -router = APIRouter(tags=["Recipes"]) +router = APIRouter( + prefix="/api/recipe", + tags=["Recipes"], +) -@router.get("/api/all-recipes/", response_model=List[dict]) -def get_all_recipes( - keys: Optional[List[str]] = Query(...), - num: Optional[int] = 100, - db: Session = Depends(generate_session), -): - """ - Returns key data for all recipes based off the query paramters provided. - For example, if slug, image, and name are provided you will recieve a list of - recipes containing the slug, image, and name property. By default, responses - are limited to 100. - - **Note:** You may experience problems with with query parameters. As an alternative - you may also use the post method and provide a body. - See the *Post* method for more details. - """ - - all_recipes = read_requested_values(db, keys, num) - return all_recipes - - -@router.post("/api/all-recipes/", response_model=List[dict]) -def get_all_recipes_post( - body: AllRecipeRequest, db: Session = Depends(generate_session) -): - """ - Returns key data for all recipes based off the body data provided. - For example, if slug, image, and name are provided you will recieve a list of - recipes containing the slug, image, and name property. - - Refer to the body example for data formats. - - """ - - all_recipes = read_requested_values(db, body.properties, body.limit) - - return all_recipes - - -@router.get("/api/recipe/{recipe_slug}/", response_model=Recipe) +@router.get("/{recipe_slug}/", response_model=Recipe) def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)): """ Takes in a recipe slug, returns all data for a recipe """ recipe = Recipe.get_by_slug(db, recipe_slug) @@ -60,7 +22,7 @@ def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)): return recipe -@router.get("/api/recipe/image/{recipe_slug}/") +@router.get("/image/{recipe_slug}/") def get_recipe_img(recipe_slug: str): """ Takes in a recipe slug, returns the static image """ recipe_image = read_image(recipe_slug) @@ -68,11 +30,7 @@ def get_recipe_img(recipe_slug: str): return FileResponse(recipe_image) -@router.post( - "/api/recipe/create-url/", - status_code=201, - response_model=str, -) +@router.post("/create-url/", status_code=201, response_model=str) def parse_recipe_url(url: RecipeURLIn, db: Session = Depends(generate_session)): """ Takes in a URL and attempts to scrape data and load it into the database """ @@ -82,7 +40,7 @@ def parse_recipe_url(url: RecipeURLIn, db: Session = Depends(generate_session)): return recipe.slug -@router.post("/api/recipe/create/") +@router.post("/create/") def create_from_json(data: Recipe, db: Session = Depends(generate_session)) -> str: """ Takes in a JSON string and loads data into the database as a new entry""" new_recipe_slug = data.save_to_db(db) @@ -90,7 +48,7 @@ def create_from_json(data: Recipe, db: Session = Depends(generate_session)) -> s return new_recipe_slug -@router.post("/api/recipe/{recipe_slug}/update/image/") +@router.post("/{recipe_slug}/update/image/") def update_recipe_image( recipe_slug: str, image: bytes = File(...), extension: str = Form(...) ): @@ -101,7 +59,7 @@ def update_recipe_image( return response -@router.post("/api/recipe/{recipe_slug}/update/") +@router.post("/{recipe_slug}/update/") def update_recipe( recipe_slug: str, data: Recipe, db: Session = Depends(generate_session) ): @@ -112,7 +70,7 @@ def update_recipe( return new_slug -@router.delete("/api/recipe/{recipe_slug}/delete/") +@router.delete("/{recipe_slug}/delete/") def delete_recipe(recipe_slug: str, db: Session = Depends(generate_session)): """ Deletes a recipe by slug """ diff --git a/mealie/routes/recipe/tag_routes.py b/mealie/routes/recipe/tag_routes.py new file mode 100644 index 000000000..944b1cdda --- /dev/null +++ b/mealie/routes/recipe/tag_routes.py @@ -0,0 +1,23 @@ +from db.database import db +from db.db_setup import generate_session +from fastapi import APIRouter, Depends +from sqlalchemy.orm.session import Session + +router = APIRouter(tags=["Recipes"]) + +router = APIRouter( + prefix="/api/recipes/tags", + tags=["Recipes"], +) + + +@router.get("/all/") +async def get_all_recipe_tags(session: Session = Depends(generate_session)): + """ Returns a list of available tags in the database """ + return db.tags.get_all_primary_keys(session) + + +@router.get("/{tag}/") +def get_all_recipes_by_tag(tag: str, session: Session = Depends(generate_session)): + """ Returns a list of recipes associated with the provided tag. """ + return db.tags.get(session, tag) diff --git a/mealie/services/category_services.py b/mealie/services/category_services.py index 2a3231c7c..5223a2fa4 100644 --- a/mealie/services/category_services.py +++ b/mealie/services/category_services.py @@ -1,10 +1,13 @@ from typing import List -from models.category_models import Category + from db.database import db -def get_all() -> List[Category]: - categories = db.category.all() - print(categories) - [print(cat) for cat in categories] - return [Category(name=cat) for cat in categories] - return [Category(name="Hej"), Category(name="Fra"), Category(name="Serveren")]; \ No newline at end of file + +def get_all_categories(session) -> List[str]: + categories = db.categories.get_all_primary_keys(session) + + return categories + + +def get_recipes_by_category(session): + pass