Example #1
0
        public ActionResult <PointOfInterestDto> CreatePointOfInterest(int cityid, [FromBody] PointOfInterestForCreatioDto pointOfInterest)
        {
            try
            {
                var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityid);
                if (city == null)
                {
                    return(NotFound());
                }
                var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointOfInterest).Max(p => p.Id);
                var finalPointOfInterest = new PointOfInterestDto()
                {
                    Id          = ++maxPointOfInterestId,
                    Name        = pointOfInterest.Name,
                    Description = pointOfInterest.Description
                };

                city.PointOfInterest.Add(finalPointOfInterest);

                return(CreatedAtRoute("GetPointOfInterest",
                                      new {
                    cityid, finalPointOfInterest.Id
                },
                                      finalPointOfInterest));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        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));
        }
        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));
        }
        //2nd parameter.. the request will contain data for pointofinterest which we have to convert to CreatePointOfInterest so we desiarilize it
        //Method to add a new PointsOfInterest to a city
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            //ModelState is a dictionary containing Both State of the Model and model Binding validations
            //this will alfo false when a model of property type is passed
            //modelstate check the parameters which come in our Action
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The Provided Description should not be same as the Name"
                    );
            }

            if (!ModelState.IsValid) //asure data is desirialize in the response body
            {
                return(BadRequest(ModelState));
            }

            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            //we have to calculate the ID of the New Point of interest.our datastore currently working on PointsOfInterestDTO not PointOfInterestForCreationDto
            //loop through all of the ID's of the PointOfInterests of all cities. find the highest id then create an ID for new PointOfInterest by adding 1 in that Highest ID.
            //highest Id come in maxPointOfInteresId
            var maxPointOfInteresId = CitiesDataStore.Current.Cities.SelectMany(
                c => c.PointsOfInterest).Max(p => p.Id);

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

            //insert new PointsOfInterest to the city
            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId = cityId, id = finalPointOfInterest.Id },
                       finalPointOfInterest));
        }
Example #5
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            //THIS BELOW SNIPPET IS NOT NEEDED THANKS TO [ApiController]
            //if( !ModelState.IsValid)
            //{
            //    return BadRequest();
            //}

            //FOR COMPLEX VALIDATIONS WE NEED TO CODE IT OURSELVES AS IS THE CASE BELOW
            //IF THE DESCRIPTION IS SAME AS THAT OF NAME WE WILL GENERATE AN ERROR
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name."
                    );
            }
            //THE REASON WE NEED TO SPECIFY THIS CODE SNIPPET THIS TIME IS BECAUSE
            //ITS TOO LATE FOR THE [ApiController] TO HANDLE IT AS THE MODEL BINDING IS ALREADY DONE
            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 IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDTO pointOfInterest /*Use to get content from body*/)
        {
            if (null == pointOfInterest)
            {
                return(BadRequest());
            }

            //Incase Name and Description are the same

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description" /*Can be a property name, but doesn't have to be*/,
                                         "The provided description should be different from Name.");
            }

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

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

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

            //demo purposes - 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));
        }
Example #7
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            // If consumer sends a bad request, send them a 400
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

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

            // If the consumer is trying to add to a non-existant city return 404
            if (city == null)
            {
                return(NotFound());
            }

            // The request needs to be mapped to the point of interest model and id calculated.
            // This way is problematic. There is an auto-id generator method.
            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));
        }