Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Update User Aggregate
Browse files Browse the repository at this point in the history
-Use @aggregate annotation for Spring
-Set aggregate repository bean on @aggregate annotation
-Put all @CommandHandler annotations on the command handling functions
-Drop the UserCommandHandler
-Rename command handler function to `handle(cmd)`
-Replace @eventhandler for @EventSourcingHandler
-Rename event handler functions to `on(event)`
-Drop useless @author tag
-Overall reindent of file
-Rename UserCommandHandlerTest to UserTest
-Clean up the test case

#28
  • Loading branch information
smcvb committed Jun 1, 2018
1 parent 5e7b255 commit ae96902
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 93 deletions.
Expand Up @@ -16,52 +16,50 @@

package org.axonframework.samples.trader.users.command;

import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.commandhandling.model.AggregateRoot;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.samples.trader.api.users.UserAuthenticatedEvent;
import org.axonframework.samples.trader.api.users.UserCreatedEvent;
import org.axonframework.samples.trader.api.users.UserId;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.samples.trader.api.users.*;
import org.axonframework.samples.trader.users.util.DigestUtils;
import org.axonframework.spring.stereotype.Aggregate;

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

/**
* @author Jettro Coenradie
*/
@AggregateRoot
@Aggregate(repository = "userRepository")
public class User {
private static final long serialVersionUID = 3291411359839192350L;

@AggregateIdentifier
private UserId userId;
private String passwordHash;

public User() {
// Required by Axon Framework
}

public User(UserId userId, String username, String name, String password) {
apply(new UserCreatedEvent(userId, name, username, hashOf(password.toCharArray())));
@CommandHandler
public User(CreateUserCommand cmd) {
apply(new UserCreatedEvent(cmd.getUserId(),
cmd.getName(),
cmd.getUsername(),
hashOf(cmd.getPassword().toCharArray())));
}

public boolean authenticate(char[] password) {
boolean success = this.passwordHash.equals(hashOf(password));
@CommandHandler
public boolean handle(AuthenticateUserCommand cmd) {
boolean success = this.passwordHash.equals(hashOf(cmd.getPassword()));
if (success) {
apply(new UserAuthenticatedEvent(userId));
}
return success;
}

@EventHandler
public void onUserCreated(UserCreatedEvent event) {
@EventSourcingHandler
public void on(UserCreatedEvent event) {
this.userId = event.getUserId();
this.passwordHash = event.getPassword();
}

private String hashOf(char[] password) {
return DigestUtils.sha1(String.valueOf(password));
}

public UserId getIdentifier() {
return userId;
}
}

This file was deleted.

0 comments on commit ae96902

Please sign in to comment.