mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-13 13:55:23 -05:00
* upgrade sqlalchemy to 2.0 * rewrite all db models to sqla 2.0 mapping api * fix some importing and typing weirdness * fix types of a lot of nullable columns * remove get_ref methods * fix issues found by tests * rewrite all queries in repository_recipe to 2.0 style * rewrite all repository queries to 2.0 api * rewrite all remaining queries to 2.0 api * remove now-unneeded __allow_unmapped__ flag * remove and fix some unneeded cases of "# type: ignore" * fix formatting * bump black version * run black * can this please be the last one. okay. just. okay. * fix repository errors * remove return * drop open API validator --------- Co-authored-by: Sören Busch <fleshgolem@gmx.net>
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
from pydantic import UUID4
|
|
from sqlalchemy import select
|
|
|
|
from mealie.db.models.recipe.ingredient import IngredientUnitModel
|
|
from mealie.schema.recipe.recipe_ingredient import IngredientUnit
|
|
|
|
from .repository_generic import RepositoryGeneric
|
|
|
|
|
|
class RepositoryUnit(RepositoryGeneric[IngredientUnit, IngredientUnitModel]):
|
|
def _get_unit(self, id: UUID4) -> IngredientUnitModel:
|
|
stmt = select(self.model).filter_by(**self._filter_builder(**{"id": id}))
|
|
return self.session.execute(stmt).scalars().one()
|
|
|
|
def merge(self, from_unit: UUID4, to_unit: UUID4) -> IngredientUnit | None:
|
|
from_model = self._get_unit(from_unit)
|
|
to_model = self._get_unit(to_unit)
|
|
|
|
to_model.ingredients += from_model.ingredients
|
|
|
|
try:
|
|
self.session.delete(from_model)
|
|
self.session.commit()
|
|
except Exception as e:
|
|
self.session.rollback()
|
|
raise e
|
|
|
|
return self.get_one(to_unit)
|
|
|
|
def by_group(self, group_id: UUID4) -> "RepositoryUnit":
|
|
return super().by_group(group_id)
|