예제 #1
0
        public async Task <IActionResult> PatchItem(long id, [FromBody] JsonPatchDocument <UpdateItem.Query> itemPatchDocument)
        {
            if (itemPatchDocument == null)
            {
                return(BadRequest());
            }

            // Step 1: Retrieve original copy
            var originalItemDetail = await _mediator.Send(new GetItemById.Query
            {
                Id = id
            });

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

            // Step 2: Call ApplyTo() to modify original copy.
            //         Note in this step we manually create the original copy
            var itemToUpdateQuery = new UpdateItem.Query
            {
                Name       = originalItemDetail.Name,
                IsComplete = originalItemDetail.IsComplete
            };

            itemPatchDocument.ApplyTo(itemToUpdateQuery, ModelState);

            // Step 3: Ensure Model is still valid after applying changes
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Step 4: Persist modified copy
            var updatedItemDto = await _mediator.Send(new UpdateItem.Command
            {
                Id           = id,
                ItemToUpdate = itemToUpdateQuery
            });

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

            return(Ok(updatedItemDto));
        }
예제 #2
0
        public async Task <IActionResult> UpdateItem(long id, [FromBody] UpdateItem.Query itemToUpdateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var updatedItemDto = await _mediator.Send(new UpdateItem.Command
            {
                Id           = id,
                ItemToUpdate = itemToUpdateDto
            });

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

            return(Ok(updatedItemDto));
        }