public MenuItem Save(Guid id, MenuItem foodItem) { if (String.IsNullOrEmpty(foodItem.FoodName)) { throw new FoodNameRequiredException("Food name is required for Menu."); } if (String.IsNullOrEmpty(foodItem.Ingridients)) { throw new IngredientsRequiredException("Ingredients are required for the Menu."); } if (foodItem.Price <= 0) { throw new PriceRequiredException("Price is required."); } MenuItem result = null; var foundMenuitem = menuItemRepository.Retrieve(id); if (foundMenuitem == null) { result = menuItemRepository.Create(foodItem); } else { result = menuItemRepository.Update(id, foodItem); } return(result); }
public void Create(MenuItem newMenuItem) { newMenuItem.DateCreated = DateTime.Now; string the_name = newMenuItem.Name; string slug_created = GenerateSlug(the_name); var checkIfSlugExists = _menuItemRepository.GetBySlug(slug_created); if (checkIfSlugExists == null) { newMenuItem.Slug = slug_created; } else { Guid guid = Guid.NewGuid(); newMenuItem.Slug = slug_created + "-" + guid; } newMenuItem.Currency = "USD"; _menuItemRepository.Create(newMenuItem); }
public int Create(MenuItem menuItem) { return(_repo.Create(menuItem)); }
public async Task <IActionResult> Create(CreateMenuItemVM model) { try { var categories = await _catRepo.FindAll(); var categoryItems = categories.Select(q => new SelectListItem { Text = q.Name, Value = q.Id.ToString() }); model.Category = categoryItems; if (!ModelState.IsValid) { return(View(model)); } string uniqueFileName = null; if (model.Image != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName; string filepath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filepath, FileMode.Create)) { model.Image.CopyTo(fileStream); } } /*var files = HttpContext.Request.Form.Files; * if (files.Count > 0) * { * byte[] p1 = null; * using(var fs1 = files[0].OpenReadStream()) * { * using(var ms1=new MemoryStream()) * { * fs1.CopyTo(ms1); * p1 = ms1.ToArray(); * } * } * model.Picture = p1; * }*/ var newMenuItem = new MenuItemVM { Name = model.Name, Description = model.Description, Spicyness = model.Spicyness, Image = uniqueFileName, CategoryId = model.CategoryId, Price = model.Price }; var obj = _mapper.Map <MenuItem>(newMenuItem); var success = await _repo.Create(obj); if (!success) { ModelState.AddModelError("", "Something went wrong with request"); return(View(model)); } return(RedirectToAction(nameof(Index))); } catch { ModelState.AddModelError("", "Something went wrong with request"); return(View(model)); } }