Skip to content

Commit

Permalink
Merge pull request #44 from DEPthes/develop
Browse files Browse the repository at this point in the history
[DEPLOY]
  • Loading branch information
jisujeong0 authored Aug 17, 2024
2 parents 7ceb6f6 + 6510741 commit e31b088
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

@Repository
public interface BoardLikeRepository extends JpaRepository<BoardLike, Long> {
Expand Down Expand Up @@ -44,4 +45,6 @@ Page<BoardLike> findPagesByUserAndStatusAndBoardFieldsContaining(
Pageable pageable
);

@Query("SELECT b.board.id FROM BoardLike b WHERE b.user.id = :userId AND b.status = :status")
Set<Long> findLikedBoardIdsByUserId(@Param("userId") Long userId, @Param("status") Status status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ public ResponseEntity<?> hitTheThemeLikeButton(@CurrentUser CustomUserDetails cu

@GetMapping("/list")
public ResponseEntity<?> getThemeList(
@CurrentUser CustomUserDetails customUserDetails,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "3") int size,
@RequestParam(defaultValue = "date") String sortBy) {
Pageable pageable = PageRequest.of(page - 1, size);
return themeService.getThemeList(pageable, sortBy);
return themeService.getThemeList(customUserDetails, pageable, sortBy);
}

@GetMapping("/search")
public ResponseEntity<?> searchTheme(
@CurrentUser CustomUserDetails customUserDetails,
@RequestParam String keyword,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "3") int size){
Pageable pageable = PageRequest.of(page - 1, size);
return themeService.searchTheme(keyword, pageable);
return themeService.searchTheme(customUserDetails, keyword, pageable);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ public static class BoardRes {
private String nickname; // 작성자 닉네임
private String date; // 게시글 작성일
private int likeCount; // 게시글 좋아요 수
private boolean likedBoard; // 게시글 좋아요 여부

@Builder
public BoardRes(Long boardId, String title, String content, String nickname, LocalDateTime date, int likeCount){
public BoardRes(Long boardId, String title, String content,
String nickname, LocalDateTime date, int likeCount, boolean likedBoard){
this.boardId = boardId;
this.title = title;
this.content = content;
this.nickname = nickname;
this.date = date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
this.likeCount =likeCount;
this.likedBoard = likedBoard;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
package depth.mvp.ns.domain.theme.dto.response;

import depth.mvp.ns.global.payload.PageInfo;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Getter
public class ThemeListRes {
private Long themeId; // 주제ID
private String content; // 주제 내용
private String date; // 주제 발행일
private int likeCount; // 주제 좋아요수
private int boardCount; // 게시글 수
private Long userId; // 회원ID
private PageInfo pageInfo; // 페이지 정보
private List<ThemeList> themeList; // 주제 목록

@Builder
ThemeListRes(Long themeId, String content, LocalDate date, int likeCount, int boardCount){
this.themeId = themeId;
this.content = content;
this.date = date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
this.likeCount = likeCount;
this.boardCount = boardCount;
public ThemeListRes(Long userId, PageInfo pageInfo, List<ThemeList> themeList){
this.userId = userId;
this.pageInfo = pageInfo;
this.themeList = themeList;
}

@Getter
public static class ThemeList {
private Long themeId; // 주제ID
private String content; // 주제 내용
private String date; // 주제 발행일
private int likeCount; // 주제 좋아요수
private int boardCount; // 게시글 수
private boolean likedTheme; // 주제 좋아요 여부

@Builder
public ThemeList(Long themeId, String content, LocalDate date, int likeCount, int boardCount, boolean likedTheme) {
this.themeId = themeId;
this.content = content;
this.date = date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
this.likeCount = likeCount;
this.boardCount = boardCount;
this.likedTheme = likedTheme;
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

@Getter
public class TodayThemeRes {
private Long themeId; // 주제ID
private String content; // 오늘의 주제
private Long userId; // 사용자ID
private boolean likedTheme; // 주제 좋아요 여부

@Builder
public TodayThemeRes(String content, Long userId, boolean likedTheme){
public TodayThemeRes(Long themeId,String content, Long userId, boolean likedTheme){
this.themeId = themeId;
this.content = content;
this.userId = userId;
this.likedTheme = likedTheme;
Expand Down
65 changes: 40 additions & 25 deletions src/main/java/depth/mvp/ns/domain/theme/service/ThemeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import depth.mvp.ns.domain.board.domain.Board;
import depth.mvp.ns.domain.board.domain.repository.BoardRepository;
import depth.mvp.ns.domain.board_like.domain.repository.BoardLikeRepository;
import depth.mvp.ns.domain.common.Status;
import depth.mvp.ns.domain.user.dto.response.PageRes;
import depth.mvp.ns.domain.user_point.domain.UserPoint;
import depth.mvp.ns.domain.user_point.domain.repository.UserPointRepository;
import depth.mvp.ns.domain.theme.domain.Theme;
Expand Down Expand Up @@ -35,9 +35,8 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -47,6 +46,7 @@ public class ThemeService {
private final ThemeLikeRepository themeLikeRepository;
private final UserRepository userRepository;
private final BoardRepository boardRepository;
private final BoardLikeRepository boardLikeRepository;
private final UserPointRepository userPointRepository;

// 오늘의 주제 조회
Expand All @@ -66,6 +66,7 @@ public ResponseEntity<?> getTodayTheme(@CurrentUser CustomUserDetails customUser
}

TodayThemeRes todayThemeRes = TodayThemeRes.builder()
.themeId(theme.getId())
.content(theme.getContent())
.userId(userId)
.likedTheme(likedTheme)
Expand Down Expand Up @@ -168,31 +169,38 @@ public ResponseEntity<?> getThemeDetail(Long themeId, String sortBy, Pageable pa
throw new InvalidParameterException(errors);
}

// 회원인지 여부에 따른 처리
Long userId = null;
boolean likedTheme = false; // 주제 좋아요 여부
Set<Long> likedBoardIds = Collections.emptySet(); // 사용자가 좋아요한 게시물 ID 목록

// 주제에 대한 좋아요 여부 확인하고 응답값 넘겨주기
if(customUserDetails != null){
User user = validateUser(customUserDetails.getId());
userId = user.getId();
likedTheme = themeLikeRepository.existsByThemeAndUserAndStatus(theme, user, Status.ACTIVE);
likedBoardIds = boardLikeRepository.findLikedBoardIdsByUserId(userId, Status.ACTIVE);
}

// set->list 변경
List<Long> likedBoardIdList = new ArrayList<>(likedBoardIds);

// 게시물 응답 목록
List<ThemeDetailRes.BoardRes> boardResList = boardPage.getContent().stream()
.map(board -> {
int likeCount = boardRepository.countLikesByBoardId(board.getId()); // 게시글 좋아요 수 계산

boolean likedBoard = likedBoardIdList.contains(board.getId());
return ThemeDetailRes.BoardRes.builder()
.boardId(board.getId())
.title(board.getTitle())
.content(board.getContent())
.nickname(board.getUser().getNickname())
.date(board.getCreatedDate())
.likeCount(likeCount)
.likedBoard(likedBoard)
.build();
}).collect(Collectors.toList());

// 회원인지 여부에 따른 처리
Long userId = null;
boolean likedTheme = false; // 주제 좋아요 여부

// 주제에 대한 좋아요 여부 확인하고 응답값 넘겨주기
if(customUserDetails != null){
User user = validateUser(customUserDetails.getId());
userId = user.getId();
likedTheme = themeLikeRepository.existsByThemeAndUserAndStatus(theme, user, Status.ACTIVE);
}

// 페이지 정보 생성
PageInfo pageInfo = PageInfo.builder()
.pageNumber(boardPage.getNumber() + 1) //페이지 번호 1부터 시작
Expand Down Expand Up @@ -221,7 +229,7 @@ public ResponseEntity<?> getThemeDetail(Long themeId, String sortBy, Pageable pa
}

// 주제 목록 조회
public ResponseEntity<?> getThemeList(Pageable pageable, String sortBy) {
public ResponseEntity<?> getThemeList(CustomUserDetails customUserDetails, Pageable pageable, String sortBy) {
Page<Theme> themePage;

switch (sortBy) {
Expand All @@ -240,28 +248,34 @@ public ResponseEntity<?> getThemeList(Pageable pageable, String sortBy) {
throw new InvalidParameterException(errors);
}

return buildThemeListResponse(themePage);
return buildThemeListResponse(themePage, customUserDetails);
}

// 주제 검색
public ResponseEntity<?> searchTheme(String keyword,Pageable pageable) {
public ResponseEntity<?> searchTheme(CustomUserDetails customUserDetails, String keyword,Pageable pageable) {
Page<Theme> themePage = themeRepository.findByContentContaining(keyword, pageable);
return buildThemeListResponse(themePage);
return buildThemeListResponse(themePage, customUserDetails);
}

// 주제 목록 처리 & 응답을 반환하는 메소드
private ResponseEntity<ApiResponse> buildThemeListResponse(Page<Theme> themePage) {
List<ThemeListRes> themeListRes = themePage.getContent().stream()
private ResponseEntity<ApiResponse> buildThemeListResponse(Page<Theme> themePage, CustomUserDetails customUserDetails) {
final User user = customUserDetails != null ? validateUser(customUserDetails.getId()) : null;
final Long userId = user != null ? user.getId() : null;

List<ThemeListRes.ThemeList> themeList = themePage.getContent().stream()
.map(theme -> {
int boardCount = themeRepository.countBoardsByThemeId(theme.getId()); // 게시글 수 계산
int likeCount = themeRepository.countLikesByThemeId(theme.getId()); // 주제 좋아요 수 계산
// 주제에 대해 사용자가 좋아요를 눌렀는지 확인
boolean likedTheme = user != null && themeLikeRepository.existsByThemeAndUserAndStatus(theme, user, Status.ACTIVE);

return ThemeListRes.builder()
return ThemeListRes.ThemeList.builder()
.themeId(theme.getId())
.content(theme.getContent())
.date(theme.getDate())
.likeCount(likeCount)
.boardCount(boardCount)
.likedTheme(likedTheme)
.build();
}).collect(Collectors.toList());

Expand All @@ -272,14 +286,15 @@ private ResponseEntity<ApiResponse> buildThemeListResponse(Page<Theme> themePage
.totalPages(themePage.getTotalPages())
.build();

PageRes<ThemeListRes> pageRes = PageRes.<ThemeListRes>builder()
ThemeListRes themeListRes = ThemeListRes.builder()
.userId(userId)
.pageInfo(pageInfo)
.resList(themeListRes)
.themeList(themeList)
.build();

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(pageRes)
.information(themeListRes)
.build();

return ResponseEntity.ok(apiResponse);
Expand Down

0 comments on commit e31b088

Please sign in to comment.