Skip to content

Commit 3f4db1d

Browse files
committed
Add register student use case
1 parent 38383e4 commit 3f4db1d

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tv.codely.apps.mooc.backend.controller.students;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PutMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import tv.codely.mooc.students.application.register.StudentRegistrar;
10+
11+
@RestController
12+
public final class StudentsPutController {
13+
14+
private final StudentRegistrar registrar;
15+
16+
public StudentsPutController(StudentRegistrar registrar) {
17+
this.registrar = registrar;
18+
}
19+
20+
@PutMapping(value = "/students/{id}")
21+
public ResponseEntity<String> index(@PathVariable String id, @RequestBody Request request) {
22+
registrar.register(id, request.name(), request.surname(), request.email());
23+
return new ResponseEntity<>(HttpStatus.CREATED);
24+
}
25+
}
26+
27+
final class Request {
28+
private String name;
29+
private String surname;
30+
private String email;
31+
32+
String name() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public String surname() {
41+
return surname;
42+
}
43+
44+
public void setSurname(String surname) {
45+
this.surname = surname;
46+
}
47+
48+
public String email() {
49+
return email;
50+
}
51+
52+
public void setEmail(String email) {
53+
this.email = email;
54+
}
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tv.codely.apps.mooc.backend.controller.students;
2+
3+
import org.junit.jupiter.api.Test;
4+
import tv.codely.apps.mooc.MoocApplicationTestCase;
5+
6+
public final class StudentsPutControllerShould extends MoocApplicationTestCase {
7+
@Test
8+
void register_a_valid_non_existing_student() throws Exception {
9+
assertRequestWithBody(
10+
"PUT",
11+
"/students/1bab45ba-3c7a-4344-8936-78466eca17fa",
12+
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"some-email\"}",
13+
201
14+
);
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tv.codely.mooc.students.application.register;
2+
3+
import tv.codely.mooc.students.domain.*;
4+
import tv.codely.shared.domain.Service;
5+
6+
@Service
7+
public class StudentRegistrar {
8+
private final StudentRepository repository;
9+
10+
public StudentRegistrar(StudentRepository repository) {
11+
this.repository = repository;
12+
}
13+
14+
public void register(String id, String name, String surname, String email) {
15+
Student student = Student.create(new StudentId(id), name, surname, email);
16+
repository.register(student);
17+
}
18+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tv.codely.mooc.students.domain;
22

3+
import java.util.Objects;
4+
35
public final class Student {
46
private final StudentId id;
57
private final String name;
@@ -13,6 +15,10 @@ public Student(StudentId id, String name, String surname, String email) {
1315
this.email = email;
1416
}
1517

18+
public static Student create(StudentId id, String name, String surname, String email) {
19+
return new Student(id, name, surname, email);
20+
}
21+
1622
public StudentId id() {
1723
return id;
1824
}
@@ -28,4 +34,20 @@ public String surname() {
2834
public String email() {
2935
return email;
3036
}
37+
38+
@Override
39+
public boolean equals(Object o) {
40+
if (this == o) return true;
41+
if (o == null || getClass() != o.getClass()) return false;
42+
Student student = (Student) o;
43+
return id.equals(student.id) &&
44+
name.equals(student.name) &&
45+
surname.equals(student.surname) &&
46+
email.equals(student.email);
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return Objects.hash(id, name, surname, email);
52+
}
3153
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import java.util.List;
44

55
public interface StudentRepository {
6+
void register(Student student);
67
List<Student> searchAll();
78
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ public List<Student> searchAll() {
2424
new Student(new StudentId(generator.generate()), "Other name", "Other surname", "another@mail.com")
2525
);
2626
}
27+
28+
@Override
29+
public void register(Student student) {}
2730
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.mooc.students.application.register;
2+
3+
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;
7+
import tv.codely.shared.domain.UuidMother;
8+
9+
import static org.mockito.Mockito.*;
10+
11+
final class StudentRegistrarTestShould {
12+
@Test
13+
void register_a_valid_student() {
14+
StudentRepository repository = mock(StudentRepository.class);
15+
StudentRegistrar registrar = new StudentRegistrar(repository);
16+
17+
StudentId id = new StudentId(UuidMother.random());
18+
String name = "name";
19+
String surname = "surname";
20+
String email = "email";
21+
22+
Student student = new Student(id, name, surname, email);
23+
24+
registrar.register(id.value(), student.name(), student.surname(), student.email());
25+
26+
verify(repository, atLeastOnce()).register(student);
27+
}
28+
}

0 commit comments

Comments
 (0)