コード例 #1
0
        public async Task <IActionResult> UpdateCommand(int id, [FromBody] JsonPatchDocument <CommandUpdate> patchDocument)
        {
            // To build a patch endpoint in C# we need JSONPatch package for ASPNET Core
            // and NewtonsoftJson

            // Check if we have that command
            var registerComand = await CommandsServices.GetCommandById(id);

            if (registerComand is null)
            {
                return(NotFound());
            }

            // Map our command to a command update
            var commandToPatch = Mapper.Map <CommandUpdate>(registerComand);

            // Apply JsonPatch to our command!
            patchDocument.ApplyTo(commandToPatch, ModelState);

            // Check if everything went well
            if (!TryValidateModel(commandToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            // Update our command!
            await CommandsServices.UpdateCommand(commandToPatch, id);

            await CommandsServices.SaveChangesAsync();

            // Return NoContent as specified by REST especifications
            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> DeleteCommandById(int id)
        {
            var command = await CommandsServices.GetCommandById(id);

            if (command == null)
            {
                return(BadRequest
                       (
                           new
                {
                    mensagem = $"Não foi possível encontrar o comando com o id {id}",
                    statusCode = HttpStatusCode.BadRequest
                }
                       ));
            }

            await CommandsServices.DeleteCommandById(command);

            return(NoContent());
        }