示例#1
0
        public async Task <IActionResult> Post([FromBody] PlaceViewModel placeViewModel)
        {
            if (ModelState.IsValid)
            {
                var newPlace = Mapper.Map <Place>(placeViewModel);
                if (await _placeService.Add(newPlace))
                {
                    return(Created($"api/places/{newPlace.Id}", Mapper.Map <PlaceViewModel>(newPlace)));
                }

                _logger.LogError("Failed to save place to database");
            }
            else
            {
                var errorMessage = string.Join(" | ", ModelState.Values
                                               .SelectMany(v => v.Errors)
                                               .Select(e => e.ErrorMessage));
                _logger.LogError($"Model not valid ({errorMessage})");
            }

            return(BadRequest("Failed to save place"));
        }