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

Add register student use case #60

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tv.codely.apps.mooc.backend.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.register.RegisterStudentCommand;
import tv.codely.shared.domain.DomainError;
import tv.codely.shared.domain.bus.command.CommandBus;
import tv.codely.shared.domain.bus.query.QueryBus;
import tv.codely.shared.infrastructure.spring.ApiController;

import java.util.HashMap;

@RestController
public final class StudentsPutController extends ApiController {

public StudentsPutController(
QueryBus queryBus,
CommandBus commandBus
) {
super(queryBus, commandBus);
}

@PutMapping(value = "/students/{id}")
public ResponseEntity<String> index(@PathVariable String id, @RequestBody Request request) {
dispatch(new RegisterStudentCommand(id, request.name(), request.surname(), request.email()));
return new ResponseEntity<>(HttpStatus.CREATED);
}

@Override
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
return null;
}
}

final class Request {
private String name;
private String surname;
private String email;

String name() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String surname() {
return surname;
}

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

public String email() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tv.codely.apps.mooc.backend.controller.students;

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

public final class StudentsPutControllerShould extends MoocApplicationTestCase {
@Test
void register_a_valid_non_existing_student() throws Exception {
assertRequestWithBody(
"PUT",
"/students/1bab45ba-3c7a-4344-8936-78466eca17fa",
"{\"name\": \"some-name\", \"surname\": \"some-surname\", \"email\": \"[email protected]\"}",
201
);
}
}
11 changes: 11 additions & 0 deletions src/mooc/main/resources/database/mooc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ CREATE TABLE IF NOT EXISTS domain_events (
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS students (
id CHAR(36) NOT NULL,
name VARCHAR(255) NOT NULL,
surname VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public StudentResponse(String id, String name, String surname, String email) {
}

public static StudentResponse fromAggregate(Student student) {
return new StudentResponse(student.id().value(), student.name(), student.surname(), student.email());
return new StudentResponse(
student.id().value(),
student.name().value(),
student.surname().value(),
student.email().value()
);
}

public String id() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tv.codely.mooc.students.application.register;

import tv.codely.shared.domain.bus.command.Command;

public final class RegisterStudentCommand implements Command {
private final String id;
private final String name;
private final String surname;
private final String email;

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

public String id() {
return id;
}

public String name() {
return name;
}

public String surname() {
return surname;
}

public String email() {
return email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tv.codely.mooc.students.application.register;

import tv.codely.mooc.students.domain.StudentEmail;
import tv.codely.mooc.students.domain.StudentId;
import tv.codely.mooc.students.domain.StudentName;
import tv.codely.mooc.students.domain.StudentSurname;
import tv.codely.shared.domain.Service;
import tv.codely.shared.domain.bus.command.CommandHandler;

@Service
public final class RegisterStudentCommandHandler implements CommandHandler<RegisterStudentCommand> {
private final StudentRegistrar registrar;

public RegisterStudentCommandHandler(StudentRegistrar registrar) {
this.registrar = registrar;
}

@Override
public void handle(RegisterStudentCommand command) {
StudentId id = new StudentId(command.id());
StudentName name = new StudentName(command.name());
StudentSurname surname = new StudentSurname(command.surname());
StudentEmail email = new StudentEmail(command.email());

registrar.register(id, name, surname, email);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tv.codely.mooc.students.application.register;

import tv.codely.mooc.students.domain.*;
import tv.codely.shared.domain.Service;
import tv.codely.shared.domain.bus.event.EventBus;

@Service
public class StudentRegistrar {
private final StudentRepository repository;
private final EventBus eventBus;

public StudentRegistrar(StudentRepository repository, EventBus eventBus) {
this.repository = repository;
this.eventBus = eventBus;
}

public void register(
StudentId id,
StudentName name,
StudentSurname surname,
StudentEmail email
) {
Student student = Student.create(id, name, surname, email);

repository.register(student);
eventBus.publish(student.pullDomainEvents());
}
}
54 changes: 45 additions & 9 deletions src/mooc/main/tv/codely/mooc/students/domain/Student.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
package tv.codely.mooc.students.domain;

public final class Student {
private final StudentId id;
private final String name;
private final String surname;
private final String email;
import tv.codely.shared.domain.AggregateRoot;
import tv.codely.shared.domain.student.StudentRegisteredDomainEvent;

public Student(StudentId id, String name, String surname, String email) {
import java.util.Objects;

public final class Student extends AggregateRoot {
private final StudentId id;
private final StudentName name;
private final StudentSurname surname;
private final StudentEmail email;

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

private Student() {
id = null;
name = null;
surname = null;
email = null;
}

public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
Student student = new Student(id, name, surname, email);

student.record(new StudentRegisteredDomainEvent(id.value(), name.value(), surname.value(), email.value()));

return student;
}

public StudentId id() {
return id;
}

public String name() {
public StudentName name() {
return name;
}

public String surname() {
public StudentSurname surname() {
return surname;
}

public String email() {
public StudentEmail email() {
return email;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id.equals(student.id) &&
name.equals(student.name) &&
surname.equals(student.surname) &&
email.equals(student.email);
}

@Override
public int hashCode() {
return Objects.hash(id, name, surname, email);
}
}
13 changes: 13 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.EmailValueObject;

public final class StudentEmail extends EmailValueObject {
public StudentEmail(String email) {
super(email);
}

private StudentEmail() {
super("");
}
}
3 changes: 3 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/StudentId.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public final class StudentId extends Identifier {
public StudentId(String value) {
super(value);
}

private StudentId() {
}
}
13 changes: 13 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,13 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.StringValueObject;

public final class StudentName extends StringValueObject {
public StudentName(String value) {
super(value);
}

private StudentName() {
super("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import java.util.List;

public interface StudentRepository {
void register(Student student);
List<Student> searchAll();
}
13 changes: 13 additions & 0 deletions src/mooc/main/tv/codely/mooc/students/domain/StudentSurname.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tv.codely.mooc.students.domain;

import tv.codely.shared.domain.StringValueObject;

public final class StudentSurname extends StringValueObject {
public StudentSurname(String value) {
super(value);
}

private StudentSurname() {
super("");
}
}

This file was deleted.

Loading