Example #1
0
        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 database.
                    _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"));
        }
Example #2
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(viewModel);

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

                    if (!result.Successful)
                    {
                        _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)));
                    }
                    else
                    {
                        return(BadRequest("Unable to save to database"));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new Stop: {0}", ex);
                return(BadRequest("Failed to post to database."));
            }
        }
Example #3
0
        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;
                        return(Json(new { Message = coordResult.Message, CreatedStop = new {} }));
                    }

                    newStop.Longitude = coordResult.Longitude;
                    newStop.Latitude  = coordResult.Latitude;

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

                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(new { Message = "Success", CreatedStop = Mapper.Map <StopViewModel>(newStop) }));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new stop for trip \"{tripName}\"", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = "Failed", CreatedStop = new {}, Exception = ex }));
            }

            _logger.LogError("Validation failed on new stop.");
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Validation failed on new stop.", CreatedStop = new {}, ModelState = ModelState }));
        }
Example #4
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"));
        }
Example #5
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // map
                    Stop newStop = Mapper.Map <Stop>(vm);

                    // lookup the geocodes
                    GeoCoordsResult coordsResult = await _coordsService.GetCoordsAsync(newStop.Name);

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

                        // save to db
                        _repository.AddStop(tripName, newStop, User.Identity.Name);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save Stop: {0}", ex);
            }

            return(BadRequest("Failed to save Stop"));
        }
Example #6
0
        public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    // look up Geo Coordinates
                    var result = await _coordService.Lookup(newStop.Name);

                    if (!result.Success)
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        return(Json(result.Message));
                    }

                    newStop.Longitude = result.Longitude;
                    newStop.Latitude  = result.Latitude;


                    // Save to DB
                    _logger.LogInformation("Attempting to Save New Stop to DB");
                    _repository.AddStop(tripName, newStop, User.Identity.Name);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <StopViewModel>(newStop)));
                    }
                }

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = "failed", ModelState = ModelState }));
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to Save new Stop", e);

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { ExceptionMsg = e.Message }));
            }
        }
        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"));
        }
Example #8
0
        public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            dynamic model = null;

            try
            {
                if (ModelState.IsValid)
                {
                    //Map viewmodel to 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;
                        return(Json(coordResult.Message));
                    }

                    newStop.Longitude = coordResult.Longitude;
                    newStop.Latitude  = coordResult.Latitude;
                    model             = newStop;
                    //Save to the database
                    _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(new { Message = "failed to save new stop", Exception = ex.Message, cEx = ex, model = model, bingKey = Startup.Configuration["AppSettings:BingKey"] }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Validation failed on new stop", ModelState = ModelState }));
        }
        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.GeoCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    //Save to the DB
                    else
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;


                        _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"));
        }
Example #10
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // If VM is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    // Lookup the GEO codes
                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

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

                        // Save to the database
                        //_repository.AddStop(tripName, newStop);
                        _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 saving new stop: {ex}");
            }

            return(BadRequest("Error saving new stop!"));
        }
        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. GeoLocation Services
                    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/{tripName}/stops/{newStop.Name}", //Created is the result of a Post when successfully save a new object. 201 Status code.
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new Stop: {0}", ex);
            }

            return(BadRequest("Failed to save new stop"));
        }
Example #12
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stop)
        {
            try
            {
                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 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"));
        }
Example #13
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!"));
        }
Example #14
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."));
        }
Example #15
0
        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"));
        }
Example #16
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Stop newStop = Mapper.Map <Stop>(vm);

                    _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($"Fail to add a new Stop {ex}");
            }

            return(BadRequest("Fail to add a new Stop"));
        }
Example #17
0
        public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    var coordResult = await _coordService.Lookup(vm.Name);

                    if (!coordResult.Success)
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        return(Json($"Error adding stop with name {vm.Name}, {coordResult.Message}"));
                    }

                    newStop.Longitude = coordResult.Longitude;
                    newStop.Latitude  = coordResult.Latitude;

                    _worldRepository.AddStop(tripName, newStop);

                    if (_worldRepository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <StopViewModel>(newStop)));
                    }
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json($"Error adding stop with name {vm.Name}"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Example #18
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..."));
        }
Example #19
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(model);

                    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, 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 stop: {ex}");
                return(BadRequest(ex.ToString()));
            }

            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("Error al guardar un nuevo Stop: {0}", ex);
            }

            return(BadRequest("Error al querer guardar un nuevo stop"));
        }
Example #21
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel stopVM)
        {
            try
            {
                // Se a VM é válida
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(stopVM);

                    // Lookup as geolocalizações
                    var result = await _coordService.GetCoordAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Longitude = result.Longitude;
                        newStop.Latitude  = result.Latitude;
                        // salvar
                        _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("Faild...", ex);
            }
            return(BadRequest("bad request"));
        }