FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add register student use case by OctaviPascual · Pull Request #60 · CodelyTV/java-ddd-example · GitHub

/ java-ddd-example Public template
Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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@email.com\"}",
201
);
}
}
11 changes: 11 additions & 0 deletions src/mooc/main/resources/database/mooc.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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());
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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("");
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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() {
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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("");
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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();
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL