Example #1
0
        public IActionResult Post(DishModel model)
        {
            try
            {
                _dishService.AddDish(ConvertDishModelToDishDTO(model));

                string currentEmail = this.User.FindFirst(ClaimTypes.Name).Value;
                string userId       = _userHelper.GetUserId(currentEmail);

                if (userId == null)
                {
                    return(NotFound("User not found"));
                }

                _logger.LogInformation($"[{DateTime.Now.ToString()}]:[dish/put]:[info:create dish {model.Name}]:[user:{userId}]");

                return(Ok(model));
            }
            catch (ValidationException ex)
            {
                _logger.LogError($"[{DateTime.Now.ToString()}]:[dish/post]:[error:{ex.Property}, {ex.Message}]");

                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogError($"[{DateTime.Now.ToString()}]:[dish/post]:[error:{ex}]");

                return(BadRequest());
            }
        }
Example #2
0
        public async Task <JsonResult> CreateDish(IFormCollection form)
        {
            try
            {
                var dish           = JsonConvert.DeserializeObject <Dish>(form["dish"]);
                var dishExtraTypes = JsonConvert.DeserializeObject <List <DishExtraType> >(form["dishExtraTypes"]);

                dish.DishExtraTypes = dishExtraTypes;

                await _service.AddDish(dish);

                return(Json("Success"));
            }
            catch (Exception ex)
            {
                return(Json("Failed"));
            }
        }
        public async Task <IActionResult> Add(IFormFile uploadedFile, [FromForm] AddDishViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DishDTO dishDTO = null;
                    string  path    = null;

                    // сохранение картинки
                    if (uploadedFile != null)
                    {
                        path = uploadedFile.FileName;
                        // сохраняем файл в папку files/provider/ в каталоге wwwroot
                        using (var fileStream = new FileStream(_appEnvironment.WebRootPath + _path + path, FileMode.Create))
                        {
                            await uploadedFile.CopyToAsync(fileStream);

                            _logger.LogInformation($"{DateTime.Now.ToString()}: Save image {path} in {_path}");
                        }
                    }

                    dishDTO = new DishDTO
                    {
                        Info      = model.Info,
                        CatalogId = model.CatalogId,
                        Name      = model.Name,
                        Path      = path,
                        Price     = model.Price,
                        Weight    = model.Weight
                    };

                    _dishService.AddDish(dishDTO);

                    string currentUserId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                    _logger.LogInformation($"{DateTime.Now.ToString()}: User {currentUserId} added new dish");

                    return(RedirectToAction("Index", new { dishDTO.CatalogId }));
                }
                catch (ValidationException ex)
                {
                    ModelState.AddModelError(ex.Property, ex.Message);
                    _logger.LogError($"{DateTime.Now.ToString()}: {ex.Property}, {ex.Message}");
                }
            }
            return(View(model));
        }
        public IActionResult PostDish([FromBody] DishPlDto dish)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(400, "Model is not valid"));
            }

            try
            {
                dish.DishId = Guid.NewGuid();
                var newDish = mapper.Map <BlDto_Dish>(dish);
                dishService.AddDish(newDish);

                return(StatusCode(201, "Dish was added"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error. Dish is not added. Exception message: " + ex));
            }



            //  return CreatedAtAction("GetDish", new { id = dish.DishId }, dish);
        }
 public void Insert(DishDto dish)
 {
     _platService.AddDish(dish);
 }