Example #1
0
        public IActionResult CreatePointOfIntrest(int cityId,
                                                  [FromBody] PointOfInterestDtoForCreation pointOfIntrest)
        {
            if (pointOfIntrest == null)
            {
                return(BadRequest());
            }

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

            var poi = Mapper.Map <PointOfInterest>(pointOfIntrest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, poi);

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

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

            return(CreatedAtRoute("GetPointOfIntrest", new { cityId = cityId, poiId = createdPointOfInterestToReturn.Id },
                                  createdPointOfInterestToReturn));
        }
Example #2
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestDtoForCreation 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));
            }

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

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterst);

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

            var createPointOfInterstToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterst);

            return(CreatedAtRoute("GetPointInterst", new { cityId = cityId, id = createPointOfInterstToReturn.Id }, createPointOfInterstToReturn));
        }
Example #3
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestDtoForCreation pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound($"city with id: {cityId}"));
            }

            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));
        }