public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(NotFound());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.GetCity(cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = city.NumberOfPointsInterest + 1,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
Esempio n. 2
0
        public IActionResult CreatPointOfInterest(int cityId,
                                                  [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            //TODO: demo purpose - to be improved
            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
                c => c.PointsOfInterest).Max(p => p.Id);

            var finalPointOfInterest = new PointOfInterestDto
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, id = finalPointOfInterest.Id },
                                  finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description should be different than Name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPoint = _mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _repository.AddPointOfInterestForCity(cityId, finalPoint);

            _repository.Save();

            var createdPointOfInterest = _mapper.Map <Models.PointOfInterestDto>(finalPoint);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = createdPointOfInterest.Id }, createdPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterestForCreation)
        {
            if (pointOfInterestForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var existingCity = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

            if (existingCity == null)
            {
                return(NotFound());
            }

            var newId = CitiesDataStore.Current.Cities
                        .SelectMany(c => c.PointsOfInterest).Max(p => p.Id) + 1;


            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = newId,
                Name        = pointOfInterestForCreation.Name,
                Description = pointOfInterestForCreation.Description
            };

            existingCity.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = newId }, finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = _mapper.Map <PointOfInterest>(pointOfInterest);



            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            _cityInfoRepository.Save();

            var createdPointOfInterestToReturn = _mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId, id = createdPointOfInterestToReturn.Id },
                       createdPointOfInterestToReturn));
        }
Esempio n. 6
0
        public IActionResult CreatePointOfInterest(int cityId, PointOfInterestForCreationDto pointOfInterestDto)
        {
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            if (pointOfInterestDto.Name == pointOfInterestDto.Description)
            {
                ModelState.AddModelError("Description", "Description should not be same as Name");
            }

            //This check is not required due to ApiController Attribute, but since we have added custom validation in previous line, let's add it
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var pointOfInterestfinal = _mapper.Map <PointOfInterest>(pointOfInterestDto);

            _cityInfoRepository.AddPointOfInterest(cityId, pointOfInterestfinal);
            _cityInfoRepository.Save();

            var pointOfInterestResult = _mapper.Map <PointOfInterestDto>(pointOfInterestfinal);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = pointOfInterestResult.Id }, pointOfInterestResult));
        }
Esempio n. 7
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            try
            {
                if (pointOfInterest == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!_cityInfoRepository.CityExists(cityId))
                {
                    return(NotFound());
                }

                var pointOfInterestEntity = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

                _cityInfoRepository.AddPointOfInterestToCity(cityId, pointOfInterestEntity);
                _cityInfoRepository.Save();

                var pointOfInterestDto = Mapper.Map <Models.PointOfInterestDto>(pointOfInterestEntity);

                // after method .Save() is call, the pointOfInterestEntity will have its autogenerated id filled out.
                return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = pointOfInterestEntity.Id }, pointOfInterestDto));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(StatusCode(Convert.ToInt32(HttpStatusCode.InternalServerError)));
            }
        }
Esempio n. 8
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Should be different to name");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_repo.CityExists(cityId))
            {
                return(NotFound());
            }


            var poiEntities = _repo.GetPointOfInterestForCity(cityId, id);

            if (poiEntities == null)
            {
                return(NotFound());
            }

            Mapper.Map(pointOfInterest, poiEntities);

            _repo.Save();

            return(NoContent());
        }
Esempio n. 9
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Should be different to name");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_repo.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfinterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _repo.AddPointOfInterestForCity(cityId, finalPointOfinterest);

            if (!_repo.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfinterest.Id }, finalPointOfinterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = AutoMapper.Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestToCity(cityId, finalPointOfInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happend while processing your request!"));
            }

            var outputPointOfInterest = AutoMapper.Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = outputPointOfInterest.Id }, outputPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (pointOfInterest == null || city == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            // Needs to be improved - Demo purposes.
            var newPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest)
                                       .Max(pointsOfInterest => pointsOfInterest.Id);

            var newPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++newPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description,
            };

            city.PointsOfInterest.Add(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = newPointOfInterestId },
                                  newPointOfInterest));
        }
Esempio n. 12
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            //1.validate
            //if deserialization fails then return 404
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            //do custom check and set modelstate error
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("title", "The provided description should be different from the name.");
            }
            //check modelstate and return 404 with error message set - custom or data annotation
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //2.save
            //if City resource does not exist then return 404
            var pointOfInterestResult = _pointsOfInterestSvc.AddPointOfInterestForCity(cityId, pointOfInterest);

            if (pointOfInterestResult == null)
            {
                return(NotFound());
            }
            //3.create response
            //use this helper method returns 201 and location header with link to the newly resource
            //Also returns GetPointOfInterestLink and pass 2 params.
            //Also pass newly created object in the response body
            return(CreatedAtRoute("GetPointOfInterestLink",
                                  new { cityId = cityId, id = pointOfInterestResult.Id }, pointOfInterestResult));
        }
Esempio n. 13
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "La descripción debe ser distinta al nombre");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Ocurrió un error al procesar su solicitud"));
            }

            var createdPointOfInterest = Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterest.Id }, createdPointOfInterest));
        }
Esempio n. 14
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Name and Desc must be different");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Faild to add point of interest"));
            }
            var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
Esempio n. 15
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description needs to be different than name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id);

            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
        public async Task <IActionResult> CreatePointOfInterest(int cityId,
                                                                [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!await _cityInfoRepository.CityExistsAsync(cityId))
            {
                _logger.LogInformation($"city with id {cityId} not found");
                return(NotFound());
            }

            var newPointOfInterest = Mapper.Map <PointOfInterest>(pointOfInterest);
            await _cityInfoRepository.AddPointOfInterestForCityAsync(cityId, newPointOfInterest);

            if (!await _cityInfoRepository.SaveAsync())
            {
                return(StatusCode(500, $"Something went wrong while creating the point of interest"));
            }
            var newPointOfInterestDto = Mapper.Map <PointOfInterestDto>(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = newPointOfInterestDto.Id }, newPointOfInterestDto));
        }
Esempio n. 17
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }


            var city = this.context.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var finalPointOfInterest = new PointOfInterestDto()
            {
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest.ToEntity());

            this.context.SaveChanges();

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId, id = finalPointOfInterest.Id },
                       finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description.ToLowerInvariant() == pointOfInterest.Name.ToLowerInvariant())
            {
                ModelState.AddModelError("Description", "The Description should be different from the Name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_repository.CityExists(cityId))
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <Entitites.PointOfInterest>(pointOfInterest);

            _repository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_repository.Save())
            {
                return(StatusCode(500, "A problem occurred when creating Point Of Interest."));
            }

            var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
        public IActionResult UpdatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto PointOfInterest, int id)
        {
            if (PointOfInterest == null)
            {
                return(BadRequest());
            }
            if (PointOfInterest.Name == PointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Description and name cannot be same");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterestFromStore = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }
            Mapper.Map(PointOfInterest, pointOfInterestFromStore);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Something wrong at the server side"));
            }
            return(NoContent());
        }
Esempio n. 20
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Enter a name value different from that of the name");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }
            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Sorry,error ecountered while handling your request."));
            }
            var createPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createPointOfInterestToReturn.Id }, createPointOfInterestToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Name and Description cannot be the same.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }


            var finalPoi = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPoi);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem occurred handling your request."));
            }

            var createdPoi = Mapper.Map <PointOfInterestDto>(finalPoi);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = createdPoi.Id }, createdPoi));
        }
        public IActionResult PostPointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var maxpointOfInterestId = city.PointsOfInterest.Count();

            var pointOfInterestToAdd = new PointOfInterestDto()
            {
                Id          = ++maxpointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(pointOfInterestToAdd);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = maxpointOfInterestId }, pointOfInterestToAdd));
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterestForCreation)
        {
            if (pointOfInterestForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var existingCity = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

            if (existingCity == null)
            {
                return(NotFound());
            }

            var pointOfInterestDto = existingCity.PointsOfInterest.FirstOrDefault(x => x.Id == id);

            if (pointOfInterestDto is null)
            {
                return(NotFound());
            }

            pointOfInterestDto.Name        = pointOfInterestForCreation.Name;
            pointOfInterestDto.Description = pointOfInterestForCreation.Description;

            return(NoContent());
        }
        public IActionResult UpadtePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var pointOfInterestforStore = city.PointsOfInterest.FirstOrDefault(c => c.Id == cityId);

            if (pointOfInterestforStore == null)
            {
                return(NotFound());
            }

            pointOfInterestforStore.Name        = pointOfInterest.Name;
            pointOfInterestforStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(x => x.PointsOfInterest).Max(x => x.Id);
            var newPointOfInterest   = new PointOfInterestDto {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = newPointOfInterest.Id }, newPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            var pointOfInterestValidator      = new PointOfInterestForCreationValidator();
            ValidationResult validationResult = pointOfInterestValidator.Validate(pointOfInterest);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors.Select(vr => new { propertyName = vr.PropertyName, errorMessage = vr.ErrorMessage })));
            }

            var cityExist = _repository.CityExists(cityId);

            if (!cityExist)
            {
                _logger.LogInformation($"City with id {cityId} wasn't found when accessing points of interest.");
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _repository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            if (!_repository.Save())
            {
                return(StatusCode(500, "A problem happened while handling you request"));
            }

            var createdPointOfInteretToReturn = Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = createdPointOfInteretToReturn.Id }, createdPointOfInteretToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto model)
        {
            if (model.Name == model.Description)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name."
                    );
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!cityInfoRepository.CityExists(cityId))
            {
                return(NotFound($"No city matching id {cityId}."));
            }

            var pointOfInterest = mapper.Map <PointOfInterestForCreationDto, PointOfInterest>(model);

            cityInfoRepository.AddPointOfInterestForCity(cityId, pointOfInterest);
            cityInfoRepository.Save();

            var createdPointOfInterest = mapper.Map <PointOfInterestDto>(pointOfInterest);

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId, id = createdPointOfInterest.Id },
                       createdPointOfInterest
                       ));
        }
Esempio n. 28
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description needs to be different than name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happend while handeling your request."));
            }

            var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId = cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
Esempio n. 30
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = _dataStorage.Cities.Get(cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <PointOfInterest>(pointOfInterest);

            _dataStorage.PointsOfInterest.Create(finalPointOfInterest);
            city.PointsOfInterest.Add(finalPointOfInterest);
            _dataStorage.SaveChanges();

            var createdPointOfInterest = Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = city.Id, id = createdPointOfInterest.Id }, createdPointOfInterest));
        }