rest controller practice : send mail - mail template(board_reply)

This commit is contained in:
haerong22
2021-03-22 00:01:07 +09:00
parent 4e9f8127c9
commit 6863329313
12 changed files with 4200 additions and 29 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,13 @@
package com.example.restcontroller.board.controller; package com.example.restcontroller.board.controller;
import com.example.restcontroller.board.entity.BoardReport; import com.example.restcontroller.board.entity.BoardReport;
import com.example.restcontroller.board.model.BoardReplyInput;
import com.example.restcontroller.board.model.ServiceResult;
import com.example.restcontroller.board.service.BoardService; import com.example.restcontroller.board.service.BoardService;
import com.example.restcontroller.common.model.ResponseResult; import com.example.restcontroller.common.model.ResponseResult;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@@ -20,4 +21,12 @@ public class ApiAdminBoardController {
List<BoardReport> list = boardService.boardReportList(); List<BoardReport> list = boardService.boardReportList();
return ResponseResult.success(list); return ResponseResult.success(list);
} }
@PostMapping("/api/admin/board/{id}/reply")
public ResponseEntity<?> chapter5_5(@PathVariable Long id,
@RequestBody BoardReplyInput boardReplyInput) {
ServiceResult result = boardService.replyBoard(id, boardReplyInput);
return ResponseResult.result(result);
}
} }

View File

@@ -23,6 +23,21 @@ public class Board {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private String title;
@Lob
private String content;
private boolean topYn;
@Lob
private String replyContents;
private LocalDateTime regDate;
private LocalDate publishStartDate;
private LocalDate publishEndDate;
@ManyToOne @ManyToOne
@JoinColumn @JoinColumn
private User user; private User user;
@@ -43,17 +58,6 @@ public class Board {
@OneToMany(mappedBy = "board") @OneToMany(mappedBy = "board")
List<BoardComment> boardCommentList = new ArrayList<>(); List<BoardComment> boardCommentList = new ArrayList<>();
private String title;
private String content;
private boolean topYn;
private LocalDateTime regDate;
private LocalDate publishStartDate;
private LocalDate publishEndDate;
@Override @Override
public String toString() { public String toString() {
return "Board{" + return "Board{" +

View File

@@ -0,0 +1,7 @@
package com.example.restcontroller.board.exception;
public class BoardNotFoundException extends RuntimeException {
public BoardNotFoundException(String s) {
super(s);
}
}

View File

@@ -0,0 +1,15 @@
package com.example.restcontroller.board.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BoardReplyInput {
private String replyContents;
}

View File

@@ -53,4 +53,6 @@ public interface BoardService {
List<Board> list(); List<Board> list();
ServiceResult add(String email, BoardInput boardInput); ServiceResult add(String email, BoardInput boardInput);
ServiceResult replyBoard(Long id, BoardReplyInput boardReplyInput);
} }

View File

@@ -1,6 +1,7 @@
package com.example.restcontroller.board.service; package com.example.restcontroller.board.service;
import com.example.restcontroller.board.entity.*; import com.example.restcontroller.board.entity.*;
import com.example.restcontroller.board.exception.BoardNotFoundException;
import com.example.restcontroller.board.exception.BoardTypeNotFoundException; import com.example.restcontroller.board.exception.BoardTypeNotFoundException;
import com.example.restcontroller.board.model.*; import com.example.restcontroller.board.model.*;
import com.example.restcontroller.board.repository.*; import com.example.restcontroller.board.repository.*;
@@ -424,4 +425,29 @@ public class BoardServiceImpl implements BoardService {
return ServiceResult.success(); return ServiceResult.success();
} }
@Transactional
@Override
public ServiceResult replyBoard(Long id, BoardReplyInput boardReplyInput) {
Board boardEntity = boardRepository.findById(id).
orElseThrow(() -> new BoardNotFoundException("게시글이 존재하지 않습니다."));
boardEntity.setReplyContents(boardReplyInput.getReplyContents());
// 메일전송
Optional<MailTemplate> optionalMailTemplate = mailTemplateRepository.findByTemplateId("BOARD_REPLY");
optionalMailTemplate.ifPresent((e) -> {
String fromEmail = e.getSendEmail();
String fromUserName = e.getSendUserName();
String title = e.getTitle().replaceAll("\\{USER_NAME\\}", boardEntity.getUser().getUserName());
String contents = e.getContents().replaceAll("\\{BOARD_TITLE\\}", boardEntity.getTitle())
.replaceAll("\\{BOARD_CONTENTS\\}", boardEntity.getContent())
.replaceAll("\\{BOARD_REPLY_CONTENTS\\}", boardEntity.getReplyContents());
mailComponent.send(fromEmail, fromUserName,
boardEntity.getUser().getEmail(), boardEntity.getUser().getUserName(), title, contents);
});
return ServiceResult.success();
}
} }

View File

@@ -1,5 +1,6 @@
package com.example.restcontroller.common; package com.example.restcontroller.common;
import com.example.restcontroller.board.exception.BoardNotFoundException;
import com.example.restcontroller.board.exception.BoardTypeNotFoundException; import com.example.restcontroller.board.exception.BoardTypeNotFoundException;
import com.example.restcontroller.common.exception.AuthFailException; import com.example.restcontroller.common.exception.AuthFailException;
import com.example.restcontroller.common.exception.BizException; import com.example.restcontroller.common.exception.BizException;
@@ -29,7 +30,8 @@ public class GlobalExceptionHandler {
PasswordNotMatchException.class, PasswordNotMatchException.class,
BoardTypeNotFoundException.class, BoardTypeNotFoundException.class,
BizException.class, BizException.class,
AuthFailException.class}) AuthFailException.class,
BoardNotFoundException.class })
public ResponseEntity<?> badRequest(RuntimeException e) { public ResponseEntity<?> badRequest(RuntimeException e) {
log.info(e.getClass().getName() + e.getMessage()); log.info(e.getClass().getName() + e.getMessage());
return ResponseResult.fail(e.getMessage()); return ResponseResult.fail(e.getMessage());

View File

@@ -30,13 +30,15 @@ public class CommonInterceptor implements HandlerInterceptor {
private boolean validJWT(HttpServletRequest request) { private boolean validJWT(HttpServletRequest request) {
String token = request.getHeader("TOKEN");
String email = ""; String email = "";
try { try {
String token = request.getHeader("TOKEN");
email = JWTUtils.getIssuer(token); email = JWTUtils.getIssuer(token);
} catch (JWTVerificationException e) { } catch (JWTVerificationException e) {
return false; return false;
} catch (Exception e) {
log.info(e.getMessage());
return false;
} }
request.setAttribute("email", email); request.setAttribute("email", email);

View File

@@ -11,7 +11,7 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CommonInterceptor()) registry.addInterceptor(new CommonInterceptor())
.addPathPatterns("/api/*") .addPathPatterns("/api/**")
.excludePathPatterns("/api/public/*"); .excludePathPatterns("/api/public/*");
} }
} }

View File

@@ -1,7 +1,7 @@
insert into user(email, password, phone, reg_date, user_name, status, lock_yn) insert into user(email, password, phone, reg_date, user_name, status, lock_yn)
values values
('test11@naver.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-1234-1234', '2021-02-20 00:50:11.000000', 'kim', 'USING', 0), ('haerong22@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-1234-1234', '2021-02-20 00:50:11.000000', 'kim', 'USING', 0),
('test22@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4321-1111', '2021-02-25 12:33:16.000000', 'lee', 'USING', 0), ('test22@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4321-1111', '2021-02-25 12:33:16.000000', 'lee', 'USING', 0),
('test33@naver.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-5555-3333', now(), 'hong', 'USING', 0), ('test33@naver.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-5555-3333', now(), 'hong', 'USING', 0),
('test44@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4343-2546', now(), 'park', 'STOP', 0); ('test44@gmail.com', '$2a$10$2ElPbt2mwiAc9IYH3rgJz.YZInXlUd363utdyU0TWfne6Y3vKh8h6', '010-4343-2546', now(), 'park', 'STOP', 0);
@@ -37,8 +37,9 @@ values
insert into board_type (board_name, reg_date, using_yn) insert into board_type (board_name, reg_date, using_yn)
values values
('게시판1', now(), 1), ('게시판1', now(), 1),
('게시판2', now(), 0), ('게시판2', now(), 1),
('게시판3', now(), 1); ('게시판3', now(), 0),
('문의게시판', now(), 1);
insert into board (board_type_id, user_id, title, content, reg_date, top_yn) insert into board (board_type_id, user_id, title, content, reg_date, top_yn)
values values
@@ -48,7 +49,8 @@ values
(2, 1, '게시글4', '게시글 내용4', now(), 0), (2, 1, '게시글4', '게시글 내용4', now(), 0),
(2, 2, '게시글5', '게시글 내용5', now(), 0), (2, 2, '게시글5', '게시글 내용5', now(), 0),
(3, 1, '게시글6', '게시글 내용6', now(), 0), (3, 1, '게시글6', '게시글 내용6', now(), 0),
(3, 3, '게시글7', '게시글 내용7', now(), 0); (3, 3, '게시글7', '게시글 내용7', now(), 0),
(4, 1, '문의제목1', '문의 내용1', now(), 0);
insert into board_comment (comments, reg_date, board_id, user_id) insert into board_comment (comments, reg_date, board_id, user_id)
values values
@@ -59,11 +61,17 @@ values
INSERT INTO MAIL_TEMPLATE(TEMPLATE_ID, TITLE, CONTENTS, SEND_EMAIL, SEND_USER_NAME, REG_DATE) INSERT INTO MAIL_TEMPLATE(TEMPLATE_ID, TITLE, CONTENTS, SEND_EMAIL, SEND_USER_NAME, REG_DATE)
VALUES VALUES
('USER_RESET_PASSWORD', ('USER_RESET_PASSWORD',
'{USER_NAME}님의 비밀번호 초기화 요청입니다.', '{USER_NAME}님의 비밀번호 초기화 요청입니다.',
'<div><p>{USER_NAME}님 안녕하세요.</p><p>아래 링크를 클릭하여, 비밀번호를 초기화해 주세요.</p><p><a href="{SERVER_URL}/reset?key={RESET_PASSWORD_KEY}">초기화</a></p></div>', '<div><p>{USER_NAME}님 안녕하세요.</p><p>아래 링크를 클릭하여, 비밀번호를 초기화해 주세요.</p><p><a href="{SERVER_URL}/reset?key={RESET_PASSWORD_KEY}">초기화</a></p></div>',
'test.email.12588@gmail.com', '관리자', now()), 'test.email.12588@gmail.com', '관리자', now()),
('BOARD_ADD', ('BOARD_ADD',
'{USER_NAME}님이 글을 게시하였습니다.', '{USER_NAME}님이 글을 게시하였습니다.',
'<div><p>제목: {BOARD_TITLE}</p><p>내용</p><div>{BOARD_CONTENTS}</div></div>', '<div><p>제목: {BOARD_TITLE}</p><p>내용</p><div>{BOARD_CONTENTS}</div></div>',
'test.email.12588@gmail.com', '관리자', now()); 'test.email.12588@gmail.com', '관리자', now()),
('BOARD_REPLY',
'{USER_NAME}님이 글에 답변이 작성되었습니다.',
'<div><p>제목: {BOARD_TITLE}</p><p>내용</p><div>{BOARD_CONTENTS}</div><p>답변</p><div>{BOARD_REPLY_CONTENTS}</div></div>',
'test.email.12588@gmail.com', '관리자', now());

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{USER_NAME}님이 글에 답변이 작성되었습니다.</title>
</head>
<body>
<div>
<p>제목: {BOARD_TITLE}</p>
<p>내용</p>
<div>
{BOARD_CONTENTS}
</div>
<p>답변</p>
<div>
{BOARD_REPLY_CONTENTS}
</div>
</div>
</body>
</html>