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.

MenuOptionRepository.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Microsoft.EntityFrameworkCore;
  2. using ProRestaurant.Classes;
  3. using ProRestaurant.Containers;
  4. using ProRestaurant.DBContexts;
  5. using ProRestaurant.Models.Restaurants;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace ProRestaurant.Repository.Restaurants
  10. {
  11. public class MenuOptionRepository : IMenuOptionRepository
  12. {
  13. private readonly DBContext dBContext;
  14. public MenuOptionRepository(DBContext _dBContext)
  15. {
  16. dBContext = _dBContext;
  17. }
  18. public void Delete(MenuOption Option)
  19. {
  20. if (Option.Options != null)
  21. {
  22. foreach (var item in Option.Options)
  23. {
  24. DeleteItem(item);
  25. }
  26. }
  27. dBContext.MenuOptions.Remove(Option);
  28. Save();
  29. }
  30. public void DeleteItem(int id)
  31. {
  32. var item = dBContext.MenuOptionItems.Where(i => i.Id == id).FirstOrDefault();
  33. dBContext.MenuOptionItems.Remove(item);
  34. Save();
  35. }
  36. public void DeleteItem(MenuOptionItem item)
  37. {
  38. dBContext.MenuOptionItems.Remove(item);
  39. Save();
  40. }
  41. public MenuOption GetMenuOption(int id)
  42. {
  43. var option = dBContext.MenuOptions.Where(m => m.Id == id).FirstOrDefault();
  44. if (option == null)
  45. {
  46. option = new MenuOption()
  47. {
  48. OptionType = OptionType.Single,
  49. IsBasePrice = false,
  50. Options = new List<MenuOptionItem>(),
  51. CategoryDescription = "All"
  52. };
  53. }
  54. else
  55. {
  56. option.Options = dBContext.MenuOptionItems.Where(m => m.MenuOption == option).ToList();
  57. if (option.CategoryId > 0)
  58. option.CategoryDescription = dBContext.MenuCategories.Where(m => m.Id == option.CategoryId).FirstOrDefault().Description;
  59. else
  60. option.CategoryDescription = "All";
  61. }
  62. return option;
  63. }
  64. public List<OptionTypeContainer> GetOptionTypes()
  65. {
  66. //TODO: hard coded for now, will need to find solution.
  67. var options = new List<OptionTypeContainer>
  68. {
  69. new OptionTypeContainer() { Id = 0, Description = "Single" },
  70. new OptionTypeContainer() { Id = 1, Description = "Single Required" },
  71. new OptionTypeContainer() { Id = 2, Description = "Multiple" },
  72. new OptionTypeContainer() { Id = 3, Description = "Multiple Limit" }
  73. };
  74. return options;
  75. }
  76. public List<MenuOption> GetRestaurantMenuOptions(int restaurantId)
  77. {
  78. var options = (from o in dBContext.MenuOptions
  79. where o.RestaurantId == restaurantId
  80. orderby o.Rank
  81. select o).ToList();
  82. foreach (var option in options)
  83. {
  84. switch (option.OptionType)
  85. {
  86. case OptionType.MultipleLimit:
  87. option.OptionTypeDescription = "Multiple Limit";
  88. break;
  89. case OptionType.SingleRequired:
  90. option.OptionTypeDescription = "Single Required";
  91. break;
  92. default:
  93. option.OptionTypeDescription = option.OptionType.ToString();
  94. break;
  95. }
  96. if (option.CategoryId > 0)
  97. option.CategoryDescription = dBContext.MenuCategories.Where(m => m.Id == option.CategoryId).FirstOrDefault().Description;
  98. else
  99. option.CategoryDescription = "All";
  100. }
  101. return options;
  102. }
  103. public void Insert(MenuOption Option)
  104. {
  105. dBContext.Add(Option);
  106. Save();
  107. }
  108. public void Save()
  109. {
  110. dBContext.SaveChanges();
  111. }
  112. public void Update(MenuOption Option)
  113. {
  114. dBContext.Entry(Option).State = EntityState.Modified;
  115. foreach (var opt in Option.Options)
  116. {
  117. if (opt.Id > 0)
  118. {
  119. dBContext.Entry(opt).State = EntityState.Modified;
  120. }
  121. else
  122. {
  123. dBContext.Add(opt);
  124. }
  125. }
  126. Save();
  127. }
  128. }
  129. }