Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add register student use case #60

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void register_a_valid_non_existing_student() throws Exception {
assertRequestWithBody(
"PUT",
"/students/1bab45ba-3c7a-4344-8936-78466eca17fa",
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"some-email\"}",
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"email@email.com\"}",
201
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public StudentResponse(String id, String name, String surname, String email) {
}

public static StudentResponse fromAggregate(Student student) {
return new StudentResponse(student.id().value(), student.name(), student.surname(), student.email());
return new StudentResponse(
student.id().value(),
student.name().value(),
student.surname().value(),
student.email().value()
);
}

public String id() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tv.codely.mooc.students.application.register;

import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.StudentId;
import tv.codely.mooc.students.domain.StudentRepository;
import tv.codely.mooc.students.domain.*;
import tv.codely.shared.domain.Service;

@Service
Expand All @@ -14,7 +12,12 @@ public StudentRegistrar(StudentRepository repository) {
}

public void register(RegisterStudentRequest request) {
Student student = Student.create(new StudentId(request.id()), request.name(), request.surname(), request.email());
Student student = Student.create(
new StudentId(request.id()),
new StudentName(request.name()),
new StudentSurname(request.surname()),
new StudentEmail(request.email())
);
repository.register(student);
}
}
18 changes: 9 additions & 9 deletions src/mooc/main/tv/codely/mooc/students/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
import java.util.Objects;

public final class Student {
private final StudentId id;
private final String name;
private final String surname;
private final String email;
private final StudentId id;
private final StudentName name;
private final StudentSurname surname;
private final StudentEmail email;

public Student(StudentId id, String name, String surname, String email) {
public Student(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
this.id = id;
this.name = name;
this.surname = surname;
this.email = email;
}

public static Student create(StudentId id, String name, String surname, String email) {
public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
return new Student(id, name, surname, email);
}

public StudentId id() {
return id;
}

public String name() {
public StudentName name() {
return name;
}

public String surname() {
public StudentSurname surname() {
return surname;
}

public String email() {
public StudentEmail email() {
return email;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.EmailValueObject;

public final class StudentEmail extends EmailValueObject {
public StudentEmail(String email) {
super(email);
}
}
9 changes: 9 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/StudentName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.StringValueObject;

public final class StudentName extends StringValueObject {
public StudentName(String value) {
super(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.StringValueObject;

public final class StudentSurname extends StringValueObject {
public StudentSurname(String value) {
super(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tv.codely.mooc.students.infrastructure;

import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.StudentId;
import tv.codely.mooc.students.domain.StudentRepository;
import tv.codely.mooc.students.domain.*;
import tv.codely.shared.domain.Service;
import tv.codely.shared.domain.UuidGenerator;

Expand All @@ -20,11 +18,22 @@ public InMemoryStudentRepository(UuidGenerator generator) {
@Override
public List<Student> searchAll() {
return Arrays.asList(
new Student(new StudentId(generator.generate()), "name", "surname", "[email protected]"),
new Student(new StudentId(generator.generate()), "Other name", "Other surname", "[email protected]")
new Student(
new StudentId(generator.generate()),
new StudentName("name"),
new StudentSurname("surname"),
new StudentEmail("[email protected]")
),
new Student(
new StudentId(generator.generate()),
new StudentName("Other name"),
new StudentSurname("Other surname"),
new StudentEmail("[email protected]")
)
);
}

@Override
public void register(Student student) {}
public void register(Student student) {
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tv.codely.mooc.students.application.register;

import org.junit.jupiter.api.Test;
import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.StudentId;
import tv.codely.mooc.students.domain.StudentRepository;
import tv.codely.mooc.students.domain.*;
import tv.codely.shared.domain.UuidMother;

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

StudentId id = new StudentId(UuidMother.random());
String name = "name";
String surname = "surname";
String email = "email";
StudentId id = new StudentId(UuidMother.random());
StudentName name = new StudentName("name");
StudentSurname surname = new StudentSurname("surname");
StudentEmail email = new StudentEmail("email@email.com");

RegisterStudentRequest request = new RegisterStudentRequest(id.value(), name, surname, email);
RegisterStudentRequest request = new RegisterStudentRequest(
id.value(), name.value(), surname.value(), email.value()
);

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

Expand Down
47 changes: 47 additions & 0 deletions src/shared/main/tv/codely/shared/domain/EmailValueObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tv.codely.shared.domain;

import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;

import java.util.Objects;

public abstract class EmailValueObject {
private static final EmailValidator emailValidator = new EmailValidator();
Copy link
Author

@OctaviPascual OctaviPascual Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better approach would be to create an EmailValidator interface to not contaminate our domain with this dependency, but I tried to do it and I didn't manage to inject the implementation into this abstract class. Is it possible to do it?

private final String email;

public EmailValueObject(String email) {
ensureValidEmail(email);
this.email = email;
}

public String value() {
return email;
}

private void ensureValidEmail(String value) {
if (!emailValidator.isValid(value, null)) {
throw new InvalidEmail(value);
}
}

@Override
public String toString() {
return this.value();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof EmailValueObject)) {
return false;
}
EmailValueObject that = (EmailValueObject) o;
return Objects.equals(email, that.email);
}

@Override
public int hashCode() {
return Objects.hash(email);
}
}
7 changes: 7 additions & 0 deletions src/shared/main/tv/codely/shared/domain/InvalidEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tv.codely.shared.domain;

public final class InvalidEmail extends DomainError {
public InvalidEmail(String email) {
super("invalid_email", String.format("The email <%s> is invalid", email));
}
}