Skip to content

Commit

Permalink
[REFACTOR] 댓글&좋아요 개수 Redis 처리 및 대댓글 구조 해결(#117) (#125)
Browse files Browse the repository at this point in the history
* ♻️refactor: 좋아요 redis 적용(#117)

* ♻️refactor: 대댓글 구조 변경 및 댓글 수 레디스에 적용(#117)

* ♻️refactor: 대댓글 구조 변경 및 댓글 수 레디스에 적용 및 좋아요 레디스 적용(#117)

* ♻️refactor: service 측 오류 수정(#117)

* ♻️refactor: redisUtil 부분 boardSaveLikeCount & boardGetLikeCount 추가 및 적용(#117)

* ♻️refactor: tacticPositionDetail 피드백에 맞게 수정(#117)

* ♻️refactor: MyTeam 개별 조회 시, 해당 팀에 속해있지 않는 상황에 대한 예외처리 로직 추가(#117)

* ♻️refactor: MyTeam 개별 조회 시, 해당 팀에 속해있지 않는 상황에 대한 예외처리(controller 수정)(#117)

* ♻️refactor: MyTeamList 조회 시, 본인이 방장인 팀과 팀원인 팀 둘다 조회되게 수정 완료 및 팀 개별 조회 시 본인 팀이 아닐 시 예외처리 추가(#117)

* ♻️refactor: 좋아요 레디스 적용 수정(getLikeCount -> boardGetLikeCount 추가)(#117)

* ♻️refactor: 댓글 갯수 redis 적용 완료(#117)

* ♻️refactor: 코드 최종 정리(#117)

* ♻️refactor: TacticService에서 필요없는 코드 정리(#117)

* ♻️refactor: BoardResponseDto에서 댓글, 좋아요 갯수 수정(#117)

* ♻️refactor: BoardResponseDto가 불필요하여 BoardResponseDto -> BoardListDto로 변경 후 코드 간소화 함(#117)

* ♻️refactor: 변경된 경로 수정(#117)

* ♻️refactor: RedisConfig에서 필요없는 메서드 제거(#117)
  • Loading branch information
600gramSik authored Jun 18, 2024
1 parent 92c2362 commit a0113cc
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 135 deletions.
5 changes: 4 additions & 1 deletion http/AccountsTest.http
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ Content-Type: application/json
"passwordCheck": "test1234!!"
}


### 로그인

POST {{api}}/accounts/login
Content-Type: application/json

{
"email": "[email protected]",
"password": "test1234!!"
"password": "test1234!!",
"fcmToken": "String"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.capstone.BnagFer.domain.board.dto.request.CreateCommentRequestDto;
import com.capstone.BnagFer.domain.board.dto.request.UpdateCommentRequestDto;
import com.capstone.BnagFer.domain.board.dto.response.BoardDetailResponseDto;
import com.capstone.BnagFer.domain.board.dto.response.BoardResponseDto;
import com.capstone.BnagFer.domain.board.dto.response.BoardListDto;
import com.capstone.BnagFer.domain.board.dto.response.CommentResponseDto;
import com.capstone.BnagFer.domain.board.dto.response.CreateBoardResponseDto;
import com.capstone.BnagFer.domain.board.service.BoardQueryService;
Expand All @@ -31,34 +31,37 @@ public class BoardController {
private final BoardService boardService;
private final BoardQueryService boardQueryService;


@GetMapping //게시판 리스트 조회
@Operation(summary = "게시판 목록 조회", description = "전체 게시판 목록을 조회합니다. 페이징 적용, 생성 날짜 기준 내림차순 정렬.")
@GetMapping
public ApiResponse<Page<BoardDetailResponseDto.BoardList>> getBoardsList(

@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size)
{
Pageable pageable = PageRequest.of(page, size);
Page<BoardDetailResponseDto.BoardList> boardsList = boardQueryService.getBoards(pageable);
Page<BoardListDto> boardsList = boardQueryService.getBoards(pageable);
return onSuccess(boardsList);

}

@Operation(summary = "개별 게시물 조회", description = "단일 게시물의 내용을 조회합니다.")
@GetMapping("/{boardId}")
public ApiResponse<BoardResponseDto> getBoard (@PathVariable Long boardId) {
BoardResponseDto board = boardQueryService.getBoard(boardId);
public ApiResponse<BoardDetailResponseDto> getBoard (@PathVariable Long boardId) {
BoardDetailResponseDto board = boardQueryService.getBoard(boardId);
return ApiResponse.onSuccess(board);
}

@Operation(summary = "내 게시물 목록 조회", description = "자신이 작성한 게시글 목록 조회. 페이징 적용, 생성 날짜 기준 내림차순 정렬.")
@GetMapping("/myboards")
public ApiResponse<Page<BoardDetailResponseDto.BoardList>> getMyBoardsList(
public ApiResponse<Page<BoardListDto>> getMyBoardsList(
@LoginUser User user,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size)
{
Pageable pageable = PageRequest.of(page, size);
Page<BoardDetailResponseDto.BoardList> myBoardsList = boardQueryService.getMyBoards(user, pageable);
Page<BoardListDto> myBoardsList = boardQueryService.getMyBoards(user, pageable);
return onSuccess(myBoardsList);
}

Expand Down Expand Up @@ -99,7 +102,7 @@ public ApiResponse<Object> deleteBoard(@PathVariable Long boardId, @LoginUser Us

@Operation(summary = "게시글 좋아요 & 좋아요 취소", description = "게시글에 좋아요를 누르는 기능. 한번 더누르면 좋아요 취소.")
@PostMapping("/{boardId}/like")
public ApiResponse<Object> likeBoard(@PathVariable Long boardId, @LoginUser User user) {
public ApiResponse<Object> likeButton(@PathVariable Long boardId, @LoginUser User user) {
return boardService.likeButton(boardId, user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ public record BoardDetailResponseDto(
String writerNickName,
String boardTitle,
String boardContent,
List<Comment> commentContent,
int likeCount
List<CommentList> commentList,
Long likeCount,
Long commentCount
) {
public static BoardDetailResponseDto from(Board board) {
public static BoardDetailResponseDto from(Board board, Long likeCount, Long commentCount) {
return BoardDetailResponseDto.builder()
.id(board.getId())
.writerId(board.getUser().getId())
.writerNickName(board.getUser().getProfile().getNickname())
.boardTitle(board.getBoardTitle())
.boardContent(board.getBoardContent())
.commentContent(board.getComments())
.likeCount(board.getLikes().size())
.commentList(CommentList.from(board.getComments()))
.likeCount(likeCount)
.commentCount(commentCount)
.build();
}
@Builder
Expand All @@ -37,7 +39,8 @@ public record CommentList(
String nickName,
String commentText,
LocalDateTime createdAt,
LocalDateTime updatedAt
LocalDateTime updatedAt,
List<CommentList> children
) {
public static CommentList from(Comment comment) {
return CommentList.builder()
Expand All @@ -48,32 +51,12 @@ public static CommentList from(Comment comment) {
.commentText(comment.getCommentText())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.children(comment.getChildren().stream().map(CommentList::from).collect(Collectors.toList()))
//최상위 댓글로만 자식 댓글이 달릴 수 있게!
.build();
}
public static List<CommentList> from(List<Comment> comments) {
return comments.stream().map(CommentList::from).collect(Collectors.toList());
}
}
@Builder
public record BoardList(
Long id,
Long userId,
String writerNickName,
String boardTitle,
int likeCount

) {
public static BoardList from(Board board) {
return BoardList.builder()
.id(board.getId())
.userId(board.getUser().getId())
.writerNickName(board.getUser().getProfile().getNickname())
.boardTitle(board.getBoardTitle())
.likeCount(board.getLikes().size())
.build();
}
public static List<BoardList> from(List<Board> boards) {
return boards.stream().map(BoardList::from).collect(Collectors.toList());
return comments.stream().filter(c -> c.getParent() == null).map(CommentList::from).collect(Collectors.toList());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.capstone.BnagFer.domain.board.dto.response;

import com.capstone.BnagFer.domain.board.entity.Board;
import lombok.Builder;

import java.util.List;
import java.util.stream.Collectors;

@Builder
public record BoardListDto(
Long id,
Long userId,
String writerNickName,
String boardTitle,
Long likeCount,
Long commentCount

) {
public static BoardListDto from(Board board, Long commentCount, Long likeCount) {
return BoardListDto.builder()
.id(board.getId())
.userId(board.getUser().getId())
.writerNickName(board.getUser().getProfile().getNickname())
.boardTitle(board.getBoardTitle())
.likeCount(likeCount)
.commentCount(commentCount)
.build();
}
public static BoardListDto from(Board board) {
return from(board, 0L, 0L);
}
public static List<BoardListDto> from(List<Board> boards) {
return boards.stream().map(board -> from(board, 0L, 0L)).collect(Collectors.toList());
}
}


This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;

public record CommentResponseDto(
Long id,
Long boardId,
Expand All @@ -24,4 +26,5 @@ public static CommentResponseDto from(Comment comment) {
comment.getUpdatedAt()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class Like extends BaseEntity {
@Column(name = "like_id", nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

@Repository
public interface BoardCommentRepository extends JpaRepository<Comment, Long> {
long countByBoardId(Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import com.capstone.BnagFer.domain.accounts.exception.AccountsExceptionHandler;
import com.capstone.BnagFer.domain.accounts.repository.UserJpaRepository;
import com.capstone.BnagFer.domain.board.dto.response.BoardDetailResponseDto;
import com.capstone.BnagFer.domain.board.dto.response.BoardResponseDto;
import com.capstone.BnagFer.domain.board.dto.response.BoardListDto;
import com.capstone.BnagFer.domain.board.entity.Board;
import com.capstone.BnagFer.domain.board.exception.BoardExceptionHandler;
import com.capstone.BnagFer.domain.board.repository.BoardRepository;
import com.capstone.BnagFer.global.common.ErrorCode;
import com.capstone.BnagFer.global.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -22,18 +23,19 @@
public class BoardQueryService {

private final BoardRepository boardRepository;
private final RedisUtil redisUtil;
private final UserJpaRepository userJpaRepository;

public Page<BoardDetailResponseDto.BoardList> getBoards(Pageable pageable) {
public Page<BoardListDto> getBoards(Pageable pageable) {
Page<Board> boards = boardRepository.findAll(pageable);
return boards.map(BoardDetailResponseDto.BoardList::from);
return boards.map(BoardListDto::from);
}

public Page<BoardDetailResponseDto.BoardList> getMyBoards(User user, Pageable pageable) {
public Page<BoardListDto> getMyBoards(User user, Pageable pageable) {
Page<Board> boards = getBoardsByUser(user, pageable);
return boards.map(BoardDetailResponseDto.BoardList::from);
return boards.map(BoardListDto::from);
}

public BoardDetailResponseDto getBoard(Long boardId) {
public Page<BoardDetailResponseDto.BoardList> getUserBoards(Long userId, Pageable pageable) {
User user = userJpaRepository.findById(userId)
.orElseThrow(() -> new AccountsExceptionHandler(ErrorCode.USER_NOT_FOUND));
Expand All @@ -43,7 +45,18 @@ public Page<BoardDetailResponseDto.BoardList> getUserBoards(Long userId, Pageabl

public BoardResponseDto getBoard(Long boardId) {
Board board = boardRepository.findById(boardId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.BOARD_NOT_FOUND));
return BoardResponseDto.from(board);
Long likeCount = redisUtil.boardGetLikeCount(boardId);

if (likeCount == null) {
likeCount = (long) board.getLikes().size();
redisUtil.boardSaveLikeCount(boardId, likeCount);
}
Long commentCount = redisUtil.boardGetCommentCount(boardId);
if(commentCount == null){
commentCount = (long) board.getComments().size();
redisUtil.boardSaveCommentCount(boardId, commentCount);
}
return BoardDetailResponseDto.from(board, likeCount, commentCount);
}

public Page<Board> getBoardsByUser(User user, Pageable pageable) {
Expand Down
Loading

0 comments on commit a0113cc

Please sign in to comment.