コード例 #1
0
        public async Task <IActionResult> AddMenu(AddMenuViewModel menuViewModel)
        {
            var user = await _userManager.FindByIdAsync(HttpContext.User.GetUserId());

            Restaurant restaurant = _applicationService.GetRestaurantByUserId(user.Id);

            if (ModelState.IsValid)
            {
                MenuItem item = new MenuItem()
                {
                    Name  = menuViewModel.Name,
                    Price = menuViewModel.Price
                };

                MenuItemType menuItemType = new MenuItemType();

                if (menuViewModel.SelectedMenuItemType == "-1" && !string.IsNullOrEmpty(menuViewModel.NewTypeName))
                {
                    //Pridedame nauja MenuItemType
                    menuItemType.Restaurant = restaurant;
                    menuItemType.TypeName   = menuViewModel.NewTypeName;
                    bool success = _applicationService.AddMenuItemType(menuItemType);

                    if (!success)
                    {
                        //redirectina i pradzia, nepavyko prideti
                        TempData["Unsuccess"] = menuItemType.TypeName + " tipas jau egzistuoja!";
                        return(RedirectToAction("ViewMenu"));
                    }
                    TempData["Success"] = "Sėkmingai įdėjote " + menuViewModel.Name + " patiekalą!";
                }
                else
                {
                    menuItemType = _applicationService.GetMenuItemTypeByRestaurantIdTypeName(
                        restaurant.Id, menuViewModel.SelectedMenuItemType);

                    if (menuItemType == null)
                    {
                        //redirectina i pradzia, nepavyko prideti
                        TempData["Unsuccess"] = "Nepavyko pridėti patiekalo!";
                        return(RedirectToAction("ViewMenu"));
                    }
                }

                item.Type = menuItemType;
                _applicationService.AddMenuItemByRestaurantId(item, restaurant.Id);
                TempData["Success"] = "Sėkmingai įdėjote " + menuViewModel.Name + " patiekalą!";
            }
            else
            {
                TempData["Unsuccess"] = "Nepavyko pridėti patiekalo!";
            }

            return(RedirectToAction("ViewMenu"));
        }