public async Task <JsonResult> Post(TripViewModel trip, StopViewModel stop) { var aTrip = Mapper.Map <Trip>(trip); var aStop = Mapper.Map <Stop>(stop); var longlat = await cs.Lookup(aStop.Name); if (longlat.Success) { aStop.Longitude = longlat.Longitude; aStop.Latitude = longlat.Latitude; } aTrip.Stops.Add(aStop); if (ModelState.IsValid) { db.Trips.Update(aTrip); db.SaveChanges(); } var result = Mapper.Map <TripViewModel>(aTrip); return(Json(result)); }
public async Task <JsonResult> Post([FromBody] TripViewModel stop) { var newStop = Mapper.Map <Stop>(stop); var longlat = await coordService.Lookup(newStop.Name); if (longlat.Success) { db.AddStop(newStop); } return(Json(newStop)); }
public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel model) { try { if (ModelState.IsValid) { var newStop = Mapper.Map <Stop>(model); var coordinateResult = await coordinateService.Lookup(newStop.Name); if (!coordinateResult.Success) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(coordinateResult.Message)); } newStop.Longitude = coordinateResult.Longitude; newStop.Latitude = coordinateResult.Latitude; repository.AddStop(tripName, User.Identity.Name, 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")); }
public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm) { try { if (ModelState.IsValid) { var newStop = Mapper.Map <Stop>(vm); var coordinateResult = await _coordinateService.Lookup(newStop.Name); if (!coordinateResult.Success) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Json($"Couldn't locate the city, Message: {coordinateResult.Message}")); } newStop.Longitude = coordinateResult.Longitude; newStop.Latitude = coordinateResult.Latitude; _repository.AddStop(newStop, User.Identity.Name, tripName); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <StopViewModel>(newStop))); } return(Json(null)); } } catch (Exception ex) { _logger.LogError("Failed to save the new stop", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Failboat")); } return(Json(null)); }