Skip to content

Commit 6f3da18

Browse files
committed
Raise event on Student registered
1 parent 586c0d2 commit 6f3da18

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

src/mooc/main/tv/codely/mooc/students/application/register/StudentRegistrar.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import tv.codely.mooc.students.domain.*;
44
import tv.codely.shared.domain.Service;
5+
import tv.codely.shared.domain.bus.event.EventBus;
56

67
@Service
78
public class StudentRegistrar {
89
private final StudentRepository repository;
10+
private final EventBus eventBus;
911

10-
public StudentRegistrar(StudentRepository repository) {
12+
public StudentRegistrar(StudentRepository repository, EventBus eventBus) {
1113
this.repository = repository;
14+
this.eventBus = eventBus;
1215
}
1316

1417
public void register(RegisterStudentRequest request) {
@@ -19,5 +22,6 @@ public void register(RegisterStudentRequest request) {
1922
new StudentEmail(request.email())
2023
);
2124
repository.register(student);
25+
eventBus.publish(student.pullDomainEvents());
2226
}
2327
}

src/mooc/main/tv/codely/mooc/students/domain/Student.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package tv.codely.mooc.students.domain;
22

3+
import tv.codely.shared.domain.AggregateRoot;
4+
import tv.codely.shared.domain.student.StudentRegisteredDomainEvent;
5+
36
import java.util.Objects;
47

5-
public final class Student {
8+
public final class Student extends AggregateRoot {
69
private final StudentId id;
710
private final StudentName name;
811
private final StudentSurname surname;
@@ -23,7 +26,11 @@ private Student() {
2326
}
2427

2528
public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
26-
return new Student(id, name, surname, email);
29+
Student student = new Student(id, name, surname, email);
30+
31+
student.record(new StudentRegisteredDomainEvent(id.value(), name.value(), surname.value(), email.value()));
32+
33+
return student;
2734
}
2835

2936
public StudentId id() {

src/mooc/test/tv/codely/mooc/students/application/register/StudentRegistrarTestShould.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import tv.codely.mooc.students.StudentsModuleUnitTestCase;
66
import tv.codely.mooc.students.domain.Student;
77
import tv.codely.mooc.students.domain.StudentMother;
8+
import tv.codely.mooc.students.domain.StudentRegisteredDomainEventMother;
9+
import tv.codely.shared.domain.student.StudentRegisteredDomainEvent;
810

911
final class StudentRegistrarTestShould extends StudentsModuleUnitTestCase {
1012
StudentRegistrar registrar;
@@ -13,16 +15,18 @@ final class StudentRegistrarTestShould extends StudentsModuleUnitTestCase {
1315
protected void setUp() {
1416
super.setUp();
1517

16-
registrar = new StudentRegistrar(repository);
18+
registrar = new StudentRegistrar(repository, eventBus);
1719
}
1820

1921
@Test
2022
void register_a_valid_student() {
21-
RegisterStudentRequest request = RegisterStudentRequestMother.random();
22-
Student student = StudentMother.fromRequest(request);
23+
RegisterStudentRequest request = RegisterStudentRequestMother.random();
24+
Student student = StudentMother.fromRequest(request);
25+
StudentRegisteredDomainEvent domainEvent = StudentRegisteredDomainEventMother.fromStudent(student);
2326

2427
registrar.register(request);
2528

2629
shouldHaveSaved(student);
30+
shouldHavePublished(domainEvent);
2731
}
2832
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tv.codely.mooc.students.domain;
2+
3+
import tv.codely.shared.domain.student.StudentRegisteredDomainEvent;
4+
5+
public final class StudentRegisteredDomainEventMother {
6+
public static StudentRegisteredDomainEvent create(
7+
StudentId id, StudentName name, StudentSurname surname, StudentEmail email
8+
) {
9+
return new StudentRegisteredDomainEvent(id.value(), name.value(), surname.value(), email.value());
10+
}
11+
12+
public static StudentRegisteredDomainEvent fromStudent(Student student) {
13+
return create(student.id(), student.name(), student.surname(), student.email());
14+
}
15+
16+
public static StudentRegisteredDomainEvent random() {
17+
return create(
18+
StudentIdMother.random(),
19+
StudentNameMother.random(),
20+
StudentSurnameMother.random(),
21+
StudentEmailMother.random()
22+
);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package tv.codely.shared.domain.student;
2+
3+
import tv.codely.shared.domain.bus.event.DomainEvent;
4+
5+
import java.io.Serializable;
6+
import java.util.HashMap;
7+
import java.util.Objects;
8+
9+
public final class StudentRegisteredDomainEvent extends DomainEvent {
10+
private final String name;
11+
private final String surname;
12+
private final String email;
13+
14+
public StudentRegisteredDomainEvent() {
15+
super(null);
16+
17+
this.name = null;
18+
this.surname = null;
19+
this.email = null;
20+
}
21+
22+
public StudentRegisteredDomainEvent(String aggregateId, String name, String surname, String email) {
23+
super(aggregateId);
24+
25+
this.name = name;
26+
this.surname = surname;
27+
this.email = email;
28+
}
29+
30+
public StudentRegisteredDomainEvent(
31+
String aggregateId,
32+
String eventId,
33+
String occurredOn,
34+
String name,
35+
String surname,
36+
String email
37+
) {
38+
super(aggregateId, eventId, occurredOn);
39+
40+
this.name = name;
41+
this.surname = surname;
42+
this.email = email;
43+
}
44+
45+
@Override
46+
public String eventName() {
47+
return "course.created";
48+
}
49+
50+
@Override
51+
public HashMap<String, Serializable> toPrimitives() {
52+
return new HashMap<String, Serializable>() {{
53+
put("name", name);
54+
put("surname", surname);
55+
put("email", email);
56+
}};
57+
}
58+
59+
@Override
60+
public StudentRegisteredDomainEvent fromPrimitives(
61+
String aggregateId,
62+
HashMap<String, Serializable> body,
63+
String eventId,
64+
String occurredOn
65+
) {
66+
return new StudentRegisteredDomainEvent(
67+
aggregateId,
68+
eventId,
69+
occurredOn,
70+
(String) body.get("name"),
71+
(String) body.get("surname"),
72+
(String) body.get("email")
73+
);
74+
}
75+
76+
public String name() {
77+
return name;
78+
}
79+
80+
public String surname() {
81+
return surname;
82+
}
83+
84+
public String email() {
85+
return email;
86+
}
87+
88+
@Override
89+
public boolean equals(Object o) {
90+
if (this == o) {
91+
return true;
92+
}
93+
if (o == null || getClass() != o.getClass()) {
94+
return false;
95+
}
96+
StudentRegisteredDomainEvent that = (StudentRegisteredDomainEvent) o;
97+
return name.equals(that.name) &&
98+
surname.equals(that.surname) &&
99+
email.equals(that.email);
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
return Objects.hash(name, surname, email);
105+
}
106+
}

0 commit comments

Comments
 (0)