Skip to content

Feat: create student #91

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

Open
wants to merge 3 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 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.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));
}
}
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 StudentRepository {
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.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(), 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.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);
}

}
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 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());
}
}