From 9a372e7c456304d1024da8afdbf8f10d844b7fe6 Mon Sep 17 00:00:00 2001 From: enrouteeee Date: Wed, 24 Nov 2021 22:25:11 +0900 Subject: [PATCH 1/3] Feat : add StudentCreatedEvent and RegistryMailSender --- .../com/classvar/mail/RegistryMailSender.java | 37 +++++++++++++++++++ .../application/StudentCommandExecutor.java | 14 ++++++- .../student/domain/StudentCreatedEvent.java | 12 ++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/classvar/mail/RegistryMailSender.java create mode 100644 src/main/java/com/classvar/student/domain/StudentCreatedEvent.java diff --git a/src/main/java/com/classvar/mail/RegistryMailSender.java b/src/main/java/com/classvar/mail/RegistryMailSender.java new file mode 100644 index 0000000..6d0c8ab --- /dev/null +++ b/src/main/java/com/classvar/mail/RegistryMailSender.java @@ -0,0 +1,37 @@ +package com.classvar.mail; + +import com.classvar.student.domain.Student; +import com.classvar.student.domain.StudentCreatedEvent; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * RegistryMailSender 는 eventPublisher 에 의해 + * StudentCreatedEvent or ManagerCreatedEvent 가 발행되면 + * 해당 클래스의 TransactionalEventListener 에 의해 각각의 method 가 실행 됨 + */ +@Component +@RequiredArgsConstructor +public class RegistryMailSender { + + private final MailService mailService; + + private final String FROM_ADDRESS = "registry@classvar.com"; + + @TransactionalEventListener + public void sendRegistryMailToStudent(StudentCreatedEvent createdStudentEvent) { + Student student = createdStudentEvent.getStudent(); + + String title = "ClassVar 응시자 등록 메일입니다."; + String registryUrl = "classvar.com/students/registry/" + student.getUuid(); + String message = "안녕하세요 응시자 등록 서비스 입니다.\n" + + "링크에 접속해서 응시자 정보를 등록해주시기 바랍니다.\n" + + registryUrl; + + SimpleMailForm simpleMailForm = new SimpleMailForm(student.getEmail(), FROM_ADDRESS, title, message); + + mailService.sendMail(simpleMailForm); + } +} + diff --git a/src/main/java/com/classvar/student/application/StudentCommandExecutor.java b/src/main/java/com/classvar/student/application/StudentCommandExecutor.java index 387fec7..43e6896 100644 --- a/src/main/java/com/classvar/student/application/StudentCommandExecutor.java +++ b/src/main/java/com/classvar/student/application/StudentCommandExecutor.java @@ -6,23 +6,33 @@ import com.classvar.student.application.dto.request.UpdateStudentInfoDto; import com.classvar.student.application.dto.request.ApproveStudentsDto; import com.classvar.student.domain.Student; +import com.classvar.student.domain.StudentCreatedEvent; import com.classvar.student.domain.StudentRepository; import lombok.RequiredArgsConstructor; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @RequiredArgsConstructor public class StudentCommandExecutor { private final StudentRepository studentRepository; private final StudentMapper studentMapper; + private final ApplicationEventPublisher eventPublisher; @Transactional public void createStudents(CreateStudentsDto dto) { - studentMapper.toStudents(dto).forEach(studentRepository::save); - // createdEvent 발생 -> 학생에게 등록하는 url 포함된 email 전송 + List students = studentMapper.toStudents(dto); + for (Student student : students) { + studentRepository.save(student); + + // createdEvent 발생 -> 학생에게 등록하는 url 포함된 email 전송 + eventPublisher.publishEvent(new StudentCreatedEvent(student)); + } } @Transactional diff --git a/src/main/java/com/classvar/student/domain/StudentCreatedEvent.java b/src/main/java/com/classvar/student/domain/StudentCreatedEvent.java new file mode 100644 index 0000000..bebd60d --- /dev/null +++ b/src/main/java/com/classvar/student/domain/StudentCreatedEvent.java @@ -0,0 +1,12 @@ +package com.classvar.student.domain; + +import lombok.Getter; + +@Getter +public class StudentCreatedEvent { + private final Student student; + + public StudentCreatedEvent(Student student) { + this.student = student; + } +} \ No newline at end of file From f2ea0aa12bb89e289196ce070cd8fd6130b1396f Mon Sep 17 00:00:00 2001 From: enrouteeee Date: Thu, 25 Nov 2021 22:16:37 +0900 Subject: [PATCH 2/3] feat : add ManagerCreatedEvenet --- .../application/ManagerCommandExecutor.java | 14 ++++++++++++-- .../manager/domain/ManagerCreatedEvent.java | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/classvar/manager/domain/ManagerCreatedEvent.java diff --git a/src/main/java/com/classvar/manager/application/ManagerCommandExecutor.java b/src/main/java/com/classvar/manager/application/ManagerCommandExecutor.java index 0658559..b6a28b2 100644 --- a/src/main/java/com/classvar/manager/application/ManagerCommandExecutor.java +++ b/src/main/java/com/classvar/manager/application/ManagerCommandExecutor.java @@ -6,21 +6,31 @@ import com.classvar.manager.application.dto.request.DeleteManagerDto; import com.classvar.manager.application.dto.request.UpdateManagerInfoDto; import com.classvar.manager.domain.Manager; +import com.classvar.manager.domain.ManagerCreatedEvent; import com.classvar.manager.domain.ManagerRepository; import lombok.RequiredArgsConstructor; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @RequiredArgsConstructor public class ManagerCommandExecutor { private final ManagerRepository managerRepository; private final ManagerMapper managerMapper; + private final ApplicationEventPublisher eventPublisher; @Transactional - public void createManager(CreateManagerDto dto) { - managerMapper.toManagers(dto).forEach(managerRepository::save); + public void createManager(CreateManagerDto dto){ + List managers = managerMapper.toManagers(dto); + for (Manager manager : managers) { + managerRepository.save(manager); + + eventPublisher.publishEvent(new ManagerCreatedEvent(manager)); + } } public void updateManager(String uuid, UpdateManagerInfoDto dto) { diff --git a/src/main/java/com/classvar/manager/domain/ManagerCreatedEvent.java b/src/main/java/com/classvar/manager/domain/ManagerCreatedEvent.java new file mode 100644 index 0000000..17cba8a --- /dev/null +++ b/src/main/java/com/classvar/manager/domain/ManagerCreatedEvent.java @@ -0,0 +1,12 @@ +package com.classvar.manager.domain; + +import lombok.Getter; + +@Getter +public class ManagerCreatedEvent { + private final Manager manager; + + public ManagerCreatedEvent(Manager manager) { + this.manager= manager; + } +} From f419cd8bfbec37725dc51d36c07fde1f8113fdff Mon Sep 17 00:00:00 2001 From: enrouteeee Date: Thu, 25 Nov 2021 22:17:23 +0900 Subject: [PATCH 3/3] feat : add sendRegistryMailToMangager on RegistryMailSender --- .../com/classvar/mail/RegistryMailSender.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/classvar/mail/RegistryMailSender.java b/src/main/java/com/classvar/mail/RegistryMailSender.java index 6d0c8ab..0c72dfb 100644 --- a/src/main/java/com/classvar/mail/RegistryMailSender.java +++ b/src/main/java/com/classvar/mail/RegistryMailSender.java @@ -1,5 +1,7 @@ package com.classvar.mail; +import com.classvar.manager.domain.Manager; +import com.classvar.manager.domain.ManagerCreatedEvent; import com.classvar.student.domain.Student; import com.classvar.student.domain.StudentCreatedEvent; import lombok.RequiredArgsConstructor; @@ -20,11 +22,11 @@ public class RegistryMailSender { private final String FROM_ADDRESS = "registry@classvar.com"; @TransactionalEventListener - public void sendRegistryMailToStudent(StudentCreatedEvent createdStudentEvent) { - Student student = createdStudentEvent.getStudent(); + public void sendRegistryMailToStudent(StudentCreatedEvent studentCreatedEvent) { + Student student = studentCreatedEvent.getStudent(); String title = "ClassVar 응시자 등록 메일입니다."; - String registryUrl = "classvar.com/students/registry/" + student.getUuid(); + String registryUrl = "http://classvar.com/students/registry/" + student.getUuid(); String message = "안녕하세요 응시자 등록 서비스 입니다.\n" + "링크에 접속해서 응시자 정보를 등록해주시기 바랍니다.\n" + registryUrl; @@ -33,5 +35,20 @@ public void sendRegistryMailToStudent(StudentCreatedEvent createdStudentEvent) { mailService.sendMail(simpleMailForm); } + + @TransactionalEventListener + public void sendRegistryMailToManager(ManagerCreatedEvent managerCreatedEvent) { + Manager manager = managerCreatedEvent.getManager(); + + String title = "ClassVar 감독관 등록 메일입니다."; + String registryUrl = "http://classvar.com/managers/registry/" + manager.getUuid(); + String message = "안녕하세요 감독관 등록 서비스 입니다.\n" + + "링크에 접속해서 감독관 정보를 등록해주시기 바랍니다.\n" + + registryUrl; + + SimpleMailForm simpleMailForm = new SimpleMailForm(manager.getEmail(), FROM_ADDRESS, title, message); + + mailService.sendMail(simpleMailForm); + } }