Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record CommunityBoardCreateRequestDto(
@NotBlank(message = "게시글 내용은 필수 값입니다.")
String content
) {
public CommunityBoard toEntity(UUID writerId, String imgUrl) {
public CommunityBoard to(UUID writerId, String imgUrl) {
return CommunityBoard.builder()
.writerId(writerId)
.title(title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public record CommunityCommentCreateRequestDto(
@Nullable
Long parentCommentId
) {
public CommunityComment toEntity(UUID writerId) {
public CommunityComment to(UUID writerId) {
return CommunityComment.builder()
.communityBoardId(communityBoardId)
.writerId(writerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.UUID;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record CommunityBoardGetDetailResponseDto(
public record CommunityBoardDetailResponseDto(
Long id,
UUID writerId,
String title,
Expand All @@ -17,8 +17,8 @@ public record CommunityBoardGetDetailResponseDto(
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
public static CommunityBoardGetDetailResponseDto fromEntity(CommunityBoard board) {
return new CommunityBoardGetDetailResponseDto(
public static CommunityBoardDetailResponseDto from(CommunityBoard board) {
return new CommunityBoardDetailResponseDto(
board.getId(),
board.getWriterId(),
board.getTitle(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import java.time.LocalDateTime;

@JsonNaming(SnakeCaseStrategy.class)
public record CommunityBoardGetResponseDto(
public record CommunityBoardResponseDto(
Long id,
String title,
String writerNickname,
LocalDateTime createdAt
) {
public static CommunityBoardGetResponseDto fromEntity(CommunityBoardView board) {
return new CommunityBoardGetResponseDto(
public static CommunityBoardResponseDto from(CommunityBoardView board) {
return new CommunityBoardResponseDto(
board.communityBoard().getId(),
board.communityBoard().getTitle(),
board.writerNickname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public record CommunityCommentResponseDto(
replies = replies == null ? new ArrayList<>() : replies;
}

public static CommunityCommentResponseDto fromView(CommunityCommentView comment) {
public static CommunityCommentResponseDto from(CommunityCommentView comment) {
return new CommunityCommentResponseDto(
comment.communityComment().getId(),
comment.writerNickname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.repository.mapper.CommunityBoardView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface CommunityBoardRepository {
CommunityBoard save(CommunityBoard communityBoard);
Optional<CommunityBoard> findById(Long id);
List<CommunityBoardView> getCommunityBoards();
List<CommunityBoardView> findByWriterId(UUID writerId);
Page<CommunityBoardView> findCommunityBoards(Pageable pageable);
Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable);
boolean existsById(Long id);
default boolean doesNotExistById(Long id) {
return !existsById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.somemore.community.domain.QCommunityBoard;
import com.somemore.volunteer.domain.QVolunteer;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -21,15 +24,16 @@ public class CommunityBoardRepositoryImpl implements CommunityBoardRepository {
private final JPAQueryFactory queryFactory;
private final CommunityBoardJpaRepository communityBoardJpaRepository;

private static final QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
private static final QVolunteer volunteer = QVolunteer.volunteer;

@Override
public CommunityBoard save(CommunityBoard communityBoard) {
return communityBoardJpaRepository.save(communityBoard);
}

@Override
public Optional<CommunityBoard> findById(Long id) {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;

return Optional.ofNullable(queryFactory
.selectFrom(communityBoard)
.where(communityBoard.id.eq(id)
Expand All @@ -38,18 +42,30 @@ public Optional<CommunityBoard> findById(Long id) {
}

@Override
public List<CommunityBoardView> getCommunityBoards() {
return getCommunityBoardsQuery()
public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
List<CommunityBoardView> content = getCommunityBoardsQuery()
.where(QCommunityBoard.communityBoard.deleted.eq(false))
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(communityBoard.count())
.from(communityBoard);

return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}

@Override
public List<CommunityBoardView> findByWriterId(UUID writerId) {
return getCommunityBoardsQuery()
public Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable) {
List<CommunityBoardView> content = getCommunityBoardsQuery()
.where(QCommunityBoard.communityBoard.writerId.eq(writerId)
.and(QCommunityBoard.communityBoard.deleted.eq(false)))
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(communityBoard.count())
.from(communityBoard);

return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}

@Override
Expand All @@ -58,9 +74,6 @@ public boolean existsById(Long id) {
}

private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
QVolunteer volunteer = QVolunteer.volunteer;

return queryFactory
.select(Projections.constructor(CommunityBoardView.class,
communityBoard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.somemore.community.domain.CommunityComment;
import com.somemore.community.repository.mapper.CommunityCommentView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

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

public interface CommunityCommentRepository {
CommunityComment save(CommunityComment communityComment);
Optional<CommunityComment> findById(Long id);
List<CommunityCommentView> findCommentsByBoardId(Long boardId);
Page<CommunityCommentView> findCommentsByBoardId(Long boardId, Pageable pageable);
boolean existsById(Long id);
default boolean doesNotExistById(Long id) {
return !existsById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.somemore.community.repository.comment;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.somemore.community.domain.CommunityComment;
import com.somemore.community.domain.QCommunityComment;
import com.somemore.community.repository.mapper.CommunityCommentView;
import com.somemore.volunteer.domain.QVolunteer;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -19,27 +23,25 @@ public class CommunityCommentRepositoryImpl implements CommunityCommentRepositor
private final JPAQueryFactory queryFactory;
private final CommunityCommentJpaRepository communityCommentJpaRepository;

private static final QCommunityComment communityComment = QCommunityComment.communityComment;
private static final QVolunteer volunteer = QVolunteer.volunteer;

@Override
public CommunityComment save(CommunityComment communityComment) {
return communityCommentJpaRepository.save(communityComment);
}

@Override
public Optional<CommunityComment> findById(Long id) {
QCommunityComment communityComment = QCommunityComment.communityComment;

return Optional.ofNullable(queryFactory
.selectFrom(communityComment)
.where(communityComment.id.eq(id)
.and(communityComment.deleted.eq(false)))
.fetchOne());
}

public List<CommunityCommentView> findCommentsByBoardId(Long boardId) {
QCommunityComment communityComment = QCommunityComment.communityComment;
QVolunteer volunteer = QVolunteer.volunteer;

return queryFactory
public Page<CommunityCommentView> findCommentsByBoardId(Long boardId, Pageable pageable) {
List<CommunityCommentView> content = queryFactory
.select(Projections.constructor(CommunityCommentView.class,
communityComment,
volunteer.nickname))
Expand All @@ -48,12 +50,16 @@ public List<CommunityCommentView> findCommentsByBoardId(Long boardId) {
.where(communityComment.communityBoardId.eq(boardId))
.orderBy(communityComment.parentCommentId.asc().nullsFirst(), communityComment.createdAt.asc())
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(communityComment.count())
.from(communityComment);

return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}

@Override
public boolean existsById(Long id) {
QCommunityComment communityComment = QCommunityComment.communityComment;

return queryFactory
.selectOne()
.from(communityComment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.repository.mapper.CommunityBoardView;
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardResponseDto;
import com.somemore.community.repository.board.CommunityBoardRepository;
import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.function.Function;

import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;

Expand All @@ -23,29 +24,26 @@
public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {

private final CommunityBoardRepository communityBoardRepository;
private static final int PAGE_SIZE = 10;

@Override
public List<CommunityBoardGetResponseDto> getCommunityBoards() {
List<CommunityBoardView> boards = communityBoardRepository.getCommunityBoards();
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
public Page<CommunityBoardResponseDto> getCommunityBoards(int page) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
Page<CommunityBoardView> boards = communityBoardRepository.findCommunityBoards(pageable);
return boards.map(CommunityBoardResponseDto::from);
}

@Override
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
List<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId);
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
public Page<CommunityBoardResponseDto> getCommunityBoardsByWriterId(UUID writerId, int page) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
Page<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId ,pageable);
return boards.map(CommunityBoardResponseDto::from);
}

@Override
public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) {
public CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id) {
CommunityBoard board = communityBoardRepository.findById(id)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));
return CommunityBoardGetDetailResponseDto.fromEntity(board);
}

private <T, R> List<R> mapEntitiesToDtos(List<T> entities, Function<T, R> mapper) {
return entities.stream()
.map(mapper)
.toList();
return CommunityBoardDetailResponseDto.from(board);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CreateCommunityBoardService implements CreateCommunityBoardUseCase
@Override
public Long createCommunityBoard(CommunityBoardCreateRequestDto requestDto, UUID writerId, String imgUrl) {

CommunityBoard communityBoard = requestDto.toEntity(writerId, imgUrl == null ? "" : imgUrl);
CommunityBoard communityBoard = requestDto.to(writerId, imgUrl == null ? "" : imgUrl);

communityBoardRepository.save(communityBoard);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.somemore.community.repository.mapper.CommunityCommentView;
import com.somemore.community.usecase.comment.CommunityCommentQueryUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -17,12 +21,16 @@
public class CommunityCommentQueryService implements CommunityCommentQueryUseCase {

private final CommunityCommentRepository communityCommentRepository;
private static final int PAGE_SIZE = 4;

@Override
public List<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId) {
List<CommunityCommentView> allComments = communityCommentRepository.findCommentsByBoardId(boardId);
List<CommunityCommentView> filteredComments = filterValidComments(allComments);
return createCommentHierarchy(filteredComments);
public Page<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId, int page) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
Page<CommunityCommentView> commentPage = communityCommentRepository.findCommentsByBoardId(boardId, pageable);
List<CommunityCommentView> filteredComments = filterValidComments(commentPage.getContent());
List<CommunityCommentResponseDto> hierarchicalComments = createCommentHierarchy(filteredComments);

return new PageImpl<>(hierarchicalComments, pageable, filteredComments.size());
}

private List<CommunityCommentView> filterValidComments(List<CommunityCommentView> comments) {
Expand Down Expand Up @@ -60,7 +68,7 @@ private List<CommunityCommentResponseDto> createCommentHierarchy(List<CommunityC
List<CommunityCommentResponseDto> rootComments = new ArrayList<>();

for (CommunityCommentView comment : comments) {
CommunityCommentResponseDto dto = CommunityCommentResponseDto.fromView(comment);
CommunityCommentResponseDto dto = CommunityCommentResponseDto.from(comment);
commentMap.put(dto.id(), dto);

Long parentCommentId = comment.communityComment().getParentCommentId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CreateCommunityCommentService implements CreateCommunityCommentUseC

@Override
public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto, UUID writerId) {
CommunityComment communityComment = requestDto.toEntity(writerId);
CommunityComment communityComment = requestDto.to(writerId);

validateCommunityBoardExists(communityComment.getCommunityBoardId());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.somemore.community.usecase.board;

import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardResponseDto;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.UUID;

public interface CommunityBoardQueryUseCase {
List<CommunityBoardGetResponseDto> getCommunityBoards();
List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId);
CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id);
Page<CommunityBoardResponseDto> getCommunityBoards(int page);
Page<CommunityBoardResponseDto> getCommunityBoardsByWriterId(UUID writerId, int page);
CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id);
}
Loading