From 11aec2ff262b36872451f304836e45f3b1f74178 Mon Sep 17 00:00:00 2001 From: Geunsik Kim <93639883+600gramSik@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:24:08 +0900 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=EC=9E=90=EC=9C=A0=EA=B2=8C?= =?UTF-8?q?=EC=8B=9C=ED=8C=90=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=B0=A8?= =?UTF-8?q?=EB=8B=A8=20=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C=20(#156)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: BoardBlock 엔티티 구현 * :sparkles: 차단한 사용자의 게시글 조회 시 안보이게 구현 * :sparkles: 차단 해제 기능 구현(#150) * :sparkles: 전체 게시물 조회 & 내 게시물 전체 조회에서 likeCount, commentCount 적용되게 수정(#150) * :sparkles: 코드 최종 검사(#150) * :sparkles: 댓글 생성 쪽 수정완료(#150) * :sparkles: Controller에 내가 차단한 사용자 조회 제목 및 설명 추가(#150) * :sparkles: 특정 사용자의 게시글 조회 기능 수정 및 merge 전 코드 최종점검(#150) * :sparkles: 특정 사용자의 게시글 조회 기능 수정 및 merge 전 코드 최종점검(#150) * :sparkles: 특정 사용자의 게시글 조회 기능 수정 및 merge 전 코드 최종점검(#150) --- .../board/controller/BoardController.java | 38 ++++++++++-- .../dto/request/BoardBlockRequestDto.java | 13 ++++ .../dto/response/BoardBlockResponseDto.java | 22 +++++++ .../dto/response/BoardDetailResponseDto.java | 4 +- .../board/dto/response/BoardListDto.java | 19 +++--- .../response/GetMyBoardBlockResponseDto.java | 22 +++++++ .../BnagFer/domain/board/entity/Board.java | 1 - .../domain/board/entity/BoardBlock.java | 30 ++++++++++ .../repository/BoardBlockRepository.java | 19 ++++++ .../board/repository/BoardLikeRepository.java | 1 + .../board/repository/BoardRepository.java | 3 + .../board/service/BoardBlockQueryService.java | 26 ++++++++ .../board/service/BoardBlockService.java | 47 +++++++++++++++ .../board/service/BoardQueryService.java | 59 ++++++++++++++++--- .../domain/board/service/BoardService.java | 40 +++++++++++-- .../BnagFer/global/common/ErrorCode.java | 4 ++ 16 files changed, 317 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/dto/request/BoardBlockRequestDto.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardBlockResponseDto.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/dto/response/GetMyBoardBlockResponseDto.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/entity/BoardBlock.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/repository/BoardBlockRepository.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockQueryService.java create mode 100644 src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockService.java diff --git a/src/main/java/com/capstone/BnagFer/domain/board/controller/BoardController.java b/src/main/java/com/capstone/BnagFer/domain/board/controller/BoardController.java index 6331a2ab..c2a7e6fb 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/controller/BoardController.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/controller/BoardController.java @@ -5,6 +5,8 @@ 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.*; +import com.capstone.BnagFer.domain.board.service.BoardBlockQueryService; +import com.capstone.BnagFer.domain.board.service.BoardBlockService; import com.capstone.BnagFer.domain.board.service.BoardQueryService; import com.capstone.BnagFer.domain.board.service.BoardService; import com.capstone.BnagFer.global.annotation.LoginUser; @@ -30,7 +32,9 @@ @RequestMapping("/board") public class BoardController { private final BoardService boardService; + private final BoardBlockService boardBlockService; private final BoardQueryService boardQueryService; + private final BoardBlockQueryService boardBlockQueryService; @GetMapping //게시판 리스트 조회 @@ -38,18 +42,19 @@ public class BoardController { public ApiResponse> getBoardsList( @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "10") int size) + @RequestParam(defaultValue = "10") int size, + @LoginUser User user) { Pageable pageable = PageRequest.of(page, size); - Page boardsList = boardQueryService.getBoards(pageable); + Page boardsList = boardQueryService.getBoards(user, pageable); return onSuccess(boardsList); } @Operation(summary = "개별 게시물 조회", description = "단일 게시물의 내용을 조회합니다.") @GetMapping("/{boardId}") - public ApiResponse getBoard (@PathVariable(name = "boardId") Long boardId) { - BoardDetailResponseDto board = boardQueryService.getBoard(boardId); + public ApiResponse getBoard (@LoginUser User user, @PathVariable(name = "boardId") Long boardId) { + BoardDetailResponseDto board = boardQueryService.getBoard(user, boardId); return ApiResponse.onSuccess(board); } @@ -70,10 +75,11 @@ public ApiResponse> getMyBoardsList( public ApiResponse> getUserBoardsList( @PathVariable(name = "userId") Long userId, @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "10") int size) + @RequestParam(defaultValue = "10") int size, + @LoginUser User user) { Pageable pageable = PageRequest.of(page, size); - Page userBoardsList = boardQueryService.getUserBoards(userId, pageable); + Page userBoardsList = boardQueryService.getUserBoards(user, userId, pageable); return onSuccess(userBoardsList); } @@ -143,4 +149,24 @@ public ApiResponse deleteComment(@PathVariable(name = "commentId") Long co boardService.deleteComment(commentId, user); return ApiResponse.noContent(); } + @Operation(summary = "자유게시판 사용자 차단", description = "차단을 하게되면, 차단한 사용자의 댓글, 게시글을 볼 수 없다.") + @PostMapping("/block/{isBlockedUserId}") + public ApiResponse blockUser(@LoginUser User user, @PathVariable(name = "isBlockedUserId") Long isBlockedUserId) { + BoardBlockResponseDto boardBlockResponseDto = boardBlockService.blockUser(user, isBlockedUserId); + return ApiResponse.onSuccess(boardBlockResponseDto); + } + + @Operation(summary = "자유게시판 사용자 차단해제", description = "차단을 해제하는 기능.") + @DeleteMapping("/block/{isBlockedUserId}") + public ApiResponse unblockUser(@LoginUser User user, @PathVariable(name = "isBlockedUserId") Long isBlockedUserId) { + boardBlockService.unblockUser(user, isBlockedUserId); + return ApiResponse.noContent(); + } + @Operation(summary = "내가 차단한 사용자 조회", description = "내가 차단한 사용자를 조회해주는 기능") + @GetMapping("/blocked-users") + public ApiResponse> getBlockedUsers(@LoginUser User currentUser) { + List blockedUsers = boardBlockQueryService.getBlockedUsers(currentUser); + return ApiResponse.onSuccess(blockedUsers); + } + } diff --git a/src/main/java/com/capstone/BnagFer/domain/board/dto/request/BoardBlockRequestDto.java b/src/main/java/com/capstone/BnagFer/domain/board/dto/request/BoardBlockRequestDto.java new file mode 100644 index 00000000..0aff4f8e --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/dto/request/BoardBlockRequestDto.java @@ -0,0 +1,13 @@ +package com.capstone.BnagFer.domain.board.dto.request; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.board.entity.BoardBlock; + +public record BoardBlockRequestDto() { + public BoardBlock toEntity(User blockUser, User isBlockedUser) { + return BoardBlock.builder() + .blockUser(blockUser) + .isBlockedUser(isBlockedUser) + .build(); + } +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardBlockResponseDto.java b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardBlockResponseDto.java new file mode 100644 index 00000000..2accd469 --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardBlockResponseDto.java @@ -0,0 +1,22 @@ +package com.capstone.BnagFer.domain.board.dto.response; + +import com.capstone.BnagFer.domain.board.entity.BoardBlock; +import lombok.Builder; + +import java.time.LocalDateTime; +@Builder +public record BoardBlockResponseDto( + Long id, + Long blockUser, + Long isBlockedUser, + LocalDateTime blockedAt +) { + public static BoardBlockResponseDto from(BoardBlock boardBlock) { + return BoardBlockResponseDto.builder() + .id(boardBlock.getId()) + .blockUser(boardBlock.getBlockUser().getId()) + .isBlockedUser(boardBlock.getIsBlockedUser().getId()) + .blockedAt(boardBlock.getBlockedAt()) + .build(); + } +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardDetailResponseDto.java b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardDetailResponseDto.java index 35ef1b1a..80977ba8 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardDetailResponseDto.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardDetailResponseDto.java @@ -20,7 +20,7 @@ public record BoardDetailResponseDto( Long likeCount, Long commentCount ) { - public static BoardDetailResponseDto from(Board board, Long likeCount, Long commentCount) { + public static BoardDetailResponseDto from(Board board, Long likeCount, Long commentCount, List filteredComments) { return BoardDetailResponseDto.builder() .id(board.getId()) .writerId(board.getUser().getId()) @@ -28,7 +28,7 @@ public static BoardDetailResponseDto from(Board board, Long likeCount, Long comm .boardTitle(board.getBoardTitle()) .boardContent(board.getBoardContent()) .images(BoardImageList.from(board.getImages())) - .commentList(CommentList.from(board.getComments())) + .commentList(CommentList.from(filteredComments)) .likeCount(likeCount) .commentCount(commentCount) .build(); diff --git a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardListDto.java b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardListDto.java index 62bd03da..7af64523 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardListDto.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/BoardListDto.java @@ -16,22 +16,25 @@ public record BoardListDto( Long commentCount ) { - public static BoardListDto from(Board board, Long commentCount, Long likeCount) { + public static BoardListDto from(Board board) { return BoardListDto.builder() .id(board.getId()) .userId(board.getUser().getId()) .writerNickName(board.getUser().getProfile().getNickname()) .boardTitle(board.getBoardTitle()) - .likeCount(likeCount) - .commentCount(commentCount) + .likeCount((long) board.getLikes().size()) + .commentCount((long) board.getComments().size()) .build(); } - public static BoardListDto from(Board board) { - return from(board, 0L, 0L); - } - public static List from(List boards) { - return boards.stream().map(board -> from(board, 0L, 0L)).collect(Collectors.toList()); + + public static List from(List boards, List blockedUserIds) { + return boards.stream() + .filter(board -> !blockedUserIds.contains(board.getUser().getId())) + .map(BoardListDto::from) + .collect(Collectors.toList()); } + + } diff --git a/src/main/java/com/capstone/BnagFer/domain/board/dto/response/GetMyBoardBlockResponseDto.java b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/GetMyBoardBlockResponseDto.java new file mode 100644 index 00000000..25e1a2f8 --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/dto/response/GetMyBoardBlockResponseDto.java @@ -0,0 +1,22 @@ +package com.capstone.BnagFer.domain.board.dto.response; + +import com.capstone.BnagFer.domain.board.entity.BoardBlock; +import lombok.Builder; + +import java.time.LocalDateTime; +@Builder +public record GetMyBoardBlockResponseDto( + Long id, + Long isBlockedUser, + String nickName, + LocalDateTime blockedAt +) { + public static GetMyBoardBlockResponseDto from(BoardBlock boardBlock) { + return GetMyBoardBlockResponseDto.builder() + .id(boardBlock.getId()) + .isBlockedUser(boardBlock.getIsBlockedUser().getId()) + .nickName(boardBlock.getIsBlockedUser().getProfile().getNickname()) + .blockedAt(boardBlock.getBlockedAt()) + .build(); + } +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/entity/Board.java b/src/main/java/com/capstone/BnagFer/domain/board/entity/Board.java index 915315f0..3e31c121 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/entity/Board.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/entity/Board.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.*; - import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/capstone/BnagFer/domain/board/entity/BoardBlock.java b/src/main/java/com/capstone/BnagFer/domain/board/entity/BoardBlock.java new file mode 100644 index 00000000..dc112ba9 --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/entity/BoardBlock.java @@ -0,0 +1,30 @@ +package com.capstone.BnagFer.domain.board.entity; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.global.common.BaseEntity; +import jakarta.persistence.*; +import lombok.*; +import java.time.LocalDateTime; +@Entity +@Builder +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@Table(name = "board_block") +public class BoardBlock extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "board_block_id", nullable = false) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "block_user_id") + private User blockUser; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "is_blocked_user_id") + private User isBlockedUser; + + @Column(name = "blocked_at") + private LocalDateTime blockedAt; +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardBlockRepository.java b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardBlockRepository.java new file mode 100644 index 00000000..7f9f09fd --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardBlockRepository.java @@ -0,0 +1,19 @@ +package com.capstone.BnagFer.domain.board.repository; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.board.entity.BoardBlock; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +public interface BoardBlockRepository extends JpaRepository { + boolean existsByBlockUserAndIsBlockedUser(User blockUser, User isBlockedUser); + + void deleteByBlockUserAndIsBlockedUser(User blockUser, User isBlockedUser); + + List findByBlockUser(User blockUser); + + @Query("SELECT b.isBlockedUser.id FROM BoardBlock b WHERE b.blockUser.id = :blockUserId") + List findIsBlockUserIdsByBlockUserId(Long blockUserId); +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardLikeRepository.java b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardLikeRepository.java index 7ee41f02..d035f9a0 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardLikeRepository.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardLikeRepository.java @@ -11,4 +11,5 @@ @Repository public interface BoardLikeRepository extends JpaRepository { Optional findByUserAndBoard(User user, Board board); + Long countByBoard(Board board); } diff --git a/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardRepository.java b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardRepository.java index 3d4fd10d..75a026eb 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardRepository.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/repository/BoardRepository.java @@ -12,4 +12,7 @@ public interface BoardRepository extends JpaRepository { Page findAll(Pageable pageable); Page findByUser(User user, Pageable pageable); + + Page findByUserId(Long userId, Pageable pageable); + } diff --git a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockQueryService.java b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockQueryService.java new file mode 100644 index 00000000..eb55e715 --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockQueryService.java @@ -0,0 +1,26 @@ +package com.capstone.BnagFer.domain.board.service; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.board.dto.response.GetMyBoardBlockResponseDto; +import com.capstone.BnagFer.domain.board.entity.BoardBlock; +import com.capstone.BnagFer.domain.board.repository.BoardBlockRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class BoardBlockQueryService { + private final BoardBlockRepository boardBlockRepository; + public List getBlockedUsers(User user) { + List blockedUsers = boardBlockRepository.findByBlockUser(user); + return blockedUsers.stream() + .map(GetMyBoardBlockResponseDto::from) + .collect(Collectors.toList()); + } + +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockService.java b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockService.java new file mode 100644 index 00000000..1e579a8d --- /dev/null +++ b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardBlockService.java @@ -0,0 +1,47 @@ +package com.capstone.BnagFer.domain.board.service; + +import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.accounts.repository.UserJpaRepository; +import com.capstone.BnagFer.domain.board.dto.request.BoardBlockRequestDto; +import com.capstone.BnagFer.domain.board.dto.response.BoardBlockResponseDto; +import com.capstone.BnagFer.domain.board.entity.BoardBlock; +import com.capstone.BnagFer.domain.board.exception.BoardExceptionHandler; +import com.capstone.BnagFer.domain.board.repository.BoardBlockRepository; +import com.capstone.BnagFer.global.common.ErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +public class BoardBlockService { + private final BoardBlockRepository boardBlockRepository; + private final UserJpaRepository userRepository; + + public BoardBlockResponseDto blockUser(User blockUser, Long isBlockedUserId) { + User blockedUser = userRepository.findById(isBlockedUserId) + .orElseThrow(() -> new BoardExceptionHandler(ErrorCode.USER_NOT_FOUND)); + if (blockUser.getId().equals(isBlockedUserId)) { + throw new BoardExceptionHandler(ErrorCode.CANNOT_REPORT_YOURSELF); + } + if(boardBlockRepository.existsByBlockUserAndIsBlockedUser(blockUser, blockedUser)){ + throw new BoardExceptionHandler(ErrorCode.ALREADY_BLOCKED); + } + BoardBlockRequestDto request = new BoardBlockRequestDto(); + BoardBlock boardBlock = request.toEntity(blockUser, blockedUser); + boardBlockRepository.save(boardBlock); + return BoardBlockResponseDto.from(boardBlock); + } + + public void unblockUser(User blockUser, Long isBlockedUserId) { + User blockedUser = userRepository.findById(isBlockedUserId) + .orElseThrow(() -> new BoardExceptionHandler(ErrorCode.USER_NOT_FOUND)); + if (blockUser.getId().equals(isBlockedUserId)) { + throw new BoardExceptionHandler(ErrorCode.CANNOT_UNBLOCK_YOURSELF); + } + boardBlockRepository.deleteByBlockUserAndIsBlockedUser(blockUser, blockedUser); + } + + +} diff --git a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardQueryService.java b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardQueryService.java index 44493e48..be92438a 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardQueryService.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardQueryService.java @@ -6,16 +6,22 @@ import com.capstone.BnagFer.domain.board.dto.response.BoardDetailResponseDto; import com.capstone.BnagFer.domain.board.dto.response.BoardListDto; import com.capstone.BnagFer.domain.board.entity.Board; +import com.capstone.BnagFer.domain.board.entity.Comment; import com.capstone.BnagFer.domain.board.exception.BoardExceptionHandler; +import com.capstone.BnagFer.domain.board.repository.BoardBlockRepository; 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.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor @Transactional(readOnly = true) @@ -24,9 +30,23 @@ public class BoardQueryService { private final BoardRepository boardRepository; private final RedisUtil redisUtil; private final UserJpaRepository userJpaRepository; - public Page getBoards(Pageable pageable) { + private final BoardBlockRepository boardBlockRepository; + + public Page getBoards(User user, Pageable pageable) { + // 모든 게시글을 조회 Page boards = boardRepository.findAll(pageable); - return boards.map(BoardListDto::from); + + // 사용자가 차단한 다른 사용자들의 ID 목록을 가져온다 + List blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId()); + + // 차단한 사용자의 게시글이 제외된 리스트 생성 + List filteredBoards = boards.getContent().stream() + .filter(board -> !blockedUserIds.contains(board.getUser().getId()) || board.getUser().getId().equals(user.getId())) + .map(BoardListDto::from) + .collect(Collectors.toList()); + + // 필터링된 리스트를 Page 객체로 변환하여 반환 + return new PageImpl<>(filteredBoards, pageable, boards.getTotalElements()); } public Page getMyBoards(User user, Pageable pageable) { @@ -34,31 +54,52 @@ public Page getMyBoards(User user, Pageable pageable) { return boards.map(BoardListDto::from); } - public Page getUserBoards(Long userId, Pageable pageable) { - User user = userJpaRepository.findById(userId) - .orElseThrow(() -> new AccountsExceptionHandler(ErrorCode.USER_NOT_FOUND)); - Page boards = getBoardsByUser(user, pageable); + public Page getUserBoards(User user, Long userId, Pageable pageable) { + User targetedUser = userJpaRepository.findById(userId).orElseThrow(() -> new AccountsExceptionHandler(ErrorCode.USER_NOT_FOUND)); + // 사용자가 차단한 다른 사용자들의 ID 목록을 가져온다 + List blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId()); + + // 만약 조회하려는 사용자가 차단된 사용자 목록에 있으면 예외를 던진다 + if (blockedUserIds.contains(targetedUser.getId())) { + throw new BoardExceptionHandler(ErrorCode.IS_BLOCKED_USER); + } + Page boards = getBoardsByUserId(targetedUser.getId(), pageable); return boards.map(BoardListDto::from); } - public BoardDetailResponseDto getBoard(Long boardId) { + public BoardDetailResponseDto getBoard(User user, Long boardId) { Board board = boardRepository.findById(boardId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.BOARD_NOT_FOUND)); 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); + List filteredComments = board.getComments().stream() + .filter(comment -> !isUserBlocked(user, comment.getUser())) + .collect(Collectors.toList()); + + return BoardDetailResponseDto.from(board, likeCount, commentCount, filteredComments); } public Page getBoardsByUser(User user, Pageable pageable) { return boardRepository.findByUser(user, pageable); } + + // 특정 사용자 ID로 게시글을 조회하는 메소드 + public Page getBoardsByUserId(Long userId, Pageable pageable) { + return boardRepository.findByUserId(userId, pageable); + } + + public boolean isUserBlocked(User blockUser, User isBlockedUser) { + return boardBlockRepository.existsByBlockUserAndIsBlockedUser(blockUser, isBlockedUser); + } + + } diff --git a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardService.java b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardService.java index 616cc8c6..0c237276 100644 --- a/src/main/java/com/capstone/BnagFer/domain/board/service/BoardService.java +++ b/src/main/java/com/capstone/BnagFer/domain/board/service/BoardService.java @@ -104,7 +104,15 @@ public ApiResponse likeButton(Long boardId, User user) { Optional like = boardLikeRepository.findByUserAndBoard(user, board); - long likeCount = redisUtil.boardGetLikeCount(boardId); + Long likeCountFromRedis = redisUtil.boardGetLikeCount(boardId); + long likeCount; + + if (likeCountFromRedis == null) { + likeCount = boardLikeRepository.countByBoard(board); + redisUtil.boardSaveLikeCount(boardId, likeCount); + } else { + likeCount = likeCountFromRedis; + } if (like.isPresent()) { boardLikeRepository.delete(like.get()); @@ -119,21 +127,43 @@ public ApiResponse likeButton(Long boardId, User user) { } } +// public CommentResponseDto createComment(Long boardId, CreateCommentRequestDto request, User user, Long parentCommentId) { +// Board board = boardRepository.findById(boardId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.BOARD_NOT_FOUND)); +// long commentCount = redisUtil.boardGetCommentCount(boardId); +// Comment parent = null; +// if (parentCommentId != null) { +// parent = boardCommentRepository.findById(parentCommentId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.COMMENT_NOT_FOUND)); +// } +//// if(user.getIsBlocked()) { +//// throw new BoardExceptionHandler(ErrorCode.COMMENT_NOT_FOUND); +//// } +// Comment comment = request.toEntity(user, board, parent); +// accountsCommonService.checkUserProfile(user); +// boardCommentRepository.save(comment); +// commentCount++; +// redisUtil.boardSaveCommentCount(boardId, commentCount); +// return CommentResponseDto.from(comment); +// } + public CommentResponseDto createComment(Long boardId, CreateCommentRequestDto request, User user, Long parentCommentId) { Board board = boardRepository.findById(boardId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.BOARD_NOT_FOUND)); - long commentCount = redisUtil.boardGetCommentCount(boardId); + + // 기본값 설정을 위한 수정된 부분 + Long commentCountObj = redisUtil.boardGetCommentCount(boardId); + long commentCount = (commentCountObj != null) ? commentCountObj : 0L; + Comment parent = null; if (parentCommentId != null) { parent = boardCommentRepository.findById(parentCommentId).orElseThrow(() -> new BoardExceptionHandler(ErrorCode.COMMENT_NOT_FOUND)); } -// if(user.getIsBlocked()) { -// throw new BoardExceptionHandler(ErrorCode.COMMENT_NOT_FOUND); -// } + Comment comment = request.toEntity(user, board, parent); accountsCommonService.checkUserProfile(user); boardCommentRepository.save(comment); + commentCount++; redisUtil.boardSaveCommentCount(boardId, commentCount); + return CommentResponseDto.from(comment); } diff --git a/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java b/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java index 36dddb27..8d03cd21 100644 --- a/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java +++ b/src/main/java/com/capstone/BnagFer/global/common/ErrorCode.java @@ -87,6 +87,10 @@ public enum ErrorCode implements BaseErrorCode { //Board 관련 에러 BOARD_NOT_FOUND(HttpStatus.BAD_REQUEST, "BOARD401", "게시글이 없습니다."), TOO_MUCH_IMAGE(HttpStatus.BAD_REQUEST, "BOARD402", "사진은 최대 10개까지 첨부 가능합니다."), + CANNOT_BLOCK_YOURSELF(HttpStatus.BAD_REQUEST, "BOARD403", "자신을 차단할 수 없습니다."), + IS_BLOCKED_USER(HttpStatus.BAD_REQUEST, "BOARD404", "차단한 사용자입니다."), + ALREADY_BLOCKED(HttpStatus.BAD_REQUEST, "BOARD405", "이미 차단된 사용자입니다."), + CANNOT_UNBLOCK_YOURSELF(HttpStatus.BAD_REQUEST, "BOARD406", "자신을 차단해제할 수 없습니다."), //Report 관련 에러 RESTRICTED_CONTENT(HttpStatus.BAD_REQUEST, "REPORT401", "제한된 게시글입니다."),