diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java index feef70c9..90b09641 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java @@ -1,6 +1,10 @@ package com.capstone.BnagFer.domain.tactic.controller; import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.board.dto.response.BoardBlockResponseDto; +import com.capstone.BnagFer.domain.board.dto.response.GetMyBoardBlockResponseDto; +import com.capstone.BnagFer.domain.board.service.BoardBlockQueryService; +import com.capstone.BnagFer.domain.board.service.BoardBlockService; import com.capstone.BnagFer.domain.tactic.dto.*; import com.capstone.BnagFer.domain.tactic.service.TacticQueryService; import com.capstone.BnagFer.domain.tactic.service.TacticService; @@ -16,6 +20,8 @@ import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequiredArgsConstructor @Tag(name = "전술 API") @@ -24,14 +30,17 @@ public class TacticController { private final TacticService tacticService; private final TacticQueryService tacticQueryService; + private final BoardBlockService boardBlockService; + private final BoardBlockQueryService boardBlockQueryService; @Operation(summary = "전술 목록 조회", description = "전체 전술 목록을 조회합니다. 단, 공개(anonymous가 false) 인 전술만 조회. 페이징 적용, 생성 날짜 기준 내림차순 정렬.") @GetMapping public ApiResponse> getTacticList( @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "10") int size) { + @RequestParam(defaultValue = "10") int size, + @LoginUser User user) { Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page tacticLists = tacticQueryService.getTactics(pageable); + Page tacticLists = tacticQueryService.getTactics(user, pageable); return ApiResponse.onSuccess(tacticLists); } @@ -58,9 +67,10 @@ public ApiResponse> getUserTactic( public ApiResponse> searchTitle( @RequestParam(value = "title", required = false) String title, @RequestParam(defaultValue = "0") int page, - @RequestParam(defaultValue = "10") int size) { + @RequestParam(defaultValue = "10") int size, + @LoginUser User user) { Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page tacticLists = tacticQueryService.searchByTitle(title, pageable); + Page tacticLists = tacticQueryService.searchByTitle(title, user, pageable); return ApiResponse.onSuccess(tacticLists); } @@ -133,4 +143,24 @@ public ApiResponse deleteComment(@PathVariable(name = "commentId") Long public ApiResponse likeToggle(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) { return tacticService.likeButton(tacticId, user); } + + @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/tactic/dto/TacticDetailResponse.java b/src/main/java/com/capstone/BnagFer/domain/tactic/dto/TacticDetailResponse.java index a6c2a13c..fecfb88a 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/dto/TacticDetailResponse.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/dto/TacticDetailResponse.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Builder @@ -55,7 +56,7 @@ public record AllTacticList(Long tacticId, String mainFormation, Long likeCnt, Long commentCnt){ - public static AllTacticList from(Tactic tactic){ + public static AllTacticList from(Tactic tactic, Long likeCnt, Long commentCnt){ return TacticDetailResponse.AllTacticList.builder() .tacticId(tactic.getTacticId()) .userId(tactic.getUser().getId()) @@ -63,13 +64,15 @@ public static AllTacticList from(Tactic tactic){ .tacticName(tactic.getTacticName()) .anonymous(tactic.isAnonymous()) .mainFormation(tactic.getMainFormation()) - .likeCnt((long) tactic.getLikes().size()) - .commentCnt((long) tactic.getComments().size()) + .likeCnt(likeCnt) + .commentCnt(commentCnt) .build(); } - public static List from(List tactics){ - return tactics.stream().map(TacticDetailResponse.AllTacticList::from).collect(Collectors.toList()); + public static List from(List tactics, Map likeCnt, Map commentCnt) { + return tactics.stream() + .map(tactic -> from(tactic, likeCnt.getOrDefault(tactic.getTacticId(), 0L), commentCnt.getOrDefault(tactic.getTacticId(), 0L))) + .collect(Collectors.toList()); } } diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java index 15287983..f084d706 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java @@ -1,6 +1,9 @@ package com.capstone.BnagFer.domain.tactic.service; import com.capstone.BnagFer.domain.accounts.entity.User; +import com.capstone.BnagFer.domain.board.dto.response.BoardListDto; +import com.capstone.BnagFer.domain.board.entity.Board; +import com.capstone.BnagFer.domain.board.repository.BoardBlockRepository; import com.capstone.BnagFer.global.util.RedisUtil; import com.capstone.BnagFer.domain.tactic.dto.TacticDetailResponse; import com.capstone.BnagFer.domain.tactic.dto.TacticResponse; @@ -10,10 +13,16 @@ import com.capstone.BnagFer.global.common.ErrorCode; 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.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor @Transactional(readOnly = true) @@ -21,10 +30,38 @@ public class TacticQueryService { private final TacticRepository tacticRepository; private final RedisUtil redisUtil; + private final BoardBlockRepository boardBlockRepository; - public Page getTactics(Pageable pageable) { + public Page getTactics(User user, Pageable pageable) { Page tactics = tacticRepository.findAllByAnonymousFalse(pageable); - return tactics.map(TacticDetailResponse.AllTacticList::from); + + List blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId()); + + Map likeCounts = new HashMap<>(); + Map commentCounts = new HashMap<>(); + + List filteredTactics = tactics.getContent().stream() + .filter(tactic -> !blockedUserIds.contains(tactic.getUser().getId()) || tactic.getUser().getId().equals(user.getId())) + .map(tactic -> { + Long likeCount = redisUtil.getLikeCount(tactic.getTacticId()); + if (likeCount == null) { + likeCount = (long) tactic.getLikes().size(); + redisUtil.saveLikeCount(tactic.getTacticId(), likeCount); + } + likeCounts.put(tactic.getTacticId(), likeCount); + + Long commentCount = redisUtil.getCommentCount(tactic.getTacticId()); + if (commentCount == null) { + commentCount = (long) tactic.getComments().size(); + redisUtil.saveCommentCount(tactic.getTacticId(), commentCount); + } + commentCounts.put(tactic.getTacticId(), commentCount); + + return TacticDetailResponse.AllTacticList.from(tactic, likeCount, commentCount); + }) + .collect(Collectors.toList()); + + return new PageImpl<>(filteredTactics, pageable, tactics.getTotalElements()); } public Page getUserTactics(User user, Pageable pageable) { @@ -52,10 +89,18 @@ public TacticDetailResponse getTacticById(Long tacticId) { return TacticDetailResponse.from(tactic, likeCount, commentCount); } - public Page searchByTitle(String title, Pageable pageable) { + public Page searchByTitle(String title, User user, Pageable pageable) { if (title == null) title = ""; Page byTitleContaining = tacticRepository.findByTacticNameContainingAndAnonymousFalse(title, pageable); - return byTitleContaining.map(TacticResponse.TacticList::from); + + List blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId()); + + List filteredBoards = byTitleContaining.getContent().stream() + .filter(tactic -> !blockedUserIds.contains(tactic.getUser().getId()) || tactic.getUser().getId().equals(user.getId())) + .map(TacticResponse.TacticList::from) + .toList(); + + return new PageImpl<>(filteredBoards, pageable, byTitleContaining.getTotalElements()); } }