-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/nexters/covid/letter/api/MailController.java
This file contains 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,24 @@ | ||
package com.nexters.covid.letter.api; | ||
|
||
import com.nexters.covid.base.BaseResponse; | ||
import com.nexters.covid.letter.service.MailService; | ||
import java.io.UnsupportedEncodingException; | ||
import javax.mail.MessagingException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class MailController { | ||
|
||
private final MailService mailService; | ||
|
||
@GetMapping("/mail/{sendOptionId}") | ||
public BaseResponse<?> sendLetters(@PathVariable("sendOptionId") Long sendOptionId) | ||
throws MessagingException, UnsupportedEncodingException { | ||
mailService.sendLetters(sendOptionId); | ||
return new BaseResponse<>(200, 0, "", null); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
51 changes: 51 additions & 0 deletions
51
src/main/java/com/nexters/covid/letter/service/MailService.java
This file contains 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,51 @@ | ||
package com.nexters.covid.letter.service; | ||
|
||
import com.nexters.covid.letter.domain.Letter; | ||
import com.nexters.covid.letter.domain.LetterRepository; | ||
import com.nexters.covid.letter.domain.State; | ||
import java.io.UnsupportedEncodingException; | ||
import java.util.List; | ||
import javax.mail.MessagingException; | ||
import javax.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class MailService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
private final LetterRepository letterRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<Letter> findLettersByOptionIdAndState(Long sendOptionId) { | ||
return letterRepository.findLettersBySendOptionIdAndState(sendOptionId, State.PENDING); | ||
} | ||
|
||
@Transactional | ||
public void sendLetters(Long sendOptionId) throws MessagingException, UnsupportedEncodingException { | ||
List<Letter> letters = findLettersByOptionIdAndState(sendOptionId); | ||
MimeMessage msg = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "UTF-8"); | ||
|
||
|
||
for (Letter letter : letters) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("<html><body><span style=\"text-align: center;\"><h1 style=\"color:blue\">hello</h1>"); | ||
sb.append("<div>"); | ||
sb.append(letter.decodeContents()); | ||
sb.append("</div></span></body></html>"); | ||
|
||
msgHelper.setFrom(letter.getEmail(), "nana"); | ||
msgHelper.setTo(letter.getLetterTo()); | ||
msgHelper.setSubject(letter.getTitle()); | ||
msgHelper.setText(sb.toString(), true); | ||
|
||
javaMailSender.send(msg); | ||
letterRepository.updateLetterState(letter.getEncryptedId()); | ||
} | ||
} | ||
} |
This file contains 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,12 +1,12 @@ | ||
insert into user (email, identifier, name) values ('email', 'identifier', 'name'); | ||
insert into user (email, identifier, name) values ('test@gmail.com', '1', '김아무개'); | ||
insert into user (email, identifier, name) values ('6230dev@gmail.com', '1', '김아무개'); | ||
|
||
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date) | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , 1, 'PENDING', 'TITLE', 1, '2021-08-11T12:09:59.342635'); | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED', '[email protected]', '[email protected]', 'HAPPY', 1 , 1, 'PENDING', 'TITLE', 1, '2021-08-11T12:09:59.342635'); | ||
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date) | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 2, 1, 'PENDING', 'TITLE1', 1, '2021-08-09T12:09:59.342635'); | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED1', '[email protected]', '[email protected]', 'BLUE', 2, 1, 'PENDING', 'TITLE1', 1, '2021-08-09T12:09:59.342635'); | ||
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date) | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED2', 'LETTER_TO1', 'email', 'BLUE', 8, 2, 'PENDING', 'TITLE1', 1, '2021-08-04T12:09:59.342635'); | ||
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED2', '[email protected]', '[email protected]', 'BLUE', 8, 2, 'PENDING', 'TITLE1', 1, '2021-08-04T12:09:59.342635'); | ||
|
||
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id) | ||
values ('CONTENTS2', 'ENCRYPTED2', 'LETTER_TO2', '[email protected]', 'BLUE', 2, 7, 'PENDING', 'TITLE2', 2); | ||
|