Exemple #1
0
        public IActionResult CreatePointsOfInterest(int cityId, [FromBody] PointsOfInterestForCreationDto pointsOfInterest)
        {
            if (pointsOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointsOfInterest.Description == pointsOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provide description should be different from the 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 PointsOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointsOfInterest.Name,
                Description = pointsOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

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

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

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

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

            int pointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
                c => c.PointsOfInterest).Max(p => p.Id);

            PointsOfInterestDto newPointOfInterest = new PointsOfInterestDto()
            {
                Id          = pointOfInterestId + 1,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, POIId = newPointOfInterest.Id },
                                  newPointOfInterest));
        }
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointsOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest(ModelState));
            }

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

            if (city == null)
            {
                ModelState.AddModelError("", "City Id not found");
                return(BadRequest(ModelState));
            }


            var currentPointOfInterest = CitiesDataStore.Current.Cities.SelectMany(c => c.PointOfIntererest).FirstOrDefault(p => p.Id == id);

            if (currentPointOfInterest == null)
            {
                ModelState.AddModelError("", "Point Id not found");
                return(BadRequest(ModelState));
            }


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

            //update
            currentPointOfInterest.Name        = pointOfInterest.Name;
            currentPointOfInterest.Description = pointOfInterest.Description;

            return(NoContent());
        }