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..0c72dfb --- /dev/null +++ b/src/main/java/com/classvar/mail/RegistryMailSender.java @@ -0,0 +1,54 @@ +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; +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 studentCreatedEvent) { + Student student = studentCreatedEvent.getStudent(); + + String title = "ClassVar 응시자 등록 메일입니다."; + String registryUrl = "http://classvar.com/students/registry/" + student.getUuid(); + String message = "안녕하세요 응시자 등록 서비스 입니다.\n" + + "링크에 접속해서 응시자 정보를 등록해주시기 바랍니다.\n" + + registryUrl; + + SimpleMailForm simpleMailForm = new SimpleMailForm(student.getEmail(), FROM_ADDRESS, title, message); + + 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); + } +} + 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; + } +} 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