public IActionResult Edit(int key = 0) { if (key <= 0) { return(BadRequest(String.Format(_localizer[ErrorConstants.ARGUMENT_IS_MISSING], "Target menu"))); } MenuDTO menu = _mnuService.FindByKey(key); if (menu == null) { return(BadRequest(String.Format(_localizer[ErrorConstants.MENU_NOT_EXISTED]))); } UpdateMenuViewModel model = new UpdateMenuViewModel(_mnuService.GetAll().ToMenuViewModels(true)); model.Key = menu.Key; model.Name = menu.Name; model.Url = menu.Url; model.Order = menu.Order; model.ParentKey = menu.ParentMenuKey; model.Visible = menu.Visible; return(ViewComponent("EditMenu", model)); }
public UpdateMenuViewModel UpdateViewModel(UpdateMenuViewModel viewModel) { viewModel.RoleGroupOptions = _roleService.GetRoleGroups( new Services.Requests.RoleGroup.GetRoleGroupsRequest { Skip = 0, Take = 0 }).RoleGroups.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList(); List <SelectListItem> menuList = _menuService.GetMenus( new Services.Requests.Menu.GetMenusRequest { Skip = 0, Take = 0 }).Menus.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList(); //menuList.Insert(0, new SelectListItem { Text = "This is Root", Value = "0" }); viewModel.MenuOptions = menuList; return(viewModel); }
public UpdateMenuViewModel GetFakeMenu() { var model = new UpdateMenuViewModel() { LunchDate = DateTime.Now.ToString(DateFormat), MenuId = 1, Price = 1.1, //Title = "Test", Sections = new List <MenuSectionViewModel>() }; model.Sections.Add(new MenuSectionViewModel() { MenuId = 1, MenuSectionId = 1, Name = "Салаты", Number = 1, Items = new List <MenuItemViewModel>() }); model.Sections.Add(new MenuSectionViewModel() { MenuId = 1, MenuSectionId = 2, Name = "Супы", Number = 2, Items = new List <MenuItemViewModel>() }); model.Sections.Add(new MenuSectionViewModel() { MenuId = 1, MenuSectionId = 3, Name = "Горячее", Number = 3, Items = new List <MenuItemViewModel>() }); model.Sections.Add(new MenuSectionViewModel() { MenuId = 1, MenuSectionId = 4, Name = "Гарнир", Number = 4, Items = new List <MenuItemViewModel>() }); model.Sections.Add(new MenuSectionViewModel() { MenuId = 1, MenuSectionId = 5, Name = "Напитки", Number = 5, Items = new List <MenuItemViewModel>() }); model.Sections[0].Items.Add(new MenuItemViewModel() { MenuSectionId = 1, Number = 1, Name = "Test", MenuItemId = 1 }); return(model); }
public UpdateMenuViewModel GetLastMenu() { var lastMenu = _context.Menus.OrderByDescending(m => m.LunchDate).FirstOrDefault(); if (lastMenu == null) { return(GetEmptyMenu()); } var model = new UpdateMenuViewModel() { LunchDate = lastMenu.LunchDate.ToString(DateFormat), MenuId = lastMenu.MenuId, Price = lastMenu.Price, Editable = lastMenu.Editable, Sections = _context.MenuSections.Select(s => new MenuSectionViewModel() { MenuId = lastMenu.MenuId, Name = s.Name, MenuSectionId = s.MenuSectionId, Number = s.Number, Items = _context.MenuItems .Where(i => i.MenuId == lastMenu.MenuId && i.MenuSectionId == s.MenuSectionId).Select( i => new MenuItemViewModel() { MenuSectionId = s.MenuSectionId, Number = i.Number, Name = i.Name, MenuItemId = i.MenuItemId }).ToList() }).ToList() }; return(model); }
private UpdateMenuViewModel UpdateMenuSections(UpdateMenuViewModel model) { foreach (var section in model.Sections) { section.MenuId = model.MenuId; var id = UpdateMenuSection(section); section.MenuSectionId = id; } return model; }
protected static void MessFakeMenu(UpdateMenuViewModel model) { model.Price = model.Price + new Random().Next(1, 2000); foreach (var section in model.Sections) { foreach (var item in section.Items) { item.Name = Guid.NewGuid().ToString(); } } }
public IActionResult Update([FromBody] UpdateMenuViewModel model) { try { var savedModel = _menuService.UpdateMenu(model); return(Ok(savedModel)); } catch (Exception e) { return(BadRequest(e)); } }
public ActionResult Update(UpdateMenuViewModel viewModel) { var request = viewModel.MapTo <UpdateMenuRequest>(); var response = _menuService.Update(request); TempData["IsSuccess"] = response.IsSuccess; TempData["Message"] = response.Message; if (response.IsSuccess) { return(RedirectToAction("Index")); } return(View("Update", viewModel)); }
private UpdateMenuViewModel GetViewModel(Menu menu = null) { var menuId = menu?.MenuId ?? 0; var price = menu != null ? menu.Price : 0; var editable = CheckIfMenuIsEditable(menuId); var date = menu?.LunchDate.ToString(_dateFormat) ?? DateTime.Now.NextFriday().ToString(_dateFormat); var model = new UpdateMenuViewModel() { LunchDate = date, MenuId = menuId, Price = price, Editable = editable, Sections = _context.MenuSections.Select(s => new MenuSectionViewModel() { MenuId = menuId, Name = s.Name, MenuSectionId = s.MenuSectionId, Number = s.Number, Items = menu != null ? _context.MenuItems .Where(i => i.MenuId == menuId && i.MenuSectionId == s.MenuSectionId) .Select( i => new MenuItemViewModel() { MenuSectionId = s.MenuSectionId, Number = i.Number, Name = i.Name, MenuItemId = i.MenuItemId, MenuId = menuId }) .ToList() : new List<MenuItemViewModel>() { new MenuItemViewModel() { MenuId = 0, MenuItemId = 0, MenuSectionId = s.MenuSectionId, Name = string.Empty, Number = 1 } } }) .ToList() }; return model; }
public UpdateMenuViewModel UpdateMenu(UpdateMenuViewModel model) { using (var tr = _context.Database.BeginTransaction()) { try { var lunchDate = model.LunchDate.ParseDate(); var name = string.Format(LocalizationStrings.MenuDefaultName, lunchDate.ToString(LocalizationStrings.RusDateFormat)); var menu = _context.Menus.FirstOrDefault(l => l.MenuId == model.MenuId); if (menu == null) { menu = new Menu { LunchDate = lunchDate, CreationDate = DateTime.Now, Price = model.Price, Active = true, Editable = true, Name = name }; _context.Menus.Add(menu); } else { if (!menu.Editable) { throw new Exception(LocalizationStrings.MenuIsLocked); } menu.LunchDate = lunchDate; menu.Price = model.Price; menu.Editable = true; menu.Name = name; } _context.SaveChanges(); model.MenuId = menu.MenuId; model = UpdateMenuSections(model); tr.Commit(); return model; } catch (Exception e) { tr.Rollback(); throw new Exception(e.Message, e.InnerException); } } }
public IActionResult Update([FromBody] UpdateMenuViewModel model) { try { var results = _validator.Validate(model); if (!results.IsValid) { throw new Exception(results.Errors[0].ErrorMessage); } var savedModel = _menuService.UpdateMenu(model); return(Ok(savedModel)); } catch (Exception e) { return(BadRequest(e)); } }
public UpdateMenuViewModel GetEmptyMenu() { var model = new UpdateMenuViewModel { LunchDate = DateTime.Now.NextFriday().ToString(DateFormat), Editable = true, Sections = _context.MenuSections.Select(s => new MenuSectionViewModel() { MenuSectionId = s.MenuSectionId, Name = s.Name, Number = s.Number, Items = new List <MenuItemViewModel>(), }).ToList() }; return(model); }
public UpdateMenuViewModel Parse(string fileName) { var document = new Document(fileName); var section = document.Sections[0]; var lines = new List <DocLine>(); foreach (Paragraph par in section.Paragraphs) { if (string.IsNullOrWhiteSpace(par.Text)) { continue; } var l = new DocLine() { Text = par.Text.Trim(), Number = lines.Count }; lines.Add(l); } var titleItem = lines.FirstOrDefault(IsTitle); var title = titleItem == null ? string.Empty : titleItem.Text; var date = titleItem == null ? DateTime.Now : title.ExtractDateFromTitle(); var priceItem = lines.FirstOrDefault(IsPrice); var price = priceItem == null ? 0 : priceItem.Text.ExtractPriceFromString(); lines.Remove(titleItem); lines.Remove(priceItem); lines.Remove(lines.FirstOrDefault(IsMenu)); List <MenuSectionViewModel> m = null; ExtractMenu(lines, ref m); var model = new UpdateMenuViewModel() { LunchDate = (date ?? DateTime.Now).ToString("YYYY-MM-DD"), Price = price, // Title = title, Sections = m, //FilePath = fileName }; return(model); }
public IActionResult Edit(UpdateMenuViewModel model) { if (ModelState.IsValid) { try { _mnuService.Update(model.Key, model.Name, model.Url, model.Visible, model.ParentKey); return(Content(Url.Action(nameof(Index)))); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } } else { return(BadRequest(GetModelError())); } }
public UpdateMenuViewModel UpdateMenu(UpdateMenuViewModel model) { Menu menu; if (model == null) { return(null); } if (model.MenuId == 0) { menu = new Menu { LunchDate = DateTime.Parse(model.LunchDate), CreationDate = DateTime.Now, Price = model.Price, Active = true, Editable = true }; _context.Menus.Add(menu); } else { menu = _context.Menus.FirstOrDefault(l => l.MenuId == model.MenuId); if (menu == null) { throw new Exception("Menu not found menuId = " + model.MenuId); } ; menu.LunchDate = !string.IsNullOrWhiteSpace(model.LunchDate) ? DateTime.Parse(model.LunchDate) : DateTime.Now.NextFriday(); menu.Price = model.Price; menu.Editable = true; } _context.SaveChanges(); model.MenuId = menu.MenuId; model = UpdateMenuSections(model); return(model); }
protected UpdateMenuViewModel GetFakeMenu(int menuId) { var sec = TestContext.MenuSections.ToList(); var model = new UpdateMenuViewModel { Editable = true, Price = 10000, MenuId = menuId, Sections = new List <MenuSectionViewModel>(), LunchDate = DateTime.Now.AddYears(1100).ToString(DateFormat) }; foreach (var dbSection in sec) { var section = new MenuSectionViewModel() { Number = dbSection.Number, Name = dbSection.Name, MenuSectionId = dbSection.MenuSectionId, MenuId = menuId, Items = new List <MenuItemViewModel>() }; for (var i = 0; i < 100; i++) { var item = new MenuItemViewModel() { MenuId = menuId, Number = i, Name = Guid.NewGuid().ToString(), MenuSectionId = section.MenuSectionId, }; section.Items.Add(item); } model.Sections.Add(section); } return(model); }