Esempio n. 1
0
        public async Task <ActionResult> Post(string tripName, [FromBody] StopViewModel stop)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(stop);

                    var result = await _coordsService.GetCoordAsync(newStop.Name);

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

                        _repository.AddStopTo(tripName, newStop, User.Identity.Name);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"api/trips/{tripName}/stops/{newStop.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                _logger.LogError($"Failed to save new stop: {ex}");
            }
            return(BadRequest($"Failed to post new stop."));
        }
Esempio n. 2
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stopVM)
        {
            try
            {
                // Se a VM é válida
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(stopVM);

                    // Lookup as geolocalizações
                    var result = await _coordService.GetCoordAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Longitude = result.Longitude;
                        newStop.Latitude  = result.Latitude;
                        // salvar
                        _repository.AddStop(tripName, newStop);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}", Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Faild...", ex);
            }
            return(BadRequest("bad request"));
        }