Пример #1
0
        public async Task <IActionResult> Create(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // If the VM is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    // Lookup the Geocodes
                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;

                        // Save to the Database
                        _repository.AddStop(tripName, newStop);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
                else
                {
                    return(BadRequest("Model state not valid"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new Stop: {0}", ex);
            }

            return(BadRequest("Failed to save new stop"));
        }