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

[Refactor]/#76 refactor code #77

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 1 addition & 24 deletions src/main/java/yonseigolf/server/apply/service/ApplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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)
Expand All @@ -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<Application> findApplicationsByPassFail(Boolean docuemntPass, Boolean finalPass) {

return applicationRepository.findApplicationsForEmail(docuemntPass, finalPass);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/yonseigolf/server/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("이미 삭제된 게시글 입니다.");
}

Expand All @@ -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("작성자만 게시글을 수정/삭제 할 수 있습니다.");
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/yonseigolf/server/board/entity/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("작성자가 아닙니다.");
}
}
}
10 changes: 2 additions & 8 deletions src/main/java/yonseigolf/server/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/yonseigolf/server/email/dto/NotificationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,17 @@ public ResponseEntity<CustomResponse<JwtTokenResponse>> kakaoLogin(@RequestBody
);
}

// TODO: 세션이 아닌 JWT Token으로부터 userId 가져와야 함, user의 refresh token이 없거나 만료된 경우 재발급
@PostMapping("/users/signIn")
public ResponseEntity<CustomResponse<JwtTokenResponse>> 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()
Expand All @@ -89,9 +88,7 @@ public ResponseEntity<CustomResponse<JwtTokenResponse>> 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);
Expand All @@ -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); // 응답에 쿠키 추가
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/yonseigolf/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ public void invalidateRefreshToken() {

this.refreshToken = null;
}

public boolean checkOwner(Long userId) {
return this.id == userId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void deleteErrorTest() {
// when & then
assertThatThrownBy(() -> boardService.deleteBoard(savedBoard.getId(), 2L))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("작성자만 게시글을 삭제할 수 있습니다.");
.hasMessage("작성자만 게시글을 수정/삭제 할 수 있습니다.");
}

@Test
Expand Down Expand Up @@ -219,7 +219,7 @@ void updateBoardTest() {
// then
assertThatThrownBy(() -> boardService.updateBoard(saved.getId(), UpdateBoardRequest.builder().build(), 2L))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("작성자와 수정하려는 사람이 다릅니다.");
.hasMessage("작성자만 게시글을 수정/삭제 할 수 있습니다.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +21,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

@Transactional
@SpringBootTest
class ReplyServiceTest {

Expand All @@ -33,12 +34,6 @@ class ReplyServiceTest {
@Autowired
private UserRepository userRepository;

@BeforeEach
void setUp() {
replyRepository.deleteAll();
boardRepository.deleteAll();
userRepository.deleteAll();
}
@Test
@DisplayName("댓글 생성 테스트")
void postReplyTest() {
Expand Down