[HttpPost]                                                                                           //Bad Request will automoatically send a 400 bad request. If this is not here, we would need to add some logic.
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointofInterestForCreationDto pointOfInterest) //Complex Request Type
        {
            //API Controller does not require the code below checking the modelstate.

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

            //Need to check the model state is good.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));//Pass the model state to bad request to ensure it is deserialized.
            }

            //check to see if a city exists before attetmping to add a point of interest to an unexisting city.
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            //=================================== Old Mapping Code=====================================================
            //Data Store code below replaced by repository code.
            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if(city == null)
            //{
            //    return NotFound();
            //}
            //Grab the max ID.
            //var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
            //    c => c.PointsOfInterest).Max(p => p.Id);
            ////Gearing up to add the information into the database or in memory data store.
            //var finalPointofInterest = new PointOfInterestDto()
            //{
            //    Id = ++maxPointOfInterestId,
            //    Name = pointOfInterest.Name,
            //    Description = pointOfInterest.Description,
            //};
            //==================================OLD MAPPING CODE=========================================

            //Mapp the final points of Interest
            var finalPointofInterest = _mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointofInterest);
            _cityInfoRepository.Save();//If something goes wrong here a 500 server error will be returned. CityID and ID will be set here.

            //Mapping back to the models.
            var createPointOfInterestToReturn = _mapper
                                                .Map <Models.PointOfInterestDto>(finalPointofInterest);

            return(CreatedAtRoute(
                       "GetPointOfInterest",                                  //Called from above with the name of Get Point of Interest.
                       new { cityId, id = createPointOfInterestToReturn.Id }, //Passing in the Route Values. cityId is used as the variable name passed.
                       createPointOfInterestToReturn                          //Final point of Interest.
                       ));
        }
        public IActionResult CreatePointofInterest(int cityId, [FromBody] PointofInterestForCreationDto pointofInterestForCreationDto)
        {
            if (pointofInterestForCreationDto == null)
            {
                return(BadRequest());
            }

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

            if (pointofInterestForCreationDto.Name == pointofInterestForCreationDto.Description)
            {
                ModelState.AddModelError("Description", "Name and Description should not be the same dude");
                return(BadRequest(ModelState));
            }

            var city = _cityInfoRepository.GetCity(cityId, false);

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

            //var maxPointofInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsofInterest).Max(p => p.ID);

            var finalPointofInterest = Mapper.Map <PointofInterest>(pointofInterestForCreationDto);

            _cityInfoRepository.AddPointofInterest(cityId, finalPointofInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Not able to Add POI"));
            }

            var finalPointofInterestDTO = Mapper.Map <PointofInterestForCreationDto>(finalPointofInterest);

            return(CreatedAtRoute("GetPointofInterest", new { id = cityId, pid = finalPointofInterest.ID }, finalPointofInterestDTO));
        }
示例#3
0
        public IActionResult CreatePointOfInterest(int cityId, PointofInterestForCreationDto pointofInterestForCreation)
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

            var maxPointOfInterestId = city.PointsOfInterests.Max(p => p.Id);

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

            city.PointsOfInterests.Add(finalPointOfInterest);

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