Beispiel #1
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInteresForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pointOfInterest.name == pointOfInterest.description)
            {
                ModelState.AddModelError("Description", "The provide values should not be equal.");
            }

            if (!_infoRepository.CityExist(cityId))
            {
                return(NotFound("City was not found"));
            }

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

            _infoRepository.AddPointOfInterest(cityId, finalPointOfInterest);

            if (!_infoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while saving your point of interest."));
            }

            PointOfInterest createdPointOfInterestToReturn = Mapper.Map <PointOfInterest>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, point = createdPointOfInterestToReturn.id }, createdPointOfInterestToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInteresForCreationDto pointOfinterest)
        {
            if (pointOfinterest.Description == pointOfinterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided descruption should be different from the name. "
                    );
            }

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

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


            var finalPointOfInterest = _mapper.Map <Data.Entities.PointOfInterest>(pointOfinterest);

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


            var createdPointOfINterestToReturn = _mapper
                                                 .Map <Data.Models.PointOfInterestDto>(finalPointOfInterest);

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

            if (pointOfInterest.Description == pointOfInterest.Name)                                             // adding our custom validation to ModelState
            {
                ModelState.AddModelError("Description", "The provided description should differ from the name"); // the key "Description" could be a property name but it doesn`t have to be
            }

            if (!ModelState.IsValid)            //checking our rules in data validations
            {
                return(BadRequest(ModelState)); // we could return just BadRequest(), in this case no custom error message appear
            }

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

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

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

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

            city.PointsOfInterest.Add(finalPointOfInterest);

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