public async Task <IActionResult> Create([FromBody] TripViewModel vm) { if (ModelState.IsValid) { var newTrip = Mapper.Map <Trip>(vm); _repository.AddTrip(newTrip); if (await _repository.SaveChangesAsync()) { return(Created($"api/trips/{newTrip.Name}", Mapper.Map <TripViewModel>(newTrip))); } return(BadRequest("Failed to write data to the database")); } return(BadRequest(ModelState)); }
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")); }