public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            var city = CitiDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

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

            var pointOfInterestFromStore = city.PointsOfInterests.FirstOrDefault(p => p.Id == id);

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

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            var city = CitiDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

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

            var maxPointPfInterestId = CitiDataStore.Current.Cities.SelectMany(c => c.PointsOfInterests).Max(p => p.Id);
            var finalPointOfInterest = new PointsOfInterestDto()
            {
                Id          = maxPointPfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterests.Add(finalPointOfInterest);

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

            var city = GetCity(cityId);

            if (city == null)
            {
                logInformation(cityId, "city");
                return(NotFound());
            }

            //Demo |  just used with in memory datastore to get the higher id from the point of interest list
            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c =>
                                                                                 c.PointsOfInterest).Max(p => p.Id);

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

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId, finalPointOfInterest.Id },
                                  finalPointOfInterest));
        }
Ejemplo n.º 4
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreateDto createDto)
        {
            if (createDto == null)
            {
                return(BadRequest());
            }

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

            var city = CityDataStore.Instance.Cities.FirstOrDefault(cit => cit.Id == cityId);

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


            var newId = CityDataStore.Instance.Cities
                        .SelectMany(cit => cit.PointsOfInterest)
                        .OrderBy(cit => cit.Id).Last().Id++;

            var newDto = new PointOfInterestDto()
            {
                Id          = newId,
                Description = createDto.Description,
                Name        = createDto.Name
            };

            city.PointsOfInterest.Add(newDto);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, poiId = newId }, newDto));
        }
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForCreateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }
            var city = CitiDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

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

            var pointOfInterestFromStore = city.PointsOfInterests.FirstOrDefault(p => p.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }
            var pointOfInterestToPatch = new PointOfInterestForCreateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }