public string CreateNewMenu([FromBody] string req) { var result = String.Empty; var request = new SaveMenuRequest(); var response = new SaveMenuResponse(); try { request = JsonConvert.DeserializeObject <SaveMenuRequest>(req); MenuService service = new MenuService(); response = service.PerformCreateNewMenu(request); } catch (Exception ex) { response = new SaveMenuResponse() { MessageString = ex.Message, MessageStatusID = (byte)EMessageStatus.Success }; } result = JsonConvert.SerializeObject(response); return(result); }
public SaveMenuResponse PerformCreateNewMenu(SaveMenuRequest request) { SaveMenuResponse response = new SaveMenuResponse(); var menu = new Menu(); var menuMenuItems = new List <MenuMenuItem>(); try { using (var db = new CuddlyWombatEntities()) { db.Configuration.LazyLoadingEnabled = false; db.Configuration.ProxyCreationEnabled = false; if (request.MenuID == 0) { menu.MenuID = 0; menu.Name = request.Name; menu.MenuTypeID = request.MenuTypeID; menu.Description = request.Description; menu.DateCreated = DateTime.Now; menu.IsUsed = true; db.Menus.Add(menu); db.SaveChanges(); } else { menu = (from m in db.Menus where m.MenuID == request.MenuID select m).FirstOrDefault(); menu.Name = request.Name; menu.MenuTypeID = request.MenuTypeID; menu.Description = request.Description; menu.IsUsed = true; var allAssignedMenuItems = (from m in db.MenuMenuItems where m.MenuID == request.MenuID select m).ToList(); foreach (var selectedMenuItem in allAssignedMenuItems) { db.MenuMenuItems.Remove(selectedMenuItem); } } foreach (var mi in request.MenuItemIDs) { var menuMenuItem = new MenuMenuItem() { MenuID = menu.MenuID, MenuItemID = mi }; menuMenuItems.Add(menuMenuItem); } db.MenuMenuItems.AddRange(menuMenuItems); db.SaveChanges(); response.Menus = (from m in db.Menus.Include("MenuType") select m).ToList(); foreach (var menu_ in response.Menus) { menu_.MenuType.Menus = null; menu_.MenuItems = (from m in db.MenuMenuItems join mi in db.MenuItems on m.MenuItemID equals mi.MenuItemID where m.MenuID == menu.MenuID select mi).ToList(); } response.MessageString = "New Menu is successfully added to the database"; response.MessageStatusID = (byte)EMessageStatus.Success; } } catch (Exception ex) { response.MessageStatusID = (byte)EMessageStatus.Exception; response.MessageString = ex.Message; } return(response); }