コード例 #1
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterestForUpdate)
        {
            if (pointOfInterestForUpdate == null)
            {
                return(BadRequest());
            }

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

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

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

            var poi = city.PointOfInterest.FirstOrDefault(p => p.Id == id);

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

            poi.Name        = pointOfInterestForUpdate.Name;
            poi.Description = pointOfInterestForUpdate.Description;

            return(NoContent());
        }
コード例 #2
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

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

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterest(cityId, id);

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

            _mapper.Map(pointOfInterest, pointOfInterestEntity);

            _cityInfoRepository.UpdatePointOfInterest(cityId, pointOfInterestEntity);

            _cityInfoRepository.Save();

            return(NoContent());
        }
コード例 #3
0
        public bool UpdatePointOfInterest(int cityId, int pointOfInterestId, PointOfInterestForUpdateDto pointOfInterestDto)
        {
            if (!_cityInfoRepository.CityExists(cityId))
            {
                _logger.LogError($"City does not exist: {cityId}");
                return(false);
            }

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, pointOfInterestId);

            if (pointOfInterestEntity == null)
            {
                _logger.LogError($"Point of Interest {pointOfInterestId} for city {cityId} does not exist");
                return(false);
            }

            //we do full update on PUT request (if some field is missed then it is populated with null!!!)
            //map dto to entity use mapping: <PointOfInterestForUpdateDto, Entities.PointOfInterest>()
            Mapper.Map(pointOfInterestDto, pointOfInterestEntity);

            if (!_cityInfoRepository.Save())
            {
                throw new ApplicationException($"A problem while saving city Id: {cityId}, Point Of Int: {pointOfInterestId} .");
            }

            return(true);
        }
コード例 #4
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The name & description must not be same");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }
            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (pointOfInterestEntity == null)
            {
                return(NotFound());
            }
            Mapper.Map(pointOfInterest, pointOfInterestEntity);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request"));
            }
            return(NoContent());
        }
コード例 #5
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if ((pointOfInterest.Name ?? string.Empty).Equals(pointOfInterest.Description, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name");
            }

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

            var pointOfInterestFromStore = CitiesDataStore.Current.Cities
                                           .FirstOrDefault(c => c.Id == cityId)
                                           ?.PointsOfInterest
                                           .FirstOrDefault(p => p.Id == id);

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

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

            return(NoContent());
        }
コード例 #6
0
        public IActionResult UpdatePointOfInterest(int cityId, int point, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            PointOfInterest pointOfInterestEntity = _infoRepository.GetPointOfInterest(cityId, point);

            if (pointOfInterestEntity == null)
            {
                return(NotFound("Point of interest was not found"));
            }

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            if (!_infoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(NoContent());
        }
コード例 #7
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, PointOfInterestForUpdateDto pointOfInterestForUpdate)
        {
            if (pointOfInterestForUpdate.Name == pointOfInterestForUpdate.Description)
            {
                ModelState.AddModelError("Invalid fields", "Name and Descriptiong should be different");
            }

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

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterest(cityId, id);

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

            _mapper.Map(pointOfInterestForUpdate, pointOfInterestEntity);

            _cityInfoRepository.save();

            return(NoContent());
        }
コード例 #8
0
        public IActionResult UpdatePointOfInterest(int cityId, int pointId, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)            //Input Validation
            {
                return(BadRequest(ModelState)); //Return Bad request with the corresponding messages
            }

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, pointId);

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

            Mapper.Map(pointOfInterest, pointOfInterestEntity);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request"));
            }
            return(NoContent());
        }
コード例 #9
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_cityInfoRepository.DoesCityExist(cityId))
            {
                _logger.LogWarning($"The city with id={cityId} was not found.");
                return(NotFound());
            }
            var existingPointOfInterest = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

            if (existingPointOfInterest == null)
            {
                return(NotFound());
            }
            Mapper.Map(pointOfInterest, existingPointOfInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Error while trying to save updated point of interest"));
            }
            return(NoContent());
        }
コード例 #10
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

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

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

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

            // update all fields - PUT definition
            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            // return 204 no content - means resource was updated successfully there is nothing
            // to return as the client has all the data already
            return(NoContent());
        }
コード例 #11
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Please provide different description than name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

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

            var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(x => x.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }
            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
コード例 #12
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        //Update a point of interest
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

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

            _mapper.Map(pointOfInterest, pointOfInterestEntity);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request"));
            }
            return(NoContent());
        }
コード例 #13
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Name and Description can't be same.");
            }

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

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

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

            var pointOfInterestFormStore = city.PointOfInterest.FirstOrDefault(p => p.Id == id);

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

            pointOfInterestFormStore.Name        = pointOfInterest.Name;
            pointOfInterestFormStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
コード例 #14
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest(ModelState));
            }

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

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

            // Use map handmade

            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if (city == null)
            //{
            //    return BadRequest();
            //}

            //var pointOfInterestFromStore = city.PointOfInterest.FirstOrDefault(p => p.Id == id);
            //if(pointOfInterestFromStore == null)
            //{
            //    return BadRequest();
            //}

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

            //return NoContent();

            // =================================================== //

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

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

            Mapper.Map(pointOfInterest, pointOfInterestEntity);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happend while handling your request"));
            }

            return(NoContent());
        }
コード例 #15
0
        public IActionResult PartialUpdatePointOfInterest(int cityId, int id,
                                                          [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

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

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

            var pointsOfInterestFromStore = city.PointOfInterest.FirstOrDefault(p => p.Id == id);

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


            var pointOfInterestPatch =
                new PointOfInterestForUpdateDto()
            {
                Name        = pointsOfInterestFromStore.Name,
                Description = pointsOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestPatch, ModelState);


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



            if (pointOfInterestPatch.Description == pointOfInterestPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be differente from the name.");
            }

            TryValidateModel(pointOfInterestPatch);

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


            pointsOfInterestFromStore.Name        = pointOfInterestPatch.Name;
            pointsOfInterestFromStore.Description = pointOfInterestPatch.Description;

            return(NoContent());
        }
コード例 #16
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDocument)
        {
            // first lets check to make sure the patch document is ok
            if (patchDocument == null)
            {
                return(BadRequest());
            }

            // now we run our normal validation checks
            CityDto city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

            PointOfInterestDto pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

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

            PointOfInterestForUpdateDto pointOfInterestToPatch =
                new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            // here we are applying the incoming patch document to our update dto object to see if it passes the requirements
            patchDocument.ApplyTo(pointOfInterestToPatch, ModelState);

            // check the model state after the patch document has been applied to see if it passes.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // check our custom validation rule
            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different than the name.");
            }

            // now we are going to use a built in validation helper to check the model itself for its validation.
            TryValidateModel(pointOfInterestToPatch);

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

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
        [HttpPut("{id}")]//Full updatea are done using the HttpPut method.
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            //=============================Old Code==================================
            ////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");
            //}
            //check to see if a city exists before attetmping to add a point of interest to an unexisting city.
            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if (city == null)
            //{
            //    return NotFound();
            //}
            //Look for the Point of Interest.
            //var pointOfInterestFromStore = city.PointsOfInterest
            //    .FirstOrDefault(p => p.Id == id); //Check the point of interest ID.
            //if (pointOfInterest == null)
            //{
            //    return NotFound();
            //}
            //PUT should update all of the resource, except for the ID.
            //pointOfInterestFromStore.Name = pointOfInterest.Name;
            //pointOfInterestFromStore.Description = pointOfInterest.Description;
            //========================================================================

            //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 the city exists.
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

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

            //Over ride the elements in the destination object from the source object.
            _mapper.Map(pointOfInterest, pointOfInterestEntity);
            //Added for a best practice.
            _cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterestEntity);

            //Save the changes to the database.
            _cityInfoRepository.Save();

            return(NoContent()); //Return a No Content as the update was given by the consumer.
        }
コード例 #18
0
        public IActionResult UpdatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

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

            #region old code and manually mapped
            //var city = CitiesMockData.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if (city == null)
            //{
            //    return NotFound();
            //}

            //var pointOfInterestFromStore = city.PointsOfInterest
            //    .FirstOrDefault(p => p.Id == id);
            //if (pointOfInterest == null)
            //{
            //    return NotFound();
            //}

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

            //return NoContent(); // the means that the request completed successfully
            #endregion

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

            var pointOfInterestEntity = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

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

            _mapper.Map(pointOfInterest, pointOfInterestEntity);

            _cityInfoRepository.UpdatePointOfInterestForCity(cityId, pointOfInterestEntity);

            _cityInfoRepository.Save();

            return(NoContent());
        }
コード例 #19
0
 public bool UpdatePointOfInterest(int cityId, int pointOfInterestId, PointOfInterestForUpdateDto pointOfInterestDto)
 {
     try
     {
         return(_pointsOfInterestOperation.UpdatePointOfInterest(cityId, pointOfInterestId, pointOfInterestDto));
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "UpdatePointOfInterest error!!!");
         throw;
     }
 }
コード例 #20
0
    public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                        [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
    {
        if (patchDoc == null)
        {
            return(BadRequest());
        }

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

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

        var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(x => x.Id == id);

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

        var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
        {
            Name        = pointOfInterestFromStore.Name,
            Description = pointOfInterestFromStore.Description
        };

        patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

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

        //For not send "remove" as operation, removing a field that is requiered by the model
        TryValidateModel(pointOfInterestToPatch);
        if (!ModelState.IsValid)
        {
            return(BadRequest(ModelState));
        }

        pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
        pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

        return(NoContent());
    }
コード例 #21
0
        public IActionResult UpdatePointOfinterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForUpdateDto pointOfInterest)

        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be differnt from name");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            /* var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
             * if (city == null)
             * {
             *   return NotFound();
             * }*/

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

            /* var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);
             *
             * if (pointOfInterestFromStore == null)
             * {
             *   return NotFound();
             * }*/
            var pointOfInterestEntity = _cityInforepository.GetPointsOfInterestForCity(cityId, id);

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

            Mapper.Map(pointOfInterest, pointOfInterestEntity);

            /*    pointOfInterestFromStore.Name = pointOfInterest.Name;
             *  pointOfInterestFromStore.Description = pointOfInterest.Description;*/
            if (!_cityInforepository.Save())
            {
                return(StatusCode(500, "problem sa rukovanjem zahteva"));
            }
            return(NoContent());
        }
コード例 #22
0
        public IActionResult PartiallyUpdatedPointOfInterest(int cityId, int id,
                                                             [FromBodyAttribute] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest("No Patch Doc"));
            }

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

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

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

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

            var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError("Description", "Name and Description must not match.");
            }
            TryValidateModel(pointOfInterestToPatch);

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

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
コード例 #23
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,

                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc
                                                            )
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(o => o.ID == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            var pointOfInterestFromStor = city.PointOfInterest
                                          .FirstOrDefault(p => p.ID == id);

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

            var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStor.Name,
                Description = pointOfInterestFromStor.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

            if (pointOfInterestToPatch.Description == pointOfInterestToPatch.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The Provide Description Must be different than the Name Lolz"
                    );
            }
            if (!TryValidateModel(pointOfInterestToPatch))
            {
                return(BadRequest(ModelState));
            }

            pointOfInterestFromStor.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStor.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
コード例 #24
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int point, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

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

            PointOfInterest pointOfInterestEntity = _infoRepository.GetPointOfInterest(cityId, point);

            if (pointOfInterestEntity == null)
            {
                return(NotFound("Point of interest was not found"));
            }

            PointOfInterestForUpdateDto pointOfInterestToPatch = Mapper.Map <PointOfInterestForUpdateDto>(pointOfInterestEntity);

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

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

            TryValidateModel(pointOfInterestToPatch);

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

            Mapper.Map(pointOfInterestToPatch, pointOfInterestEntity);

            if (!_infoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(NoContent());
        }
コード例 #25
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        // We use JsonPatchDocument<PointOfInterestForUpdateDto> is because PointOfInterestForUpdateDto class doesn't have ID in it's class
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(item => item.Id == cityId);

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

            // find POI
            var pointOfInterestFromStore = city.PointOfInterest.FirstOrDefault(POI => POI.Id == id);

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

            var pointOfInterestPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestPatch, ModelState); // The patchDoc document may not exist and it's consumer's fault, we need to to check for that

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

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

            if (!(TryValidateModel(pointOfInterestPatch)))
            {
                return(BadRequest());
            }

            pointOfInterestFromStore.Name        = pointOfInterestPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestPatch.Description;

            return(NoContent());
        }
コード例 #26
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(NotFound());
            }

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

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

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

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

            // create a point of interest to patch - we can apply the patch to - see if it's valid
            var pointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

            TryValidateModel(pointOfInterestToPatch);

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

            pointOfInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointOfInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
コード例 #27
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var pointFromInterestFromStore = CitiesDataStore.Current.Cities
                                             .FirstOrDefault(c => c.Id == cityId)
                                             ?.PointsOfInterest
                                             .FirstOrDefault(p => p.Id == id);

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

            var pointOfInterestToPatch = new PointOfInterestForUpdateDto
            {
                Name        = pointFromInterestFromStore.Name,
                Description = pointFromInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

            if ((pointOfInterestToPatch.Name ?? string.Empty).Equals(pointOfInterestToPatch.Description, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name");
            }

            TryValidateModel(pointOfInterestToPatch);

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

            pointFromInterestFromStore.Name        = pointOfInterestToPatch.Name;
            pointFromInterestFromStore.Description = pointOfInterestToPatch.Description;

            return(NoContent());
        }
コード例 #28
0
        public IActionResult PatchPointOfInterest(int cityId, int id,
                                                  [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

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

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

            var poi = city.PointOfInterest.FirstOrDefault(p => p.Id == id);

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

            var poiToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = poi.Name,
                Description = poi.Description
            };

            patchDoc.ApplyTo(poiToPatch, ModelState);

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

            TryValidateModel(poiToPatch);

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

            poi.Name        = poiToPatch.Name;
            poi.Description = poiToPatch.Description;

            return(NoContent());
        }
コード例 #29
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, PointOfInterestForUpdateDto pointOfInterestForUpdateDto)
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

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

            pointOfInterest.Name        = pointOfInterestForUpdateDto.Name;
            pointOfInterest.Description = pointOfInterestForUpdateDto.Description;

            return(NoContent());
        }
コード例 #30
0
        public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id,
                                                            [FromBody] JsonPatchDocument <PointOfInterestForUpdateDto> patchDoc)
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

            var pointOfInterestFormStore = city.PointOfInterest.FirstOrDefault(p => p.Id == id);

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

            var finalPointOfInterestToPatch = new PointOfInterestForUpdateDto()
            {
                Name        = pointOfInterestFormStore.Name,
                Description = pointOfInterestFormStore.Description
            };

            patchDoc.ApplyTo(finalPointOfInterestToPatch, ModelState);

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

            pointOfInterestFormStore.Name        = finalPointOfInterestToPatch.Name;
            pointOfInterestFormStore.Description = finalPointOfInterestToPatch.Description;

            if (finalPointOfInterestToPatch.Name == finalPointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "Name and Description can't be same.");
            }

            if (!TryValidateModel(finalPointOfInterestToPatch))
            {
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }