public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stopVm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = _mapper.Map <Stop>(stopVm);

                    var coors = await _geoCoordsService.GetCoordsAsync(newStop.Name);

                    if (!coors.Success)
                    {
                        _logger.LogError(coors.Message);
                    }
                    else
                    {
                        newStop.Latitude  = coors.Latitude;
                        newStop.Longitude = coors.Longitude;

                        _repository.AddStop(tripName, newStop);
                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                           _mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Failed to save new stop {e.Message}");
            }

            return(BadRequest("Failed to save new stop"));
        }
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);
                    var result  = await _coordsService.GetCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;
                        _repository.AddStop(tripName, newStop, User.Identity.Name);
                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"api/trips/{tripName}/stops/{newStop.Name}", Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to get stops: {0}", ex);
            }
            return(BadRequest("Failed to get stops"));
        }
Esempio n. 3
0
        public async Task <IActionResult> Post(string nameOfTrip, [FromBody] ViewModels.Stop viewStopModel)
        {
            try
            {
                //if the view model is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <models.Stop>(viewStopModel);
                    //lookup the Geocode here
                    var result = await _geoService.GetCoordsAsync("VNPT Bình Dươngsss");

                    //save stop to DB
                    _repository.AddNewStop(nameOfTrip, newStop);
                    if (await _repository.SaveChangesAsyn())
                    {
                        //return if everything will be ok
                        var updateViewModel = Mapper.Map <ViewModels.Stop>(newStop);
                        return(Created($"api/trips/{nameOfTrip}/stops/{newStop.Name}", updateViewModel));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"can not add stop ({viewStopModel.Longitude},{viewStopModel.Latitude}) information {ex.Message}");
            }
            return(BadRequest("bad data"));
        }
        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.Latitude = result.Longtude;
                        // Save to the database
                        _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"));
        }
Esempio n. 5
0
        public async Task <IActionResult> Post(string tripName, [FromBody] Stop newStop)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var result = await geoCoordsService.GetCoordsAsync(newStop.Name);

                    if (result.Success)
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;

                        repository.AddStop(tripName, newStop, User.Identity.Name);
                        if (await repository.SaveChangesAsync())
                        {
                            return(Created($"api/trips/{newStop.Id}", newStop));
                        }
                    }
                    else
                    {
                        return(BadRequest(result.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Ouch!{ex}");
            }

            return(BadRequest("Failed to save Stop"));
        }
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stopVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var stop = Mapper.Map <Stop>(stopVM);

                    var result = await _coordsService.GetCoordsAsync(stop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }

                    stop.Latitude  = result.Latitude;
                    stop.Longitude = result.Longitude;

                    _repository.AddStop(tripName, stop, User.Identity.Name);

                    if (await _repository.SaveChangesAsync())
                    {
                        return(Created($"/api/stips/{tripName}/stops/{stop.Name}", Mapper.Map <StopViewModel>(stop)));
                    }
                }

                return(BadRequest(ModelState));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save stop: {ex}");
                return(BadRequest("Failed to save stop"));
            }
        }
Esempio n. 7
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel theStop)
        {
            if (ModelState.IsValid)
            {
                var newStop = Mapper.Map <Stop>(theStop);

                // 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/{theStop.Name}", Mapper.Map <StopViewModel>(newStop)));
                    }
                }
            }

            return(BadRequest(ModelState /* "Failed to save changes to the database" */));
        }
Esempio n. 8
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // Check if the VM is valid
                if (ModelState.IsValid)
                {
                    // Create new Stop using the AutoMapper
                    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())
                    {
                        // Created is result of a post when you successfully save a new object
                        // Use the mapper to convert the newStop back into a StopViewModel
                        // ** Want to return the view model not the actual object **
                        return(Created($"/api/trips{tripName}/stops/{newStop.Name}", Mapper.Map <StopViewModel>(newStop)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to get stops: {0}", ex);
            }

            // If the first return is not reached then return a bad request
            return(BadRequest("Failed to get stops"));
        }
Esempio n. 9
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        // accept from the body of page a stopviewmodel
        {
            try
            {
                // If the VM is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    // Look up GeoCodes
                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

                    // Checking properties from service
                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else // set the obj in use to the results of the service
                    {
                        // now when go to save changes it will include lat and long
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;
                    }

                    // Save to the DB. Now include 3rd piece of data - username to make sure it is the right trip
                    _repository.AddStop(tripName, newStop, User.Identity.Name);  // Addstop gets added to repository. can use refactoring

                    if (await _repository.SaveChangesAsync())
                    {
                        // Created is result of a post when you successfully created new obj
                        return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                       Mapper.Map <StopViewModel>(newStop))); // mapper to convert back to a Stop VM
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save a new Stop: {0}", ex);
            }
            return(BadRequest("Failed to save new stop"));
        }
Esempio n. 10
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // If the VM is valid
                if (vm.Arrival > DateTime.Now)
                {
                    ModelState.AddModelError("", "This date didn't become!");
                }
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    //Lookup the Geocodes
                    var result = await _coordService.GetCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError($"Failed to get coords: {result.Message}");
                    }
                    else
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;

                        // Save to the Database
                        _repository.AddStop(tripName, newStop, User.Identity.Name);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}", Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error occured while POST: {ex}");
            }

            return(BadRequest($"Error occured while POST:"));
        }
Esempio n. 11
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(viewModel);

                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

                    //if (!result.Success)
                    //{
                    //    _logger.LogError(result.Message);
                    //}
                    //else
                    //{

                    // Using hard-coded values ... did not find it worthwhile to register for the Bing web service.
                    newStop.Latitude  = -1111;
                    newStop.Longitude = -2222;

                    _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."));
        }
Esempio n. 12
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stopvm)
        {
            try
            {
                //Check stop is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(stopvm);
                    //Lookup latlong
                    var geoCoords = await _coordsService.GetCoordsAsync(newStop.Name);

                    if (!geoCoords.Success)
                    {
                        _logger.LogError(geoCoords.Message);
                    }
                    else
                    {
                        newStop.Latitude  = geoCoords.Latitude;
                        newStop.Longitude = geoCoords.Longitude;
                    }

                    newStop.Latitude = geoCoords.Latitude;
                    //save changes to db
                    _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 the stops!! {ex}");
            }

            return(BadRequest("Failed to save new stop!"));
        }
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stop)
        {
            if (ModelState.IsValid)
            {
                var newStop = Mapper.Map <Stop>(stop);

                // 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 database
                _repository.AddStop(tripName, newStop);


                if (await _repository.SaveChangesAsync())
                {
                    // Returning the TripViewModel and not the entity itself (Trip) for security/encapsulation.
                    return(Created($"api/trips/{tripName}/stops/{newStop.Name}",
                                   Mapper.Map <StopViewModel>(newStop)));
                }
            }

            // DEBUGGING: Determine why ModelState is invalid:
            //var errors = ModelState.Values.SelectMany(v => v.Errors);

            // Returning just the ModelState is helpful for debugging in internal services.
            return(BadRequest("Failed to save the stop."));
        }
Esempio n. 14
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, newStop, User.Identity.Name);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{vm.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($">>StopsController: Error when saving to database: {ex}");
                return(BadRequest("An error occured when saving data..."));
            }
            return(BadRequest("Failed to save data..."));
        }
Esempio n. 15
0
        public async Task <IActionResult> Get(string tripsName, [FromBody] StopsViewModel stopsViewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Model Not Valid In post"));
                }

                var newStop = Mapper.Map <Stop>(stopsViewModel);

                var result = await _coordsService.GetCoordsAsync(newStop.Name);

                if (!result.Success)
                {
                    _logger.LogError(result.Message);
                }
                else
                {
                    newStop.Latitude  = result.Latitude;
                    newStop.Longitude = result.Longitude;

                    _repository.AddStops(tripsName, newStop);

                    if (await _repository.SaveChangesAsync())
                    {
                        return(Created($"api/trips/{tripsName}/stops/{newStop.Name}",
                                       Mapper.Map <StopsViewModel>(newStop)));
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to Save Stop:{0}", e);
            }
            return(BadRequest("Failed To save New Stop"));
        }