Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/unit_and_foods/units.py: 87%
46 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
1from functools import cached_property 1a
3from fastapi import APIRouter, Depends, HTTPException 1a
4from pydantic import UUID4 1a
6from mealie.routes._base.base_controllers import BaseUserController 1a
7from mealie.routes._base.controller import controller 1a
8from mealie.routes._base.mixins import HttpRepo 1a
9from mealie.routes._base.routers import MealieCrudRoute 1a
10from mealie.schema import mapper 1a
11from mealie.schema.recipe.recipe_ingredient import ( 1a
12 CreateIngredientUnit,
13 IngredientUnit,
14 IngredientUnitPagination,
15 MergeUnit,
16 SaveIngredientUnit,
17)
18from mealie.schema.response.pagination import PaginationQuery 1a
19from mealie.schema.response.responses import SuccessResponse 1a
21router = APIRouter(prefix="/units", tags=["Recipes: Units"], route_class=MealieCrudRoute) 1a
24@controller(router) 1a
25class IngredientUnitsController(BaseUserController): 1a
26 @cached_property 1a
27 def repo(self): 1a
28 return self.repos.ingredient_units 1cdefghijkb
30 @cached_property 1a
31 def mixins(self): 1a
32 return HttpRepo[CreateIngredientUnit, IngredientUnit, CreateIngredientUnit]( 1cdefghijkb
33 self.repo,
34 self.logger,
35 self.registered_exceptions,
36 )
38 @router.get("", response_model=IngredientUnitPagination) 1a
39 def get_all(self, q: PaginationQuery = Depends(PaginationQuery), search: str | None = None): 1a
40 response = self.repo.page_all( 1cdefghijb
41 pagination=q,
42 override=IngredientUnit,
43 search=search,
44 )
46 response.set_pagination_guides(router.url_path_for("get_all"), q.model_dump()) 1cdefghijb
47 return response 1cdefghijb
49 @router.post("", response_model=IngredientUnit, status_code=201) 1a
50 def create_one(self, data: CreateIngredientUnit): 1a
51 save_data = mapper.cast(data, SaveIngredientUnit, group_id=self.group_id) 1cdefghijkb
52 return self.mixins.create_one(save_data) 1cdefghijkb
54 @router.put("/merge", response_model=SuccessResponse) 1a
55 def merge_one(self, data: MergeUnit): 1a
56 try:
57 self.repo.merge(data.from_unit, data.to_unit)
58 return SuccessResponse.respond("Successfully merged units")
59 except Exception as e:
60 self.logger.error(e)
61 raise HTTPException(500, "Failed to merge units") from e
63 @router.get("/{item_id}", response_model=IngredientUnit) 1a
64 def get_one(self, item_id: UUID4): 1a
65 return self.mixins.get_one(item_id) 1c
67 @router.put("/{item_id}", response_model=IngredientUnit) 1a
68 def update_one(self, item_id: UUID4, data: CreateIngredientUnit): 1a
69 return self.mixins.update_one(data, item_id)
71 @router.delete("/{item_id}", response_model=IngredientUnit) 1a
72 def delete_one(self, item_id: UUID4): 1a
73 return self.mixins.delete_one(item_id) # type: ignore