Skip to content

Commit 932a2ad

Browse files
committed
implement registration in domain model and repository
1 parent 36e4686 commit 932a2ad

26 files changed

+816
-1
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
<artifactId>spring-boot-starter-test</artifactId>
5858
<scope>test</scope>
5959
</dependency>
60+
<dependency>
61+
<groupId>com.h2database</groupId>
62+
<artifactId>h2</artifactId>
63+
<scope>test</scope>
64+
</dependency>
6065
</dependencies>
6166

6267
<build>

src/main/java/com/taskagile/domain/application/commands/RegistrationCommand.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.taskagile.domain.application.commands;
22

3+
import org.springframework.util.Assert;
4+
35
public class RegistrationCommand {
46

57
private String username;
68
private String emailAddress;
79
private String password;
810

911
public RegistrationCommand(String username, String emailAddress, String password) {
12+
Assert.hasText(username, "Parameter `username` must not be empty");
13+
Assert.hasText(emailAddress, "Parameter `emailAddress` must not be empty");
14+
Assert.hasText(password, "Parameter `password` must not be empty");
15+
1016
this.username = username;
1117
this.emailAddress = emailAddress;
1218
this.password = password;

src/main/java/com/taskagile/domain/application/impl/UserServiceImpl.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,52 @@
22

33
import com.taskagile.domain.application.UserService;
44
import com.taskagile.domain.application.commands.RegistrationCommand;
5+
import com.taskagile.domain.common.event.DomainEventPublisher;
6+
import com.taskagile.domain.common.mail.MailManager;
7+
import com.taskagile.domain.common.mail.MessageVariable;
58
import com.taskagile.domain.model.user.RegistrationException;
9+
import com.taskagile.domain.model.user.RegistrationManagement;
10+
import com.taskagile.domain.model.user.User;
11+
import com.taskagile.domain.model.user.events.UserRegisteredEvent;
612
import org.springframework.stereotype.Service;
13+
import org.springframework.util.Assert;
14+
15+
import javax.transaction.Transactional;
716

817
@Service
18+
@Transactional
919
public class UserServiceImpl implements UserService {
1020

21+
private RegistrationManagement registrationManagement;
22+
private DomainEventPublisher domainEventPublisher;
23+
private MailManager mailManager;
24+
25+
public UserServiceImpl(RegistrationManagement registrationManagement,
26+
DomainEventPublisher domainEventPublisher,
27+
MailManager mailManager) {
28+
this.registrationManagement = registrationManagement;
29+
this.domainEventPublisher = domainEventPublisher;
30+
this.mailManager = mailManager;
31+
}
32+
1133
@Override
1234
public void register(RegistrationCommand command) throws RegistrationException {
13-
// TODO implement this
35+
Assert.notNull(command, "Parameter `command` must not be null");
36+
User newUser = registrationManagement.register(
37+
command.getUsername(),
38+
command.getEmailAddress(),
39+
command.getPassword());
40+
41+
sendWelcomeMessage(newUser);
42+
domainEventPublisher.publish(new UserRegisteredEvent(newUser));
1443
}
1544

45+
private void sendWelcomeMessage(User user) {
46+
mailManager.send(
47+
user.getEmailAddress(),
48+
"Welcome to TaskAgile",
49+
"welcome.ftl",
50+
MessageVariable.from("user", user)
51+
);
52+
}
1653
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.taskagile.domain.common.event;
2+
3+
import org.springframework.context.ApplicationEventPublisher;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* The default implementation of DomainEventPublisher that
8+
* bases on Spring Application Event
9+
*/
10+
@Component
11+
public class DefaultDomainEventPublisher implements DomainEventPublisher {
12+
13+
private ApplicationEventPublisher actualPublisher;
14+
15+
public DefaultDomainEventPublisher(ApplicationEventPublisher actualPublisher) {
16+
this.actualPublisher = actualPublisher;
17+
}
18+
19+
@Override
20+
public void publish(DomainEvent event) {
21+
actualPublisher.publishEvent(event);
22+
}
23+
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.taskagile.domain.common.event;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
/**
6+
* Domain event
7+
*/
8+
public abstract class DomainEvent extends ApplicationEvent {
9+
10+
private static final long serialVersionUID = -444783093811334147L;
11+
12+
public DomainEvent(Object source) {
13+
super(source);
14+
}
15+
16+
/**
17+
* Get the timestamp this event occurred
18+
*/
19+
public long occurredAt() {
20+
// Return the underlying implementation's timestamp
21+
return getTimestamp();
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.taskagile.domain.common.event;
2+
3+
public interface DomainEventPublisher {
4+
5+
/**
6+
* Publish a domain event
7+
*/
8+
void publish(DomainEvent event);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.taskagile.domain.common.mail;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class DefaultMailManager implements MailManager {
7+
8+
@Override
9+
public void send(String emailAddress, String subject, String template, MessageVariable... variables) {
10+
// TODO implement this
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.taskagile.domain.common.mail;
2+
3+
public interface MailManager {
4+
5+
/**
6+
* Send a message to a recipient
7+
*
8+
* @param emailAddress the recipient's email address
9+
* @param subject the subject key of the email
10+
* @param template the template file name of the email
11+
* @param variables message variables in the template file
12+
*/
13+
void send(String emailAddress, String subject, String template, MessageVariable... variables);
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.taskagile.domain.common.mail;
2+
3+
import java.util.Objects;
4+
5+
public class MessageVariable {
6+
private String key;
7+
private Object value;
8+
9+
private MessageVariable(String key, Object value) {
10+
this.key = key;
11+
this.value = value;
12+
}
13+
14+
public static MessageVariable from(String key, Object value) {
15+
return new MessageVariable(key, value);
16+
}
17+
18+
public String getKey() {
19+
return key;
20+
}
21+
22+
public Object getValue() {
23+
return value;
24+
}
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o) return true;
29+
if (!(o instanceof MessageVariable)) return false;
30+
MessageVariable that = (MessageVariable) o;
31+
return Objects.equals(key, that.key) &&
32+
Objects.equals(value, that.value);
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
return Objects.hash(key, value);
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "MessageVariable{" +
43+
"key='" + key + '\'' +
44+
", value=" + value +
45+
'}';
46+
}
47+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.taskagile.domain.common.model;
2+
3+
import java.io.Serializable;
4+
5+
public abstract class AbstractBaseEntity implements Serializable {
6+
7+
private static final long serialVersionUID = -1153931912966528994L;
8+
9+
public abstract boolean equals(Object obj);
10+
11+
public abstract int hashCode();
12+
13+
public abstract String toString();
14+
15+
}

0 commit comments

Comments
 (0)