You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IMenuRepository.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using ProRestaurant.Classes;
  2. using ProRestaurant.Containers;
  3. using ProRestaurant.DBContexts;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace ProRestaurant.Repository.Restaurants
  9. {
  10. public interface IMenuRepository
  11. {
  12. RestaurantMenuContainer GetMenu(int restaurantId);
  13. MenuContainer GetOptions(int MenuID, int CategoryID);
  14. }
  15. public class MenuRepository : IMenuRepository
  16. {
  17. private readonly DBContext dBContext;
  18. public MenuRepository(DBContext db)
  19. {
  20. dBContext = db;
  21. }
  22. public RestaurantMenuContainer GetMenu(int restaurantId)
  23. {
  24. var restaurant = dBContext.Restaurants.Where(r => r.Id == restaurantId).FirstOrDefault();
  25. var menu = new RestaurantMenuContainer
  26. {
  27. Id = restaurant.Id,
  28. Name = restaurant.Name,
  29. Categories = restaurant.Categories,
  30. DeliveryTime = restaurant.DeliveryTime,
  31. DeliveryFee = string.Format("R {0:N}", restaurant.DeliveryFee),
  32. Suburb = restaurant.Suburb,
  33. City = restaurant.City,
  34. Province = restaurant.Province,
  35. PostalCode = restaurant.PostalCode,
  36. Latitude = restaurant.Latitude,
  37. Longitude = restaurant.Longitude,
  38. Logo = restaurant.Logo.StartsWith("data:image") ? restaurant.Logo : ImageFormatter.ImageToBase64(restaurant.Logo),
  39. CategoryList = new List<string>(),
  40. TradingHours = new List<TradingHourContainer>(),
  41. MenuList = new List<MenuCategoryContainer>()
  42. };
  43. var categories = dBContext.MenuCategories.Where(c => c.RestaurantId == restaurantId).ToList();
  44. foreach (var cat in categories)
  45. {
  46. menu.CategoryList.Add(cat.Description);
  47. MenuCategoryContainer menuItem = new MenuCategoryContainer
  48. {
  49. Category = cat.Description,
  50. Items = new List<MenuItemContainer>()
  51. };
  52. var MenuItems = dBContext.MenuItems.Where(mi => mi.CategoryId == cat.Id).ToList();
  53. foreach (var item in MenuItems)
  54. {
  55. menuItem.Items.Add(new MenuItemContainer
  56. {
  57. MenuId = item.Id,
  58. CategoryId = item.CategoryId,
  59. Name = item.Name,
  60. Description = item.Description,
  61. Price = string.Format("{0:C}", item.Price),
  62. Image = item.Image.StartsWith("data:image") ? item.Image : ImageFormatter.ImageToBase64(item.Image)
  63. });
  64. }
  65. menu.MenuList.Add(menuItem);
  66. }
  67. var tradingHours = dBContext.TradingHours.Where(t => t.RestaurantId == restaurantId).ToList();
  68. foreach (var tHours in tradingHours)
  69. {
  70. var hour = new TradingHourContainer()
  71. {
  72. Description = tHours.Description
  73. };
  74. if (tHours.Closed)
  75. hour.Time = "Closed";
  76. else if (tHours.Opened24H)
  77. hour.Time = "Open 24 Hours";
  78. else
  79. hour.Time = string.Format("{0:hh:mm} - {1:hh:mm}", tHours.OpeningTime, tHours.ClosingTime);
  80. menu.TradingHours.Add(hour);
  81. }
  82. return menu;
  83. }
  84. public MenuContainer GetOptions(int MenuID, int CategoryID)
  85. {
  86. var mItem = dBContext.MenuItems.Where(m => m.Id == MenuID).FirstOrDefault();
  87. MenuContainer menu = new MenuContainer
  88. {
  89. Id = MenuID,
  90. Name = mItem.Name,
  91. Description = mItem.Description,
  92. Price = mItem.Price,
  93. DisplayPrice = string.Format("{0:C}", mItem.Price),
  94. Image = mItem.Image.StartsWith("data:image") ? mItem.Image : ImageFormatter.ImageToBase64(mItem.Image),
  95. Options = new List<OptionContainer>()
  96. };
  97. var menuOptions = dBContext.MenuOptions.Where(mo => mo.CategoryId == CategoryID).OrderBy(mo => mo.Rank).ToList();
  98. foreach (var option in menuOptions)
  99. {
  100. OptionContainer opt = new OptionContainer
  101. {
  102. Description = option.Description,
  103. OptionType = option.OptionType.ToString(),
  104. Limit = option.OptionLimit,
  105. IsBasePrice = option.IsBasePrice,
  106. SpecialInstructions = "",
  107. Options = new List<OptionItemContainer>()
  108. };
  109. switch (option.OptionType)
  110. {
  111. case OptionType.SingleRequired:
  112. opt.SubTitle = "Required";
  113. break;
  114. case OptionType.MultipleLimit:
  115. opt.SubTitle = string.Format("Choose {0}", opt.Limit);
  116. break;
  117. default:
  118. opt.SubTitle = "";
  119. break;
  120. }
  121. var menuItems = dBContext.MenuOptionItems.Where(i => i.MenuOption == option).OrderBy(i => i.Rank).ToList();
  122. foreach (var item in menuItems)
  123. {
  124. opt.Options.Add(new OptionItemContainer()
  125. {
  126. Description = item.Description,
  127. Price = item.Price
  128. });
  129. }
  130. menu.Options.Add(opt);
  131. }
  132. menuOptions = dBContext.MenuOptions.Where(mo => mo.CategoryId == null).OrderBy(mo => mo.Rank).ToList();
  133. foreach (var option in menuOptions)
  134. {
  135. OptionContainer opt = new OptionContainer
  136. {
  137. Description = option.Description,
  138. OptionType = option.OptionType.ToString(),
  139. Limit = option.OptionLimit,
  140. IsBasePrice = option.IsBasePrice,
  141. SpecialInstructions = "",
  142. Options = new List<OptionItemContainer>()
  143. };
  144. switch (option.OptionType)
  145. {
  146. case OptionType.SingleRequired:
  147. opt.SubTitle = "Required";
  148. break;
  149. case OptionType.MultipleLimit:
  150. opt.SubTitle = string.Format("Choose {0}", opt.Limit);
  151. break;
  152. default:
  153. opt.SubTitle = "";
  154. break;
  155. }
  156. var menuItems = dBContext.MenuOptionItems.Where(i => i.MenuOption == option).OrderBy(i => i.Rank).ToList();
  157. foreach (var item in menuItems)
  158. {
  159. opt.Options.Add(new OptionItemContainer()
  160. {
  161. Description = item.Description,
  162. Price = item.Price
  163. });
  164. }
  165. menu.Options.Add(opt);
  166. }
  167. return menu;
  168. }
  169. }
  170. }