예제 #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 <ActionResult <IEnumerable <CommandResponse> > > GetAllCommands()
        {
            var commandEntities = await CommandsServices.GetAllCommandsAsync();

            var response = Mapper.Map <IEnumerable <CommandResponse> >(commandEntities);

            return(Ok(response));
        }
예제 #3
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());
        }
예제 #4
0
        public async Task <ActionResult> UpdateCommand(CommandUpdate command, int id)
        {
            await CommandsServices.UpdateCommand(command, id);

            return(NoContent());
        }