Beispiel #1
0
        public async Task <IActionResult> Post([FromBody] TripViewModel tripModel)
        {
            if (ModelState.IsValid)
            {
                // save to the database
                var newTrip = Mapper.Map <Trip>(tripModel);
                newTrip.UserName = User.Identity.Name;
                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{tripModel.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }

            return(BadRequest("Failed to save the trip"));
        }
Beispiel #2
0
        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.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, User.Identity.Name, 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"));
        }