Beispiel #1
0
        public IActionResult UpdateRating(int itemId, int id, [FromBody] RatingForUpdateDto rating)
        {
            if (rating.Description == rating.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

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

            var item = ItemsDataStore.Current.Items.FirstOrDefault(x => x.Id == itemId);

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

            var ratingFromStore = item.Rating.FirstOrDefault(x => x.Id == id);

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

            ratingFromStore.Name        = rating.Name;
            ratingFromStore.Description = rating.Description;

            return(NoContent());
        }
Beispiel #2
0
        public IActionResult PatchRating(int itemId, int id, [FromBody] JsonPatchDocument <RatingForUpdateDto> patchDoc)
        {
            var item = ItemsDataStore.Current.Items.FirstOrDefault(x => x.Id == itemId);

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

            var ratingFromStore = item.Rating.FirstOrDefault(x => x.Id == id);

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

            var ratingToPatch = new RatingForUpdateDto()
            {
                Name        = ratingFromStore.Name,
                Description = ratingFromStore.Description
            };

            patchDoc.ApplyTo(ratingToPatch, ModelState);

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

            //check if the patch Dto is Valid (ModelState.IsValid above only check if the PatchDocument is valid )
            if (ratingToPatch.Description == ratingToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }

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

            ratingFromStore.Name        = ratingToPatch.Name;
            ratingFromStore.Description = ratingToPatch.Description;

            return(NoContent());
        }