public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm) { try { var newStop = Mapper.Map <Stop>(vm); //Look up geocodes var result = await _coordsService.GeoCoordsAsync(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, User.Identity.Name); if (await _repository.SaveChangesAsync()) { return(Created($"/api/trips/{tripName}/stops/{newStop.Name})", Mapper.Map <StopViewModel>(newStop))); } } } catch (Exception ex) { _logger.LogError("Failed to get Stops:{0}", ex); } return(BadRequest("Failed to get Stops")); }
public async Task <IActionResult> Post(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.GeoCoordsAsync(newStop.Name); if (!result.Success) { _logger.LogError(result.Message); } //Save to the DB else { newStop.Latitude = result.Latitude; newStop.Longitude = result.Longitude; _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("Failed to save new Stop:{0}", ex); } return(BadRequest("Failed to save new stop")); }