public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm) { try { if (ModelState.IsValid) { var newStop = Mapper.Map <Stop>(vm); var result = await _coordService.GetCoordsAsync(newStop.Name); if (!result.Success) { _logger.LogError(result.Message); } 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")); }
public async Task <IActionResult> Post([FromBody] TripViewModel trip) { if (ModelState.IsValid) { Trip newTrip = Mapper.Map <Trip>(trip); newTrip.UserName = this.User.Identity.Name; _repository.AddTrip(newTrip); if (await _repository.SaveChangesAsync()) { return(Created($"api/trips/{trip.Name}", Mapper.Map <TripViewModel>(newTrip))); } //else //{ // return BadRequest("Failed to save changes to database"); //} } return(BadRequest("Failed to save trip")); }