コード例 #1
0
        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)));
        }
コード例 #2
0
        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!"));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        public ActionResult Post([FromRoute] int restaurantId, [FromBody] CreateDishDto dto)
        {
            var newDishId = _dishService.Create(restaurantId, dto);

            return(Created($"api/restaurant/{restaurantId}/dish/{newDishId}", null));
        }
コード例 #5
0
 public RedirectResult Add(DishDTO dish)
 {
     _dishService.Create(dish);
     return(Redirect("/Dish/GetAll"));
 }
コード例 #6
0
 protected override void ExecuteCommand()
 {
     dishService.Create(Model);
 }