diff --git a/app/main/tv/codely/app/mooc/controller/students/Request.java b/app/main/tv/codely/app/mooc/controller/students/Request.java new file mode 100644 index 00000000..5677ce24 --- /dev/null +++ b/app/main/tv/codely/app/mooc/controller/students/Request.java @@ -0,0 +1,22 @@ +package tv.codely.app.mooc.controller.students; + +public class Request { + private String name; + private String surname; + + public String name() { + return name; + } + + public String surname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java b/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java new file mode 100644 index 00000000..979d46bb --- /dev/null +++ b/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java @@ -0,0 +1,26 @@ +package tv.codely.app.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; + +@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) { + creator.create(id, request.name(), request.surname()); + + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git a/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java b/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java new file mode 100644 index 00000000..9f5a627b --- /dev/null +++ b/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java @@ -0,0 +1,17 @@ +package tv.codely.app.mooc.controller.students; + +import org.junit.jupiter.api.Test; +import tv.codely.app.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\", \"lastname\": \"The best lastname\"}", + 201); + } + +} diff --git a/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java new file mode 100644 index 00000000..76f9f5d4 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java @@ -0,0 +1,18 @@ +package tv.codely.mooc.students.application.create; + +import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentRepository; +import tv.codely.shared.domain.Service; + +@Service +public class StudentCreator { + private final StudentRepository repository; + + public StudentCreator(StudentRepository repository) { + this.repository = repository; + } + + public void create(String id, String name, String surname) { + repository.save(new Student(id, name, surname)); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/Student.java b/src/mooc/main/tv/codely/mooc/students/domain/Student.java new file mode 100644 index 00000000..d42c355c --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/Student.java @@ -0,0 +1,40 @@ +package tv.codely.mooc.students.domain; + +public class Student { + private String id; + private String name; + private String surname; + + public Student(String id, String name, String surname) { + this.id = id; + this.name = name; + this.surname = surname; + } + + public String id() { + return id; + } + + public String name() { + return name; + } + + public String surname() { + return surname; + } + + @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(); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java new file mode 100644 index 00000000..2a6c36ec --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java @@ -0,0 +1,9 @@ +package tv.codely.mooc.students.domain; + +import java.util.Optional; + +public interface StudentRepository { + void save(Student student); + + Optional search(String id); +} diff --git a/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java new file mode 100644 index 00000000..56905d04 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java @@ -0,0 +1,22 @@ +package tv.codely.mooc.students.infrastructure.persistence; + +import tv.codely.mooc.students.domain.Student; +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 students = new HashMap<>(); + + public void save(Student student) { + students.put(student.id(), student); + } + + @Override + public Optional search(String id) { + return Optional.ofNullable(students.get(id)); + } +} diff --git a/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java new file mode 100644 index 00000000..d41e0e44 --- /dev/null +++ b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java @@ -0,0 +1,26 @@ +package tv.codely.mooc.students.application.create; + +import org.junit.jupiter.api.Test; +import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentRepository; + +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); + + String id = "some-id"; + String name = "name"; + String surname = "surname"; + + Student student = new Student(id, name, surname); + + creator.create(student.id(), student.name(), student.surname()); + + verify(repository, atLeastOnce()).save(student); + } + +} diff --git a/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java new file mode 100644 index 00000000..b01d3fc0 --- /dev/null +++ b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java @@ -0,0 +1,36 @@ +package tv.codely.mooc.students.infrastructure.persistence; + +import org.junit.jupiter.api.Test; +import tv.codely.mooc.students.domain.Student; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class InMemoryStudentRepositoryTest { + + @Test + void save_a_student() { + InMemoryStudentRepository repository = new InMemoryStudentRepository(); + Student student = new Student("id", "name", "surname"); + + repository.save(student); + } + + @Test + void return_an_existing_student() { + InMemoryStudentRepository repository = new InMemoryStudentRepository(); + Student student = new Student("id", "name", "surname"); + + 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("randomId").isPresent()); + } +}