Skip to content

Commit b1582b2

Browse files
committed
Use Value Objects for Student
1 parent 4e9c42b commit b1582b2

File tree

11 files changed

+127
-29
lines changed

11 files changed

+127
-29
lines changed

apps/test/tv/codely/apps/mooc/backend/controller/students/StudentsPutControllerShould.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void register_a_valid_non_existing_student() throws Exception {
99
assertRequestWithBody(
1010
"PUT",
1111
"/students/1bab45ba-3c7a-4344-8936-78466eca17fa",
12-
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"some-email\"}",
12+
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"email@email.com\"}",
1313
201
1414
);
1515
}

src/mooc/main/tv/codely/mooc/students/application/StudentResponse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public StudentResponse(String id, String name, String surname, String email) {
1717
}
1818

1919
public static StudentResponse fromAggregate(Student student) {
20-
return new StudentResponse(student.id().value(), student.name(), student.surname(), student.email());
20+
return new StudentResponse(
21+
student.id().value(),
22+
student.name().value(),
23+
student.surname().value(),
24+
student.email().value()
25+
);
2126
}
2227

2328
public String id() {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package tv.codely.mooc.students.application.register;
22

3-
import tv.codely.mooc.students.domain.Student;
4-
import tv.codely.mooc.students.domain.StudentId;
5-
import tv.codely.mooc.students.domain.StudentRepository;
3+
import tv.codely.mooc.students.domain.*;
64
import tv.codely.shared.domain.Service;
75

86
@Service
@@ -14,7 +12,12 @@ public StudentRegistrar(StudentRepository repository) {
1412
}
1513

1614
public void register(RegisterStudentRequest request) {
17-
Student student = Student.create(new StudentId(request.id()), request.name(), request.surname(), request.email());
15+
Student student = Student.create(
16+
new StudentId(request.id()),
17+
new StudentName(request.name()),
18+
new StudentSurname(request.surname()),
19+
new StudentEmail(request.email())
20+
);
1821
repository.register(student);
1922
}
2023
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33
import java.util.Objects;
44

55
public final class Student {
6-
private final StudentId id;
7-
private final String name;
8-
private final String surname;
9-
private final String email;
6+
private final StudentId id;
7+
private final StudentName name;
8+
private final StudentSurname surname;
9+
private final StudentEmail email;
1010

11-
public Student(StudentId id, String name, String surname, String email) {
11+
public Student(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
1212
this.id = id;
1313
this.name = name;
1414
this.surname = surname;
1515
this.email = email;
1616
}
1717

18-
public static Student create(StudentId id, String name, String surname, String email) {
18+
public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
1919
return new Student(id, name, surname, email);
2020
}
2121

2222
public StudentId id() {
2323
return id;
2424
}
2525

26-
public String name() {
26+
public StudentName name() {
2727
return name;
2828
}
2929

30-
public String surname() {
30+
public StudentSurname surname() {
3131
return surname;
3232
}
3333

34-
public String email() {
34+
public StudentEmail email() {
3535
return email;
3636
}
3737

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tv.codely.mooc.students.domain;
2+
3+
import tv.codely.shared.domain.EmailValueObject;
4+
5+
public final class StudentEmail extends EmailValueObject {
6+
public StudentEmail(String email) {
7+
super(email);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tv.codely.mooc.students.domain;
2+
3+
import tv.codely.shared.domain.StringValueObject;
4+
5+
public final class StudentName extends StringValueObject {
6+
public StudentName(String value) {
7+
super(value);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tv.codely.mooc.students.domain;
2+
3+
import tv.codely.shared.domain.StringValueObject;
4+
5+
public final class StudentSurname extends StringValueObject {
6+
public StudentSurname(String value) {
7+
super(value);
8+
}
9+
}

src/mooc/main/tv/codely/mooc/students/infrastructure/InMemoryStudentRepository.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package tv.codely.mooc.students.infrastructure;
22

3-
import tv.codely.mooc.students.domain.Student;
4-
import tv.codely.mooc.students.domain.StudentId;
5-
import tv.codely.mooc.students.domain.StudentRepository;
3+
import tv.codely.mooc.students.domain.*;
64
import tv.codely.shared.domain.Service;
75
import tv.codely.shared.domain.UuidGenerator;
86

@@ -20,11 +18,22 @@ public InMemoryStudentRepository(UuidGenerator generator) {
2018
@Override
2119
public List<Student> searchAll() {
2220
return Arrays.asList(
23-
new Student(new StudentId(generator.generate()), "name", "surname", "email@mail.com"),
24-
new Student(new StudentId(generator.generate()), "Other name", "Other surname", "another@mail.com")
21+
new Student(
22+
new StudentId(generator.generate()),
23+
new StudentName("name"),
24+
new StudentSurname("surname"),
25+
new StudentEmail("email@mail.com")
26+
),
27+
new Student(
28+
new StudentId(generator.generate()),
29+
new StudentName("Other name"),
30+
new StudentSurname("Other surname"),
31+
new StudentEmail("another@mail.com")
32+
)
2533
);
2634
}
2735

2836
@Override
29-
public void register(Student student) {}
37+
public void register(Student student) {
38+
}
3039
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package tv.codely.mooc.students.application.register;
22

33
import org.junit.jupiter.api.Test;
4-
import tv.codely.mooc.students.domain.Student;
5-
import tv.codely.mooc.students.domain.StudentId;
6-
import tv.codely.mooc.students.domain.StudentRepository;
4+
import tv.codely.mooc.students.domain.*;
75
import tv.codely.shared.domain.UuidMother;
86

97
import static org.mockito.Mockito.*;
@@ -14,12 +12,14 @@ void register_a_valid_student() {
1412
StudentRepository repository = mock(StudentRepository.class);
1513
StudentRegistrar registrar = new StudentRegistrar(repository);
1614

17-
StudentId id = new StudentId(UuidMother.random());
18-
String name = "name";
19-
String surname = "surname";
20-
String email = "email";
15+
StudentId id = new StudentId(UuidMother.random());
16+
StudentName name = new StudentName("name");
17+
StudentSurname surname = new StudentSurname("surname");
18+
StudentEmail email = new StudentEmail("email@email.com");
2119

22-
RegisterStudentRequest request = new RegisterStudentRequest(id.value(), name, surname, email);
20+
RegisterStudentRequest request = new RegisterStudentRequest(
21+
id.value(), name.value(), surname.value(), email.value()
22+
);
2323

2424
Student student = new Student(id, name, surname, email);
2525

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tv.codely.shared.domain;
2+
3+
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;
4+
5+
import java.util.Objects;
6+
7+
public abstract class EmailValueObject {
8+
private static final EmailValidator emailValidator = new EmailValidator();
9+
private final String email;
10+
11+
public EmailValueObject(String email) {
12+
ensureValidEmail(email);
13+
this.email = email;
14+
}
15+
16+
public String value() {
17+
return email;
18+
}
19+
20+
private void ensureValidEmail(String value) {
21+
if (!emailValidator.isValid(value, null)) {
22+
throw new InvalidEmail(value);
23+
}
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return this.value();
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) {
34+
return true;
35+
}
36+
if (!(o instanceof EmailValueObject)) {
37+
return false;
38+
}
39+
EmailValueObject that = (EmailValueObject) o;
40+
return Objects.equals(email, that.email);
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return Objects.hash(email);
46+
}
47+
}

0 commit comments

Comments
 (0)