33from backend .app .schemas .menu_section import MenuSection
44from backend .app .schemas .menu_item import MenuItem
55from backend .app .schemas .menu_item_request import MenuItemCreate , MenuItemUpdate
6+ from backend .app .schemas .menu import Menu
67
78UNCATEGORIZED_MENU_SECTION_ID = "uncategorized"
89UNCATEGORIZED_MENU_SECTION_NAME = "Uncategorized"
@@ -79,43 +80,47 @@ def add_menu_item(self, restaurant_id: str, request: MenuItemCreate) -> MenuItem
7980
8081 return new_menu_item
8182
82- def update_menu_item (self , restaurant_id : str , menu_section_id : str , menu_item_id : str , request : MenuItemUpdate ) -> MenuItem :
83+ def update_menu_item (self , restaurant_id : str , menu_item_id : str , request : MenuItemUpdate ) -> MenuItem :
8384 restaurants = self .repository .get_all_restaurants ()
8485 restaurant = self ._find_restaurant (restaurants , restaurant_id )
85- menu_section = self ._find_menu_section (restaurant .hasMenu .hasMenuSection , menu_section_id )
86- menu_items = (menu_section .hasMenuItem if isinstance (menu_section .hasMenuItem , list ) else [menu_section .hasMenuItem ])
8786
88- for menu_item in menu_items :
89- if menu_item .id == menu_item_id :
90- update_data = request .model_dump (exclude_unset = True )
87+ if restaurant .hasMenu is None :
88+ raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
9189
92- for field , value in update_data .items ():
93- setattr (menu_item , field , value )
90+ _ , menu_item = self ._find_menu_item (restaurant .hasMenu .hasMenuSection , menu_item_id )
9491
95- self . repository . save_restaurants ( restaurants )
92+ update_data = request . model_dump ( exclude_unset = True )
9693
97- return menu_item
94+ for field , value in update_data .items ():
95+ setattr (menu_item , field , value )
9896
99- raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
97+ self .repository .save_restaurants (restaurants )
98+ return menu_item
10099
101- def delete_menu_item (self , restaurant_id : str , menu_section_id : str , menu_item_id : str ) -> None :
100+
101+ def delete_menu_item (self , restaurant_id : str , menu_item_id : str ) -> None :
102102 restaurants = self .repository .get_all_restaurants ()
103103 restaurant = self ._find_restaurant (restaurants , restaurant_id )
104104
105- menu_section = self ._find_menu_section (restaurant .hasMenu .hasMenuSection , menu_section_id )
105+ if restaurant .hasMenu is None :
106+ raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
107+
108+ menu_section , menu_item = self ._find_menu_item (restaurant .hasMenu .hasMenuSection , menu_item_id )
106109 menu_items = (menu_section .hasMenuItem if isinstance (menu_section .hasMenuItem , list ) else [menu_section .hasMenuItem ])
110+ menu_items .remove (menu_item )
111+ menu_section .hasMenuItem = menu_items
112+
113+ self .repository .save_restaurants (restaurants )
107114
108- for menu_item in menu_items :
109- if menu_item .id == menu_item_id :
110- menu_items .remove (menu_item )
111- menu_section .hasMenuItem = menu_items
115+ def _get_or_create_uncategorized_section (self , restaurant : Restaurant ) -> MenuSection :
112116
113- self . repository . save_restaurants ( restaurants )
114- return
117+ if restaurant . hasMenu is None :
118+ uncategorized_section = MenuSection ( id = UNCATEGORIZED_MENU_SECTION_ID , name = UNCATEGORIZED_MENU_SECTION_NAME , description = "" , hasMenuItem = [])
115119
116- raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
120+ restaurant .hasMenu = Menu (hasMenuSection = [uncategorized_section ])
121+
122+ return uncategorized_section
117123
118- def _get_or_create_uncategorized_section (self , restaurant : Restaurant ) -> MenuSection :
119124 menu_sections = (restaurant .hasMenu .hasMenuSection if isinstance (restaurant .hasMenu .hasMenuSection , list ) else [restaurant .hasMenu .hasMenuSection ])
120125
121126 for section in menu_sections :
@@ -128,3 +133,25 @@ def _get_or_create_uncategorized_section(self, restaurant: Restaurant) -> MenuSe
128133 restaurant .hasMenu .hasMenuSection = menu_sections
129134
130135 return uncategorized_section
136+
137+
138+ def _find_menu_item (self , menu_sections : MenuSection | list [MenuSection ], menu_item_id : str ) -> tuple [MenuSection , MenuItem ]:
139+
140+ section_list = (menu_sections if isinstance (menu_sections , list ) else [menu_sections ])
141+
142+ for section in section_list :
143+ if section .hasMenuItem is not None :
144+ menu_items = (section .hasMenuItem if isinstance (section .hasMenuItem , list ) else [section .hasMenuItem ])
145+
146+ for item in menu_items :
147+ if item .id == menu_item_id :
148+ return section , item
149+
150+ if section .hasMenuSection is not None :
151+ try :
152+ return self ._find_menu_item (section .hasMenuSection , menu_item_id )
153+ except ValueError :
154+ pass
155+
156+ raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
157+
0 commit comments