public JsonResult Post([FromBody] TripViewModel vm) { try { if (ModelState.IsValid) { var newTrip = Mapper.Map <Trip>(vm); _logger.LogInformation("Attempting to save a new trip"); _repository.AddTrip(newTrip); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <TripViewModel>(newTrip))); } } } catch (Exception ex) { _logger.LogError("Failed to save new trip", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = ex.Message })); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = "Failed", ModelState = ModelState })); }
public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm) { try { if (ModelState.IsValid) { //Map to the Entity var newStop = Mapper.Map <Stop>(vm); //Looking up Geocoordinates var coordResult = await _coordService.Lookup(newStop.Name); if (!coordResult.Success) { Response.StatusCode = (int)HttpStatusCode.BadRequest; Json(coordResult.Message); } newStop.Latitude = coordResult.Latitude; newStop.Longitude = coordResult.Longitude; //Save to the db _repository.AddStop(tripName, newStop); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <StopViewModel>(newStop))); } } } catch (Exception ex) { _logger.LogError($"Failed to save new stop", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Failed to save new stop")); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Validation Failed on new stop")); }