Skip to content

Instantly share code, notes, and snippets.

@jaocamp
Last active August 23, 2018 03:42
Show Gist options
  • Save jaocamp/3d20cf264c68557e253741d9877636c4 to your computer and use it in GitHub Desktop.
Save jaocamp/3d20cf264c68557e253741d9877636c4 to your computer and use it in GitHub Desktop.
CQRS e Event Sourcing com Axon Framework e Spring Boot - BankAccountController.java
package br.com.coderef.controller;
import br.com.coderef.command.AddBankAccountCommand;
import br.com.coderef.command.RemoveBankAccountCommand;
import br.com.coderef.command.UpdateBalanceBankAccountCommand;
import br.com.coderef.dto.BankAccountDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/bank-accounts")
public class BankAccountController {
private CommandGateway commandGateway;
@PostMapping
public CompletableFuture<String> create(@RequestBody BankAccountDTO dto) {
var command = new AddBankAccountCommand(UUID.randomUUID().toString(), dto.getName());
log.info("Executing command: {}", command);
return commandGateway.send(command);
}
@PutMapping("/{id}/balances")
public CompletableFuture<String> updateBalance(@PathVariable String id, @RequestBody BankAccountDTO dto) {
var command = new UpdateBalanceBankAccountCommand(id, dto.getBalance());
log.info("Executing command: {}", command);
return commandGateway.send(command);
}
@DeleteMapping("/{id}")
public CompletableFuture<String> remove(@PathVariable String id) {
var command = new RemoveBankAccountCommand(id);
log.info("Executing command: {}", command);
return commandGateway.send(command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment