Skip to content

Commit

Permalink
feat: ingredient delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
zubkovmd committed Mar 3, 2024
1 parent d19e917 commit 792e9c4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
23 changes: 22 additions & 1 deletion app/api/routes/v1/recipes/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from fastapi import Depends, UploadFile, Form, File, APIRouter, Body, Query, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from starlette import status

from app.api.routes.default_response_models import DefaultResponse, DefaultResponseWithPayload
from app.api.routes.v1.recipes.utility_classes import (
Expand All @@ -27,7 +28,7 @@
from app.api.routes.v1.utils.service_models import UserModel
from app.api.routes.v1.utils.utility import build_full_path
from app.database import DatabaseManagerAsync
from app.database.models.base import RecipeCategories
from app.database.models.base import RecipeCategories, Ingredients
from app.utils import S3Manager

router = APIRouter(prefix="/recipes")
Expand Down Expand Up @@ -452,6 +453,26 @@ async def get_ingredients(
return await get_ingredients_view(session)


@router.delete("/utils/delete_ingredient")
async def delete_ingredient(
ingredient_id: int,
session: AsyncSession = Depends(DatabaseManagerAsync.get_instance().get_session_object)
):
"""
Route will delete ingredients registered in this service
:param ingredient_id: Ingredient id
:param session: SqlAlchemy AsyncSession object
:return: Nothing
"""
ingredient = await Ingredients.get_by_id(ingredient_id=ingredient_id, session=session)
if not ingredient:
return DefaultResponse(status_code=status.HTTP_404_NOT_FOUND, detail="Ингредиент не найден")
await session.delete(ingredient)
await session.commit()
return DefaultResponse(status_code=200, detail="Ингредиент удален")


@router.get("/utils/get_available_ingredients_with_groups", response_model=GetIngredientsWithGroupsResponseModel)
async def get_ingredients_with_groups(
session: AsyncSession = Depends(DatabaseManagerAsync.get_instance().get_session_object)
Expand Down
2 changes: 1 addition & 1 deletion app/api/routes/v1/recipes/utility_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class GetRecipesResponseModel(BaseModel):

class GetIngredientsResponseModel(BaseModel):
"""Model for listed available ingredients response"""
ingredients: List[str]
ingredients: List[IngredientFindResponseModel]


class IngredientWithGroupResponseModel(BaseModel):
Expand Down
20 changes: 13 additions & 7 deletions app/api/routes/v1/recipes/views/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,21 @@ async def get_ingredients_view(session: AsyncSession) -> GetIngredientsResponseM
:param session: SQLAlchemy AsyncSession object.
:return: Response with existing ingredients.
"""
async with session.begin():
stmt = sqlalchemy.select(Ingredients.name)
async with (session.begin()):
stmt = sqlalchemy.select(Ingredients)
response = await session.execute(stmt)
ingredients: List[str] = response.fetchall()
if ingredients:
ingredients = [i[0] for i in ingredients]
loaded_ingredients: List[Ingredients] = response.fetchall()
if loaded_ingredients:
output_ingredients: List[IngredientFindResponseModel] = [
IngredientFindResponseModel(
name = ingredient[0].name,
ingredient_id = ingredient[0].id
)
for ingredient in loaded_ingredients
]
else:
ingredients = []
return GetIngredientsResponseModel(ingredients=ingredients)
output_ingredients: List[IngredientFindResponseModel] = []
return GetIngredientsResponseModel(ingredients=output_ingredients)


async def get_ingredients_with_groups_view(session: AsyncSession) -> GetIngredientsWithGroupsResponseModel:
Expand Down
18 changes: 18 additions & 0 deletions app/database/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,24 @@ async def get_by_name_or_create(cls, ingredient: CreateRecipeIngredientRequestMo
session.add(new_ingredient_model)
return new_ingredient_model

@classmethod
async def get_by_id(cls, ingredient_id: int, session: AsyncSession) \
-> IngredientsTypeVar:
"""
Method return ingredient by passed name. If ingredient does not exist, then it will be created.
Ingredient groups will be added too.
For additional info check app.database.models.base -> Ingredients.
:param ingredient: ingredient name.
:param session: SQLAlchemy AsyncSession object.
:return: Ingredient group mapped object.
"""
stmt = sqlalchemy.select(Ingredients).where(Ingredients.id == ingredient_id)
response = await session.execute(stmt)
found_ingredient = response.scalars().first()
return found_ingredient



class RecipeDimensions(Base):
"""
Expand Down

0 comments on commit 792e9c4

Please sign in to comment.