11from backend .app .repositories .restaurant_repository import RestaurantRepository
22from backend .app .schemas .restaurant import Restaurant
3+ from backend .app .schemas .menu_section import MenuSection
4+ from backend .app .schemas .menu_item import MenuItem
5+ from backend .app .schemas .menu_item_request import MenuItemCreate , MenuItemUpdate
6+ from backend .app .schemas .menu import Menu
37
8+ UNCATEGORIZED_MENU_SECTION_ID = "uncategorized"
9+ UNCATEGORIZED_MENU_SECTION_NAME = "Uncategorized"
410
511class RestaurantService :
612 def __init__ (self , repository : RestaurantRepository ):
@@ -32,4 +38,120 @@ def get_sort_value(r):
3238 return restaurantsList
3339
3440 def get_restaurant_by_id (self , restaurant_id : str ):
35- return self .repository .get_restaurant_by_id (restaurant_id )
41+ return self .repository .get_restaurant_by_id (restaurant_id )
42+
43+ def _find_restaurant (self , restaurants : list [Restaurant ], restaurant_id : str ) -> Restaurant :
44+ for restaurant in restaurants :
45+ if restaurant .id == restaurant_id :
46+ return restaurant
47+ raise ValueError (f"Restaurant with id '{ restaurant_id } ' is not found" )
48+
49+ def _find_menu_section (self , menu_sections : MenuSection | list [MenuSection ], menu_section_id : str ) -> MenuSection :
50+ section_list = (menu_sections if isinstance (menu_sections , list ) else [menu_sections ])
51+
52+ for menu_section in section_list :
53+ if menu_section .id == menu_section_id :
54+ return menu_section
55+
56+ if menu_section .hasMenuSection is not None :
57+ try :
58+ return self ._find_menu_section (menu_section .hasMenuSection , menu_section_id )
59+
60+ except ValueError :
61+ pass
62+
63+ raise ValueError (f"MenuSection with id '{ menu_section_id } ' is not found" )
64+
65+ def add_menu_item (self , restaurant_id : str , request : MenuItemCreate ) -> MenuItem :
66+ restaurants = self .repository .get_all_restaurants ()
67+ restaurant = self ._find_restaurant (restaurants , restaurant_id )
68+
69+ menu_section = self ._get_or_create_uncategorized_section (restaurant )
70+ new_menu_item = MenuItem (** request .model_dump ())
71+
72+ if menu_section .hasMenuItem is None :
73+ menu_section .hasMenuItem = [new_menu_item ]
74+ elif isinstance (menu_section .hasMenuItem , list ):
75+ menu_section .hasMenuItem .append (new_menu_item )
76+ else :
77+ menu_section .hasMenuItem = [menu_section .hasMenuItem , new_menu_item ]
78+
79+ self .repository .save_restaurants (restaurants )
80+
81+ return new_menu_item
82+
83+ def update_menu_item (self , restaurant_id : str , menu_item_id : str , request : MenuItemUpdate ) -> MenuItem :
84+ restaurants = self .repository .get_all_restaurants ()
85+ restaurant = self ._find_restaurant (restaurants , restaurant_id )
86+
87+ if restaurant .hasMenu is None :
88+ raise ValueError (f"MenuItem with id '{ menu_item_id } ' is not found" )
89+
90+ _ , menu_item = self ._find_menu_item (restaurant .hasMenu .hasMenuSection , menu_item_id )
91+
92+ update_data = request .model_dump (exclude_unset = True )
93+
94+ for field , value in update_data .items ():
95+ setattr (menu_item , field , value )
96+
97+ self .repository .save_restaurants (restaurants )
98+ return menu_item
99+
100+
101+ def delete_menu_item (self , restaurant_id : str , menu_item_id : str ) -> None :
102+ restaurants = self .repository .get_all_restaurants ()
103+ restaurant = self ._find_restaurant (restaurants , restaurant_id )
104+
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 )
109+ 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 )
114+
115+ def _get_or_create_uncategorized_section (self , restaurant : Restaurant ) -> MenuSection :
116+
117+ if restaurant .hasMenu is None :
118+ uncategorized_section = MenuSection (id = UNCATEGORIZED_MENU_SECTION_ID , name = UNCATEGORIZED_MENU_SECTION_NAME , description = "" , hasMenuItem = [])
119+
120+ restaurant .hasMenu = Menu (hasMenuSection = [uncategorized_section ])
121+
122+ return uncategorized_section
123+
124+ menu_sections = (restaurant .hasMenu .hasMenuSection if isinstance (restaurant .hasMenu .hasMenuSection , list ) else [restaurant .hasMenu .hasMenuSection ])
125+
126+ for section in menu_sections :
127+ if section .id == UNCATEGORIZED_MENU_SECTION_ID :
128+ return section
129+
130+ uncategorized_section = MenuSection (id = UNCATEGORIZED_MENU_SECTION_ID , name = UNCATEGORIZED_MENU_SECTION_NAME , description = "" , hasMenuItem = [])
131+
132+ menu_sections .append (uncategorized_section )
133+ restaurant .hasMenu .hasMenuSection = menu_sections
134+
135+ 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