Skip to content

Commit

Permalink
[FEATURE] 전술게시판 차단 기능 구현 (#161) (#182)
Browse files Browse the repository at this point in the history
* ✨ feat: 차단, 차단해제, 차단목록 조회 기능 구현 (#161)

* ✨ feat: 차단 로직 적용 및 redis에서 좋아요 및 댓글 수 가져오는 로직으로 변경 (#161)
  • Loading branch information
jiiiiiw authored Aug 25, 2024
1 parent b974b25 commit 14f1812
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -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<Page<TacticDetailResponse.AllTacticList>> 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<TacticDetailResponse.AllTacticList> tacticLists = tacticQueryService.getTactics(pageable);
Page<TacticDetailResponse.AllTacticList> tacticLists = tacticQueryService.getTactics(user, pageable);
return ApiResponse.onSuccess(tacticLists);
}

Expand All @@ -58,9 +67,10 @@ public ApiResponse<Page<TacticResponse.TacticList>> getUserTactic(
public ApiResponse<Page<TacticResponse.TacticList>> 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<TacticResponse.TacticList> tacticLists = tacticQueryService.searchByTitle(title, pageable);
Page<TacticResponse.TacticList> tacticLists = tacticQueryService.searchByTitle(title, user, pageable);
return ApiResponse.onSuccess(tacticLists);
}

Expand Down Expand Up @@ -133,4 +143,24 @@ public ApiResponse<Object> deleteComment(@PathVariable(name = "commentId") Long
public ApiResponse<Object> likeToggle(@PathVariable(name = "tacticId") Long tacticId, @LoginUser User user) {
return tacticService.likeButton(tacticId, user);
}

@Operation(summary = "전술게시판 사용자 차단", description = "차단을 하게되면, 차단한 사용자의 댓글, 게시글을 볼 수 없다.")
@PostMapping("/block/{isBlockedUserId}")
public ApiResponse<BoardBlockResponseDto> 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<Void> unblockUser(@LoginUser User user, @PathVariable(name = "isBlockedUserId") Long isBlockedUserId) {
boardBlockService.unblockUser(user, isBlockedUserId);
return ApiResponse.noContent();
}
@Operation(summary = "내가 차단한 사용자 조회", description = "내가 차단한 사용자를 조회해주는 기능")
@GetMapping("/blocked-users")
public ApiResponse<List<GetMyBoardBlockResponseDto>> getBlockedUsers(@LoginUser User currentUser) {
List<GetMyBoardBlockResponseDto> blockedUsers = boardBlockQueryService.getBlockedUsers(currentUser);
return ApiResponse.onSuccess(blockedUsers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Builder
Expand Down Expand Up @@ -55,21 +56,23 @@ 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())
.nickname(tactic.getUser().getProfile().getNickname())
.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<AllTacticList> from(List<Tactic> tactics){
return tactics.stream().map(TacticDetailResponse.AllTacticList::from).collect(Collectors.toList());
public static List<AllTacticList> from(List<Tactic> tactics, Map<Long, Long> likeCnt, Map<Long, Long> commentCnt) {
return tactics.stream()
.map(tactic -> from(tactic, likeCnt.getOrDefault(tactic.getTacticId(), 0L), commentCnt.getOrDefault(tactic.getTacticId(), 0L)))
.collect(Collectors.toList());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,21 +13,55 @@
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)
public class TacticQueryService {

private final TacticRepository tacticRepository;
private final RedisUtil redisUtil;
private final BoardBlockRepository boardBlockRepository;

public Page<TacticDetailResponse.AllTacticList> getTactics(Pageable pageable) {
public Page<TacticDetailResponse.AllTacticList> getTactics(User user, Pageable pageable) {
Page<Tactic> tactics = tacticRepository.findAllByAnonymousFalse(pageable);
return tactics.map(TacticDetailResponse.AllTacticList::from);

List<Long> blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId());

Map<Long, Long> likeCounts = new HashMap<>();
Map<Long, Long> commentCounts = new HashMap<>();

List<TacticDetailResponse.AllTacticList> 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<TacticResponse.TacticList> getUserTactics(User user, Pageable pageable) {
Expand Down Expand Up @@ -52,10 +89,18 @@ public TacticDetailResponse getTacticById(Long tacticId) {
return TacticDetailResponse.from(tactic, likeCount, commentCount);
}

public Page<TacticResponse.TacticList> searchByTitle(String title, Pageable pageable) {
public Page<TacticResponse.TacticList> searchByTitle(String title, User user, Pageable pageable) {
if (title == null) title = "";

Page<Tactic> byTitleContaining = tacticRepository.findByTacticNameContainingAndAnonymousFalse(title, pageable);
return byTitleContaining.map(TacticResponse.TacticList::from);

List<Long> blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId());

List<TacticResponse.TacticList> 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());
}
}

0 comments on commit 14f1812

Please sign in to comment.