generated from CodelyTV/java-basic-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 206
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
OctaviPascual
wants to merge
9
commits into
CodelyTV:main
Choose a base branch
from
OctaviPascual:register-student
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f4db1d
Add register student use case
OctaviPascual 4e9c42b
Use RegisterStudentRequest in application service
OctaviPascual b1582b2
Use Value Objects for Student
OctaviPascual aab00a0
Use ObjectMother for Student fields
OctaviPascual b3c1984
Add semantics to StudentRegistrar test
OctaviPascual ef2f366
Add hibernate to Student
OctaviPascual 586c0d2
Add Student repository integration test
OctaviPascual 5bd2db5
Raise event on Student registered
OctaviPascual 2249b13
Migrate register Student use case to command
OctaviPascual File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
apps/main/tv/codely/apps/mooc/backend/controller/students/StudentsPutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package tv.codely.apps.mooc.backend.controller.students; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import tv.codely.mooc.students.application.register.RegisterStudentRequest; | ||
import tv.codely.mooc.students.application.register.StudentRegistrar; | ||
|
||
@RestController | ||
public final class StudentsPutController { | ||
|
||
private final StudentRegistrar registrar; | ||
|
||
public StudentsPutController(StudentRegistrar registrar) { | ||
this.registrar = registrar; | ||
} | ||
|
||
@PutMapping(value = "/students/{id}") | ||
public ResponseEntity<String> index(@PathVariable String id, @RequestBody Request request) { | ||
registrar.register(new RegisterStudentRequest(id, request.name(), request.surname(), request.email())); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
} | ||
} | ||
|
||
final class Request { | ||
private String name; | ||
private String surname; | ||
private String email; | ||
|
||
String name() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String surname() { | ||
return surname; | ||
} | ||
|
||
public void setSurname(String surname) { | ||
this.surname = surname; | ||
} | ||
|
||
public String email() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/test/tv/codely/apps/mooc/backend/controller/students/StudentsPutControllerShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tv.codely.apps.mooc.backend.controller.students; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tv.codely.apps.mooc.MoocApplicationTestCase; | ||
|
||
public final class StudentsPutControllerShould extends MoocApplicationTestCase { | ||
@Test | ||
void register_a_valid_non_existing_student() throws Exception { | ||
assertRequestWithBody( | ||
"PUT", | ||
"/students/1bab45ba-3c7a-4344-8936-78466eca17fa", | ||
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"[email protected]\"}", | ||
201 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/mooc/main/tv/codely/mooc/students/application/register/RegisterStudentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tv.codely.mooc.students.application.register; | ||
|
||
public final class RegisterStudentRequest { | ||
private final String id; | ||
private final String name; | ||
private final String surname; | ||
private final String email; | ||
|
||
public RegisterStudentRequest(String id, String name, String surname, String email) { | ||
this.id = id; | ||
this.name = name; | ||
this.surname = surname; | ||
this.email = email; | ||
} | ||
|
||
public String id() { | ||
return id; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public String surname() { | ||
return surname; | ||
} | ||
|
||
public String email() { | ||
return email; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/mooc/main/tv/codely/mooc/students/application/register/StudentRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tv.codely.mooc.students.application.register; | ||
|
||
import tv.codely.mooc.students.domain.*; | ||
import tv.codely.shared.domain.Service; | ||
|
||
@Service | ||
public class StudentRegistrar { | ||
private final StudentRepository repository; | ||
|
||
public StudentRegistrar(StudentRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public void register(RegisterStudentRequest request) { | ||
Student student = Student.create( | ||
new StudentId(request.id()), | ||
new StudentName(request.name()), | ||
new StudentSurname(request.surname()), | ||
new StudentEmail(request.email()) | ||
); | ||
repository.register(student); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
package tv.codely.mooc.students.domain; | ||
|
||
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, 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; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Student student = (Student) o; | ||
return id.equals(student.id) && | ||
name.equals(student.name) && | ||
surname.equals(student.surname) && | ||
email.equals(student.email); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, name, surname, email); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
src/mooc/main/tv/codely/mooc/students/domain/StudentName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/mooc/main/tv/codely/mooc/students/domain/StudentSurname.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -20,8 +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) { | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/mooc/test/tv/codely/mooc/students/application/register/StudentRegistrarTestShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tv.codely.mooc.students.application.register; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tv.codely.mooc.students.domain.*; | ||
import tv.codely.shared.domain.UuidMother; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
final class StudentRegistrarTestShould { | ||
@Test | ||
void register_a_valid_student() { | ||
StudentRepository repository = mock(StudentRepository.class); | ||
StudentRegistrar registrar = new StudentRegistrar(repository); | ||
|
||
StudentId id = new StudentId(UuidMother.random()); | ||
StudentName name = new StudentName("name"); | ||
StudentSurname surname = new StudentSurname("surname"); | ||
StudentEmail email = new StudentEmail("[email protected]"); | ||
|
||
RegisterStudentRequest request = new RegisterStudentRequest( | ||
id.value(), name.value(), surname.value(), email.value() | ||
); | ||
|
||
Student student = new Student(id, name, surname, email); | ||
|
||
registrar.register(request); | ||
|
||
verify(repository, atLeastOnce()).register(student); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/shared/main/tv/codely/shared/domain/EmailValueObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?