Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 app/main/tv/codely/app/mooc/controller/students/Request.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

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

import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.Student仓库;
import tv.codely.shared.domain.Service;

@Service
public class StudentCreator {
private final Student仓库 repository;

public StudentCreator(Student仓库 repository) {
this.repository = repository;
}

public void create(String id, String name, String surname) {
repository.save(new Student(id, name, surname));
}
}
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 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();
}
}
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 Student仓库 {
void save(Student student);

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

import tv.codely.mooc.students.domain.Student;
import tv.codely.mooc.students.domain.Student仓库;
import tv.codely.shared.domain.Service;

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

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

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

@Override
public Optional<Student> search(String id) {
return Optional.ofNullable(students.get(id));
}
}
Original file line number Diff line number Diff line change
@@ -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.Student仓库;

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

@Test
public void create_a_valid_student() {
Student仓库 repository = mock(Student仓库.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);
}

}
Original file line number Diff line number Diff line change
@@ -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 InMemoryStudent仓库Test {

@Test
void save_a_student() {
InMemoryStudent仓库 repository = new InMemoryStudent仓库();
Student student = new Student("id", "name", "surname");

repository.save(student);
}

@Test
void return_an_existing_student() {
InMemoryStudent仓库 repository = new InMemoryStudent仓库();
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() {
InMemoryStudent仓库 repository = new InMemoryStudent仓库();

assertFalse(repository.search("randomId").isPresent());
}
}