Skip to main content

Execute queries from a Rest Controller

· One min read
Defrim Hasani
tip

You can clone an already prepared sample which is built on top of spring boot using the spring boot starter library.

src/adapters/rest/UsersController.java
@RestController
@RequestMapping("/users")
public class UsersController {

private final AlbanoiGateway albanoiGateway;

public UsersController(AlbanoiGateway albanoiGateway) {
this.albanoiGateway = albanoiGateway;
}

@PostMapping
public ResponseEntity<User> createUser(@RequestBody CreateUserRequest createUserRequest) {

// Commands are part of your domain
var createUserCommand = new CreateUserCommand();
createUserCommand.setUsername(createUserRequest.getUsername());

// You don't need to inject the handler itself, let the gateway handle it 😊
CommandResult<User> createUserResult = albanoiGateway.execute(createUserCommand, User.class);

return ResponseEntity.ok(createUserResult.getResult());
}
}