Skip to content

Commit

Permalink
feat: 발송 옵션에 따른 편지 발송 기능 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 20, 2021
1 parent 71ee05d commit be822d6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'commons-codec:commons-codec:1.15'
implementation 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/nexters/covid/letter/api/MailController.java
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);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/nexters/covid/letter/domain/Letter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.covid.letter.domain;

import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.codec.binary.Base64.encodeBase64String;

import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -78,4 +79,8 @@ public void updateLetterState() {
this.state = State.DISPLAYED;
}
}

public String decodeContents() {
return new String(decodeBase64(this.contents));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface LetterRepository extends JpaRepository<Letter, Long> {

Expand All @@ -11,4 +14,10 @@ public interface LetterRepository extends JpaRepository<Letter, Long> {
Optional<Letter> findLetterByEncryptedId(String encryptedId);

List<Letter> findLetterByState(State state);

List<Letter> findLettersBySendOptionIdAndState(Long sendOptionId, State state);

@Modifying
@Query("UPDATE Letter l set l.state = 'SEND' WHERE l.encryptedId = :encryptedId")
int updateLetterState(@Param("encryptedId") String encryptedId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ public LetterResponse updateLetterState(String encryptedId) {
}).orElseThrow(() -> new IllegalArgumentException("해당 ID의 편지가 없습니다."));
return new LetterResponse(letter);
}

}
51 changes: 51 additions & 0 deletions src/main/java/com/nexters/covid/letter/service/MailService.java
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());
}
}
}
8 changes: 4 additions & 4 deletions src/main/resources/data-h2.sql
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);
Expand Down

0 comments on commit be822d6

Please sign in to comment.