Skip to content

Commit

Permalink
Migrate register Student use case to command
Browse files Browse the repository at this point in the history
  • Loading branch information
OctaviPascual committed Nov 14, 2020
1 parent 6f3da18 commit 34b9ecf
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@
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.RegisterStudentRequest;
import tv.codely.mooc.students.application.register.StudentRegistrar;
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;

@RestController
public final class StudentsPutController {
import java.util.HashMap;

private final StudentRegistrar registrar;
@RestController
public final class StudentsPutController extends ApiController {

public StudentsPutController(StudentRegistrar registrar) {
this.registrar = registrar;
public StudentsPutController(
QueryBus queryBus,
CommandBus commandBus
) {
super(queryBus, commandBus);
}

@PutMapping(value = "/students/{id}")
public ResponseEntity<String> index(@PathVariable String id, @RequestBody Request request) {
registrar.register(new RegisterStudentRequest(id, request.name(), request.surname(), request.email()));
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tv.codely.mooc.students.application.register;

public final class RegisterStudentRequest {
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 RegisterStudentRequest(String id, String name, String surname, String email) {
public RegisterStudentCommand(String id, String name, String surname, String email) {
this.id = id;
this.name = name;
this.surname = surname;
Expand Down
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
Expand Up @@ -14,13 +14,14 @@ public StudentRegistrar(StudentRepository repository, EventBus eventBus) {
this.eventBus = eventBus;
}

public void register(RegisterStudentRequest request) {
Student student = Student.create(
new StudentId(request.id()),
new StudentName(request.name()),
new StudentSurname(request.surname()),
new StudentEmail(request.email())
);
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
import tv.codely.mooc.students.domain.StudentRegisteredDomainEventMother;
import tv.codely.shared.domain.student.StudentRegisteredDomainEvent;

final class StudentRegistrarTestShould extends StudentsModuleUnitTestCase {
StudentRegistrar registrar;
final class RegisterStudentCommandHandlerShould extends StudentsModuleUnitTestCase {
private RegisterStudentCommandHandler handler;

@BeforeEach
protected void setUp() {
super.setUp();

registrar = new StudentRegistrar(repository, eventBus);
handler = new RegisterStudentCommandHandler(new StudentRegistrar(repository, eventBus));
}

@Test
void register_a_valid_student() {
RegisterStudentRequest request = RegisterStudentRequestMother.random();
Student student = StudentMother.fromRequest(request);
RegisterStudentCommand command = RegisterStudentCommandMother.random();
Student student = StudentMother.fromCommand(command);
StudentRegisteredDomainEvent domainEvent = StudentRegisteredDomainEventMother.fromStudent(student);

registrar.register(request);
handler.handle(command);

shouldHaveSaved(student);
shouldHavePublished(domainEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import tv.codely.mooc.students.domain.*;

public class RegisterStudentRequestMother {
public static RegisterStudentRequest create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
return new RegisterStudentRequest(id.value(), name.value(), surname.value(), email.value());
public class RegisterStudentCommandMother {
public static RegisterStudentCommand create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
return new RegisterStudentCommand(id.value(), name.value(), surname.value(), email.value());
}

public static RegisterStudentRequest random() {
public static RegisterStudentCommand random() {
return create(
StudentIdMother.random(),
StudentNameMother.random(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package tv.codely.mooc.students.domain;

import tv.codely.mooc.students.application.register.RegisterStudentRequest;
import tv.codely.mooc.students.application.register.RegisterStudentCommand;

public class StudentMother {
public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
return new Student(id, name, surname, email);
}

public static Student fromRequest(RegisterStudentRequest request) {
public static Student fromCommand(RegisterStudentCommand request) {
return create(
StudentIdMother.create(request.id()),
StudentNameMother.create(request.name()),
Expand Down

0 comments on commit 34b9ecf

Please sign in to comment.