refactor/recipe routers

This commit is contained in:
hayden 2021-01-30 17:24:19 -09:00
commit 016108d35f
5 changed files with 119 additions and 62 deletions

View file

@ -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

View file

@ -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)

View file

@ -1,58 +1,20 @@
from typing import List, Optional
from db.db_setup import generate_session from db.db_setup import generate_session
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query from fastapi import APIRouter, Depends, File, Form, HTTPException, Query
from fastapi.responses import FileResponse 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.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 services.scrape_services import create_from_url
from sqlalchemy.orm.session import Session from sqlalchemy.orm.session import Session
from utils.snackbar import SnackResponse 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]) @router.get("/{recipe_slug}/", response_model=Recipe)
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)
def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)): def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
""" Takes in a recipe slug, returns all data for a recipe """ """ Takes in a recipe slug, returns all data for a recipe """
recipe = Recipe.get_by_slug(db, recipe_slug) 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 return recipe
@router.get("/api/recipe/image/{recipe_slug}/") @router.get("/image/{recipe_slug}/")
def get_recipe_img(recipe_slug: str): def get_recipe_img(recipe_slug: str):
""" Takes in a recipe slug, returns the static image """ """ Takes in a recipe slug, returns the static image """
recipe_image = read_image(recipe_slug) recipe_image = read_image(recipe_slug)
@ -68,11 +30,7 @@ def get_recipe_img(recipe_slug: str):
return FileResponse(recipe_image) return FileResponse(recipe_image)
@router.post( @router.post("/create-url/", status_code=201, response_model=str)
"/api/recipe/create-url/",
status_code=201,
response_model=str,
)
def parse_recipe_url(url: RecipeURLIn, db: Session = Depends(generate_session)): 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 """ """ 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 return recipe.slug
@router.post("/api/recipe/create/") @router.post("/create/")
def create_from_json(data: Recipe, db: Session = Depends(generate_session)) -> str: 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""" """ Takes in a JSON string and loads data into the database as a new entry"""
new_recipe_slug = data.save_to_db(db) 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 return new_recipe_slug
@router.post("/api/recipe/{recipe_slug}/update/image/") @router.post("/{recipe_slug}/update/image/")
def update_recipe_image( def update_recipe_image(
recipe_slug: str, image: bytes = File(...), extension: str = Form(...) recipe_slug: str, image: bytes = File(...), extension: str = Form(...)
): ):
@ -101,7 +59,7 @@ def update_recipe_image(
return response return response
@router.post("/api/recipe/{recipe_slug}/update/") @router.post("/{recipe_slug}/update/")
def update_recipe( def update_recipe(
recipe_slug: str, data: Recipe, db: Session = Depends(generate_session) recipe_slug: str, data: Recipe, db: Session = Depends(generate_session)
): ):
@ -112,7 +70,7 @@ def update_recipe(
return new_slug 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)): def delete_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
""" Deletes a recipe by slug """ """ Deletes a recipe by slug """

View file

@ -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)

View file

@ -1,10 +1,13 @@
from typing import List from typing import List
from models.category_models import Category
from db.database import db from db.database import db
def get_all() -> List[Category]:
categories = db.category.all() def get_all_categories(session) -> List[str]:
print(categories) categories = db.categories.get_all_primary_keys(session)
[print(cat) for cat in categories]
return [Category(name=cat) for cat in categories] return categories
return [Category(name="Hej"), Category(name="Fra"), Category(name="Serveren")];
def get_recipes_by_category(session):
pass