diff --git a/src/main/java/yonseigolf/server/apply/service/ApplyService.java b/src/main/java/yonseigolf/server/apply/service/ApplyService.java index d9db6b0..8c925be 100644 --- a/src/main/java/yonseigolf/server/apply/service/ApplyService.java +++ b/src/main/java/yonseigolf/server/apply/service/ApplyService.java @@ -68,18 +68,6 @@ public void updatePass(Long id, UpdatePassRequest request) { findById(id).updatePass(request); } -// @Transactional -// public void updateDocumentPass(Long id, Boolean updatePass) { -// -// findById(id).updateDocumentPass(updatePass); -// } -// -// @Transactional -// public void updateFinalPass(Long id, Boolean finalPass) { -// -// findById(id).updateFinalPass(finalPass); -// } - @Transactional public void updateInterviewTime(Long id, LocalDateTime time) { @@ -88,7 +76,7 @@ public void updateInterviewTime(Long id, LocalDateTime time) { public void sendEmailNotification(boolean isDocumentPass, Boolean isFinalPass) { - final NotificationType type = getNotificationType(isDocumentPass, isFinalPass); + final NotificationType type = NotificationType.decideNotificationType(isDocumentPass, isFinalPass); final String subject = "안녕하세요. 연세대학교 골프동아리 결과 메일입니다."; findApplicationsByPassFail(isDocumentPass, isFinalPass) @@ -98,17 +86,6 @@ public void sendEmailNotification(boolean isDocumentPass, Boolean isFinalPass) { }); } - private NotificationType getNotificationType(boolean isDocumentPass, Boolean isFinalPass) { - - if (isDocumentPass && isFinalPass == null) { - return NotificationType.DOCUMENT_PASS; - } - if (isDocumentPass && isFinalPass) { - return NotificationType.FINAL_PASS; - } - return NotificationType.FAIL; - } - private List findApplicationsByPassFail(Boolean docuemntPass, Boolean finalPass) { return applicationRepository.findApplicationsForEmail(docuemntPass, finalPass); diff --git a/src/main/java/yonseigolf/server/board/entity/Board.java b/src/main/java/yonseigolf/server/board/entity/Board.java index 5f7d53d..97dbfe5 100644 --- a/src/main/java/yonseigolf/server/board/entity/Board.java +++ b/src/main/java/yonseigolf/server/board/entity/Board.java @@ -53,7 +53,7 @@ public static Board createBoardForForeignKey(long boardId) { public void updateBoard(UpdateBoardRequest request) { - if (this.deleted == true) { + if (this.deleted) { throw new DeletedBoardException("이미 삭제된 게시글 입니다."); } @@ -64,10 +64,17 @@ public void updateBoard(UpdateBoardRequest request) { public void deleteBoard() { - if (this.deleted == true) { + if (this.deleted) { throw new DeletedBoardException("이미 삭제된 게시글 입니다."); } this.deleted = true; } + + public void checkOwner(Long userId) { + + if(!this.writer.checkOwner(userId)){ + throw new IllegalArgumentException("작성자만 게시글을 수정/삭제 할 수 있습니다."); + } + } } diff --git a/src/main/java/yonseigolf/server/board/entity/Reply.java b/src/main/java/yonseigolf/server/board/entity/Reply.java index 6f23358..a57da3c 100644 --- a/src/main/java/yonseigolf/server/board/entity/Reply.java +++ b/src/main/java/yonseigolf/server/board/entity/Reply.java @@ -37,4 +37,11 @@ public static Reply createReplyForPost(long writerId, long boardId, PostReplyReq .user(User.createUserForForeignKey(writerId)) .build(); } + + public void checkOwner(Long userId) { + + if (!user.checkOwner(userId)) { + throw new IllegalArgumentException("작성자가 아닙니다."); + } + } } diff --git a/src/main/java/yonseigolf/server/board/service/BoardService.java b/src/main/java/yonseigolf/server/board/service/BoardService.java index e35d63d..d0ae63b 100644 --- a/src/main/java/yonseigolf/server/board/service/BoardService.java +++ b/src/main/java/yonseigolf/server/board/service/BoardService.java @@ -45,25 +45,19 @@ public void postBoard(CreateBoardRequest createBoardRequest, long userId) { boardRepository.save(Board.createBoardForPost(createBoardRequest, userId)); } - // TODO: 작성자와 수정하려는 사람이 같은 지 확인 @Transactional public void updateBoard(Long boardId, UpdateBoardRequest createBoardRequest, Long userId) { Board board = findById(boardId); - if (board.getWriter().getId() != userId) { - throw new IllegalArgumentException("작성자와 수정하려는 사람이 다릅니다."); - } + board.checkOwner(userId); board.updateBoard(createBoardRequest); } - // TODO: 작성자와 삭제하려는 사람이 같은지 확인 @Transactional public void deleteBoard(Long boardId, Long userId) { Board board = findById(boardId); - if (board.getWriter().getId() != userId) { - throw new IllegalArgumentException("작성자만 게시글을 삭제할 수 있습니다."); - } + board.checkOwner(userId); board.deleteBoard(); } diff --git a/src/main/java/yonseigolf/server/board/service/ReplyService.java b/src/main/java/yonseigolf/server/board/service/ReplyService.java index 41b01c3..438b2dd 100644 --- a/src/main/java/yonseigolf/server/board/service/ReplyService.java +++ b/src/main/java/yonseigolf/server/board/service/ReplyService.java @@ -22,12 +22,10 @@ public void postReply(long writerId, long boardId, PostReplyRequest replyRequest replyRepository.save(reply); } - // TODO: 작성자와 삭제하려는 사람이 같은지 확인 public void deleteReply(long replyId, long userId) { + Reply reply = findReply(replyId); - if (reply.getUser().getId() != userId){ - throw new IllegalArgumentException("작성자가 아닙니다."); - } + reply.checkOwner(userId); replyRepository.deleteById(replyId); } diff --git a/src/main/java/yonseigolf/server/email/dto/NotificationType.java b/src/main/java/yonseigolf/server/email/dto/NotificationType.java index ff61792..7fb9f71 100644 --- a/src/main/java/yonseigolf/server/email/dto/NotificationType.java +++ b/src/main/java/yonseigolf/server/email/dto/NotificationType.java @@ -34,4 +34,14 @@ public String generateMessage(String name) { }; public abstract String generateMessage(String name); + + public static NotificationType decideNotificationType(boolean isDocumentPass, Boolean isFinalPass) { + if (isDocumentPass && isFinalPass == null) { + return DOCUMENT_PASS; + } + if (isDocumentPass && isFinalPass) { + return FINAL_PASS; + } + return FAIL; + } } diff --git a/src/main/java/yonseigolf/server/user/controller/UserController.java b/src/main/java/yonseigolf/server/user/controller/UserController.java index e381654..47ab769 100644 --- a/src/main/java/yonseigolf/server/user/controller/UserController.java +++ b/src/main/java/yonseigolf/server/user/controller/UserController.java @@ -67,18 +67,17 @@ public ResponseEntity> kakaoLogin(@RequestBody ); } - // TODO: 세션이 아닌 JWT Token으로부터 userId 가져와야 함, user의 refresh token이 없거나 만료된 경우 재발급 @PostMapping("/users/signIn") public ResponseEntity> signIn(@RequestAttribute(required = false) Long kakaoId, HttpServletResponse response) { - System.out.println(kakaoId); + LoggedInUser loggedInUser = userService.signIn(kakaoId); // 30분 시간 제한 Date date = new Date(new Date().getTime() + 1800000); String tokenReponse = jwtUtil.createToken(loggedInUser, date); - // refresh token 검증후 발급 - 2주기한 - validateRefreshTokenAndRefresh(loggedInUser.getId(), response, loggedInUser); + // signIn 할 경우 로그인 진행 + makeRefreshToken(response, loggedInUser); return ResponseEntity .ok() @@ -89,9 +88,7 @@ public ResponseEntity> signIn(@RequestAttribute ); } - private void validateRefreshTokenAndRefresh(Long userId, HttpServletResponse response, LoggedInUser loggedInUser) { - // refresh token이 없거나 만료된 경우 재발급 - userService.validateRefreshToken(userId, jwtUtil); + private void makeRefreshToken(HttpServletResponse response, LoggedInUser loggedInUser) { Date expireDate = new Date(new Date().getTime() + 1209600000); String refreshToken = jwtUtil.createRefreshToken(loggedInUser.getId(), expireDate); @@ -102,7 +99,7 @@ private void validateRefreshTokenAndRefresh(Long userId, HttpServletResponse res private void createRefreshToken(HttpServletResponse response, String refreshToken) { Cookie cookie = new Cookie("refreshToken", refreshToken); cookie.setHttpOnly(true); // HTTP Only 설정 - cookie.setSecure(true); // Secure 설정, TODO: 배포할 땐 true로 변경 + cookie.setSecure(true); // Secure 설정, cookie.setPath("/"); // 경로 설정 cookie.setMaxAge(60 * 60 * 24 * 14); // 2주일 response.addCookie(cookie); // 응답에 쿠키 추가 diff --git a/src/main/java/yonseigolf/server/user/entity/User.java b/src/main/java/yonseigolf/server/user/entity/User.java index cc76d80..2a22bcb 100644 --- a/src/main/java/yonseigolf/server/user/entity/User.java +++ b/src/main/java/yonseigolf/server/user/entity/User.java @@ -93,4 +93,8 @@ public void invalidateRefreshToken() { this.refreshToken = null; } + + public boolean checkOwner(Long userId) { + return this.id == userId; + } } diff --git a/src/test/java/yonseigolf/server/board/service/BoardServiceTest.java b/src/test/java/yonseigolf/server/board/service/BoardServiceTest.java index 9a0e893..3e37f96 100644 --- a/src/test/java/yonseigolf/server/board/service/BoardServiceTest.java +++ b/src/test/java/yonseigolf/server/board/service/BoardServiceTest.java @@ -172,7 +172,7 @@ void deleteErrorTest() { // when & then assertThatThrownBy(() -> boardService.deleteBoard(savedBoard.getId(), 2L)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("작성자만 게시글을 삭제할 수 있습니다."); + .hasMessage("작성자만 게시글을 수정/삭제 할 수 있습니다."); } @Test @@ -219,7 +219,7 @@ void updateBoardTest() { // then assertThatThrownBy(() -> boardService.updateBoard(saved.getId(), UpdateBoardRequest.builder().build(), 2L)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("작성자와 수정하려는 사람이 다릅니다."); + .hasMessage("작성자만 게시글을 수정/삭제 할 수 있습니다."); } @Test diff --git a/src/test/java/yonseigolf/server/board/service/ReplyServiceTest.java b/src/test/java/yonseigolf/server/board/service/ReplyServiceTest.java index 0787e4e..ac22b2f 100644 --- a/src/test/java/yonseigolf/server/board/service/ReplyServiceTest.java +++ b/src/test/java/yonseigolf/server/board/service/ReplyServiceTest.java @@ -1,10 +1,10 @@ package yonseigolf.server.board.service; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; import yonseigolf.server.board.dto.request.PostReplyRequest; import yonseigolf.server.board.entity.Board; import yonseigolf.server.board.entity.Category; @@ -21,6 +21,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; +@Transactional @SpringBootTest class ReplyServiceTest { @@ -33,12 +34,6 @@ class ReplyServiceTest { @Autowired private UserRepository userRepository; - @BeforeEach - void setUp() { - replyRepository.deleteAll(); - boardRepository.deleteAll(); - userRepository.deleteAll(); - } @Test @DisplayName("댓글 생성 테스트") void postReplyTest() {