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

Refactor students creator #92

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/main/tv/codely/apps/mooc/controller/students/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tv.codely.apps.mooc.controller.students;

public class Request {
private String name;
private String email;

public String name() {
return name;
}

public String email() {
return email;
}

public void setEmail(String surname) {
this.email = surname;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tv.codely.apps.mooc.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.create.StudentCreator;
import tv.codely.mooc.students.application.create.StudentCreatorRequest;

@RestController
public class StudentsPutController {

private final StudentCreator creator;

public StudentsPutController(StudentCreator creator) {
this.creator = creator;
}

@PutMapping("/students/{id}")
public ResponseEntity<?> create(@PathVariable String id, @RequestBody Request request) {
StudentCreatorRequest studentCreatorRequest = new StudentCreatorRequest(id, request.name(), request.email());

creator.create(studentCreatorRequest);

return new ResponseEntity<>(HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tv.codely.apps.mooc.controller.students;

import org.junit.jupiter.api.Test;
import tv.codely.apps.mooc.controller.RequestTestCase;

public class StudentsPutControllerShould extends RequestTestCase {

@Test
public void create_a_valid_non_existing_student() throws Exception {
assertRequestWithBody(
"PUT",
"/students/1aab45ba-3c7a-4344-8936-78466eca77fa",
"{\"name\": \"The best student\", \"email\": \"[email protected]\"}",
201);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tv.codely.mooc.students.application.create;

import tv.codely.mooc.courses.domain.Course;
import tv.codely.mooc.students.domain.*;
import tv.codely.shared.domain.Service;

@Service
public class StudentCreator {
private final StudentRepository repository;

public StudentCreator(StudentRepository repository) {
this.repository = repository;
}

public void create(StudentCreatorRequest request) {
StudentId id = new StudentId(request.id());
StudentName name = new StudentName(request.name());
StudentEmail email = new StudentEmail(request.email());

repository.save(new Student(id, name, email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tv.codely.mooc.students.application.create;

public class StudentCreatorRequest {
private final String id;
private final String name;
private final String email;

public StudentCreatorRequest(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

public String id() {
return id;
}

public String name() {
return name;
}

public String email() {
return email;
}
}
40 changes: 40 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tv.codely.mooc.students.domain;

public class Student {
private StudentId id;
private StudentName name;
private StudentEmail email;

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

public StudentId id() {
return id;
}

public StudentName name() {
return name;
}

public StudentEmail email() {
return email;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;

Student student = (Student) o;

return id.equals(student.id);
}

@Override
public int hashCode() {
return id.hashCode();
}
}
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 class StudentEmail extends EmailValueObject {
public StudentEmail(String value) {
super(value);
}
}
9 changes: 9 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/StudentId.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.Identifier;

public class StudentId extends Identifier {
public StudentId(String value) {
super(value);
}
}
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 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 java.util.Optional;

public interface StudentRepository {
void save(Student student);

Optional<Student> search(StudentId id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tv.codely.mooc.students.infrastructure.persistence;

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

import java.util.HashMap;
import java.util.Optional;

@Service
public class InMemoryStudentRepository implements StudentRepository {
private final HashMap<String, Student> students = new HashMap<>();

public void save(Student student) {
students.put(student.id().value(), student);
}

@Override
public Optional<Student> search(StudentId id) {
return Optional.ofNullable(students.get(id.value()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tv.codely.mooc.students.application.create;

import org.junit.jupiter.api.Test;
import tv.codely.mooc.students.domain.*;

import static org.mockito.Mockito.*;
public class StudentCreatorTest {

@Test
public void create_a_valid_student() {
StudentRepository repository = mock(StudentRepository.class);
StudentCreator creator = new StudentCreator(repository);
StudentCreatorRequest creatorRequest = new StudentCreatorRequest(
"decf33ca-81a7-419f-a07a-74f214e928e5",
"name",
"[email protected]");

Student student = new Student(
new StudentId(creatorRequest.id()),
new StudentName(creatorRequest.name()),
new StudentEmail(creatorRequest.email())
);

creator.create(creatorRequest);

verify(repository, atLeastOnce()).save(student);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tv.codely.mooc.students.infrastructure.persistence;

import org.junit.jupiter.api.Test;
import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.StudentEmail;
import tv.codely.mooc.students.domain.StudentId;
import tv.codely.mooc.students.domain.StudentName;

import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

class InMemoryStudentRepositoryTest {

@Test
void save_a_student() {
InMemoryStudentRepository repository = new InMemoryStudentRepository();
Student student = new Student(
new StudentId(UUID.randomUUID().toString()),
new StudentName("name"),
new StudentEmail("[email protected]")
);

repository.save(student);
}

@Test
void return_an_existing_student() {
InMemoryStudentRepository repository = new InMemoryStudentRepository();
Student student = new Student(
new StudentId(UUID.randomUUID().toString()),
new StudentName("name"),
new StudentEmail("[email protected]"));

repository.save(student);

assertEquals(Optional.of(student), repository.search(student.id()));
}

@Test
void not_return_a_non_existing_student() {
InMemoryStudentRepository repository = new InMemoryStudentRepository();

assertFalse(repository.search(new StudentId(
UUID.randomUUID().toString()
)).isPresent());
}
}
20 changes: 20 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,20 @@
package tv.codely.shared.domain;

public abstract class EmailValueObject {
private final String value;

public EmailValueObject(String value) {
ensureIsValidEmail(value);
this.value = value;
}

private void ensureIsValidEmail(String value) {
if (!value.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$")) {
throw new IllegalArgumentException(String.format("The email <%s> is not valid", value));
}
}

public String value() {
return value;
}
}