Beispiel #1
0
      public IActionResult UpdatePointOfInterest(int cityId, [FromBody] PointOfInterestForUpdate pointOfIntrerest, int id)
      {
          if (!ModelState.IsValid)
          {
              return(BadRequest());
          }

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

          if (city == null)
          {
              return(BadRequest());
          }
          var point = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

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

          point.Name        = pointOfIntrerest.Name;
          point.Description = pointOfIntrerest.Description;

          return(NoContent());
      }
        public IActionResult UpdatePointOfInterest(int cityId, int poiId, [FromBody] PointOfInterestForUpdate pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Description must be different than Name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = GetCity(cityId);

            if (city == null)
            {
                return(NotFound());
            }
            var pointOfInterestFromStore = city.PointOfInterests.FirstOrDefault(p => p.Id == poiId);

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

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;
            return(NoContent());
        }
Beispiel #3
0
      public IActionResult PartiallyUpdatePointOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestForUpdate> patchDocument)
      {
          var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == id);

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

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

          var pointToPatch = new PointOfInterestForUpdate()
          {
              Name        = point.Name,
              Description = point.Description
          };


          patchDocument.ApplyTo(pointToPatch, ModelState);
          //luam formatul json din body, ii facem apply pe un model si apoi setam valorile
          point.Name        = pointToPatch.Name;
          point.Description = pointToPatch.Description;
          if (!ModelState.IsValid)
          {
              return(BadRequest());
          }

          return(NoContent());
      }
Beispiel #4
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForUpdate pointOfInterest)
        {
            // Body from the request
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

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

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

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

            // Maps the Request Body to the Entity
            Mapper.Map(pointOfInterest, pointOfInterestEntity);

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

            return(NoContent());

            #region Without AutoMapper
            // // Validating City existence
            // var city = CitiesDataStore.Current.Cities.FirstOrDefault (c => c.Id == cityId);

            // if (city == null) return NotFound ();

            // // Validating Point of Interest existence
            // var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault (p => p.Id == id);

            // if (pointOfInterestFromStore == null) return NotFound ();

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

            // // Update successful, nothing to return
            // return NoContent ();
            #endregion
        }
        public IActionResult PartiallyUpdatePointOfInterest(
            int cityId,
            int id,
            [FromBody] JsonPatchDocument <PointOfInterestForUpdate> patchDoc
            )
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var city = Store.CitiesStore.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 PointOfInterestForUpdate()
            {
                Name        = pointOfInterestFromStore.Name,
                Description = pointOfInterestFromStore.Description
            };

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

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

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

            TryValidateModel(pointOfInterestToPatch);

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

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

            return(NoContent());
        }
Beispiel #6
0
        public IActionResult UpdatePointOfInterest(int cityId, int poiId,
                                                   [FromBody] PointOfInterestForUpdate pointOfInterest)
        {
            //If the data sent cannot be de-serialized into a PointsOfInterestForUpdate, the POI will be null
            if (pointOfInterest == null)
            {
                return(BadRequest()); //The consuming app messed up when sending data, so let them know it was a bad request.
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description and Name can not be the same");
            }

            //Check for things like the Required or MaxLength Attributes, which can make the model state invalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // See if the city that was passed in exists in the collection
            var doesCityExist = _cityInfoRepo.CityExists(cityId);

            if (doesCityExist == false)
            {
                return(NotFound());
            }

            //Get the POI entity that's in the DB, so we can update it.
            var pointOfInterestEntity = _cityInfoRepo.GetPointOfInterestForCity(cityId, poiId);

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

            // The first argument is the new Object you're using to overwrite the existing entity. The second argument is the existing entity in the DB
            AutoMapper.Mapper.Map(pointOfInterest, pointOfInterestEntity); //This will overwrite the pointOfInterestEntity with the new pointOfInterest

            if (!_cityInfoRepo.Save())
            {
                _logger.LogInformation("There was a problem when trying to update an existing POI in the database.");
                return(StatusCode(500, "A problem occured while handling your request"));
            }

            //Since we're just updating
            return(NoContent());
        }
        public IActionResult PartialUpdatePoint(int cityId, int poiId, [FromBody] JsonPatchDocument <PointOfInterestForUpdate> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var city = GetCity(cityId);

            if (city == null)
            {
                return(NotFound());
            }
            var pointOfInterestFromStore = city.PointOfInterests.FirstOrDefault(p => p.Id == poiId);

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

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

            patchDoc.ApplyTo(pointOfInterestToPatch, ModelState);

            if (pointOfInterestToPatch.Name == pointOfInterestToPatch.Description)
            {
                ModelState.AddModelError("Description", "Cannot have name and description be equal.");
            }

            TryValidateModel(pointOfInterestToPatch);

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


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

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

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

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

            var city = Store.CitiesStore.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());
            }

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

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

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

            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());
        }
Beispiel #10
0
        public IActionResult UpdatePointOfInterest(int cityId, int pointOfInterestIdToUpdate, [FromBody] PointOfInterestForUpdate pointOfInterestUpdated)
        {
            if (_cityInfoRepository.DoesCityExist(cityId))
            {
                return(NotFound());
            }

            var pointOfInterest = _cityInfoRepository.GetCityPointOfInterest(cityId, pointOfInterestIdToUpdate);

            if (pointOfInterest == null)
            {
                return(NotFound());
            }
            // When you do this, the first parameters gets the values from the second one, its a way to update using mapping~
            _mapper.Map(pointOfInterest, pointOfInterestUpdated);

            //IMPORTANT
            // Entity framework can update an resource by tracking it, whitch means that a resource can be updated
            // by searching it in the DB and then changing its chaning its values follow by saving the changes,
            // but it is a good practice that the Repository implements an update method in case we would like to change
            // teclogies, so we include it anyway for future cases where we could need it, but again, for updating we could just
            // update the resource directly and then save.
            _cityInfoRepository.UpdatePointOfInterest(cityId, pointOfInterest);
            _cityInfoRepository.Save();

            return(NoContent());
        }