예제 #1
0
 /// <summary>
 /// Adds new user model to DB
 /// </summary>
 /// <param name="dish">Dish model to add</param>
 /// <returns>Id of new dish</returns>
 public int AddDish(DishModel dish)
 {
     try
     {
         return(_dishRepository.AddDish(dish));
     }
     catch (Exception ex)
     {
         throw new Exception("Error while adding dish: " + ex.Message, ex);
     }
 }
        public ActionResult Create(DishFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Heading      = "Create a new dish";
                viewModel.CategoryList = new SelectList(categoryRepository.Categories, "ID", "Name");
                viewModel.ImageLists   = new List <SelectList>();
                for (int i = 0; i < Constant.Constant.ImagesPerDish; i++)
                {
                    viewModel.ImageLists.Add(new SelectList(dishImageRepository.DishImages, "ID", "FileName"));
                }
                return(View("DishForm", viewModel));
            }

            var dish = new Dish
            {
                Name          = viewModel.Name,
                Description   = viewModel.Description,
                StandardPrice = viewModel.StandardPrice,
                CategoryID    = viewModel.CategoryID,
            };



            dishRepository.AddDish(dish);


            string[] dishImages = viewModel.DishImages.Where(pi =>
                                                             !string.IsNullOrEmpty(pi)).ToArray();
            List <DishImageMapping> mappings = new List <DishImageMapping>();

            for (int i = 0; i < dishImages.Length; i++)
            {
                DishImageMapping mapping = new DishImageMapping()
                {
                    DishID      = dish.DishID,
                    ImageNumber = i,
                    DishImageID = int.Parse(dishImages[i])
                };
                mappingRepository.Add(mapping);
                mappingRepository.Save();
            }


            TempData["message"] = $"{viewModel.Name} has been saved.";
            return(RedirectToAction("Index", "Admin"));
        }
예제 #3
0
 public async Task <Dish> Add(Dish dish)
 {
     return(await _dishRepo.AddDish(dish));
 }