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 1 commit
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
Next Next commit
Feat : add StudentCreatedEvent and RegistryMailSender
  • Loading branch information
enrouteeee committed Nov 25, 2021
commit 9a372e7c456304d1024da8afdbf8f10d844b7fe6
37 changes: 37 additions & 0 deletions src/main/java/com/classvar/mail/RegistryMailSender.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

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