public async Task <IActionResult> Create(Dish dishModel) { if (!ModelState.IsValid) { return(View(dishModel)); } 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(); } } dishModel.Image = p1; } if (!(TempData["RestaurantId"] is null)) { await _dishService.Create(dishModel); int restaurantId = Convert.ToInt32(TempData["RestaurantId"].ToString()); await _restaurantService.AddDish(restaurantId, dishModel); } return(RedirectToAction(nameof(Index))); }
public string Create([FromBody] CreateDishDto dto) { if (ModelState.IsValid) { if (!_dishService.GetAll().Any(x => x.NameOfDish == dto.NameOfDish)) { int id = _dishService.Create(dto); return(JsonConvert.SerializeObject("Dish added successfully!")); } } return(JsonConvert.SerializeObject("Something went wrong!")); }
public IActionResult Add(DishCreationViewModel model, IEnumerable <IFormFile> images) { if (ModelState.IsValid) { if (images.Any()) { var imageLinks = new List <string>(); foreach (var formFile in images) { var uploadParams = new ImageUploadParams() { File = new FileDescription(formFile.FileName, formFile.OpenReadStream()) }; var uploadResult = _cloudinary.Upload(uploadParams); var url = uploadResult.Uri.ToString(); imageLinks.Add(url); } model.ImageUrls = imageLinks; } var dish = _mapper.Map <Dish>(model); _dishService.Create(dish); var imageList = model.ImageUrls.Select(source => new Image { DishId = dish.DishId, Url = source }).ToList(); imageList.ForEach(source => _imageService.Create(source)); return(RedirectToAction("Index")); } var categories = _dishService.GetCategories(); var allergies = _dishService.GetAllergies(); var dishCreationViewModel = new DishCreationViewModel { AllCategories = categories, AllAllergies = allergies }; return(View(dishCreationViewModel)); }
public ActionResult Post([FromRoute] int restaurantId, [FromBody] CreateDishDto dto) { var newDishId = _dishService.Create(restaurantId, dto); return(Created($"api/restaurant/{restaurantId}/dish/{newDishId}", null)); }
public RedirectResult Add(DishDTO dish) { _dishService.Create(dish); return(Redirect("/Dish/GetAll")); }
protected override void ExecuteCommand() { dishService.Create(Model); }