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

Feature 69 add student or manager created event #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions src/main/java/com/classvar/mail/RegistryMailSender.java
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";

@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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<Manager> managers = managerMapper.toManagers(dto);
for (Manager manager : managers) {
managerRepository.save(manager);

eventPublisher.publishEvent(new ManagerCreatedEvent(manager));
}
}

public void updateManager(String uuid, UpdateManagerInfoDto dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Student> students = studentMapper.toStudents(dto);
for (Student student : students) {
studentRepository.save(student);

// createdEvent 발생 -> 학생에게 등록하는 url 포함된 email 전송
eventPublisher.publishEvent(new StudentCreatedEvent(student));
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}