Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
@@ -0,0 +1,40 @@
package com.somemore.community.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.somemore.community.domain.CommunityBoard;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.UUID;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Schema(description = "커뮤니티 게시글 상세 조회 응답 DTO")
public record CommunityBoardDetailResponseDto(
@Schema(description = "커뮤니티 게시글 ID", example = "12")
Long id,
@Schema(description = "작성자(봉사자) ID", example = "123e4567-e89b-12d3-a456-426614174000")
UUID writerId,
@Schema(description = "커뮤니티 게시글 제목", example = "11/29 OO도서관 봉사 같이 갈 사람 모집합니다.")
String title,
@Schema(description = "커뮤니티 게시글 내용", example = "저 포함 5명이 같이 가면 좋을 거 같아요")
String content,
@Schema(description = "이미지 URL", example = "https://image.domain.com/links")
String imageUrl,
@Schema(description = "커뮤니티 게시글 생성 일시", example = "2023-12-02T11:00:00")
LocalDateTime createdAt,
@Schema(description = "커뮤니티 게시글 수정 일시", example = "2023-12-02T11:00:00")
LocalDateTime updatedAt
) {
public static CommunityBoardDetailResponseDto from(CommunityBoard board) {
return new CommunityBoardDetailResponseDto(
board.getId(),
board.getWriterId(),
board.getTitle(),
board.getContent(),
board.getImgUrl(),
board.getCreatedAt(),
board.getUpdatedAt()
);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.somemore.community.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.somemore.community.repository.mapper.CommunityBoardView;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;

@JsonNaming(SnakeCaseStrategy.class)
@Schema(description = "커뮤니티 게시글 목록 조회 응답 DTO")
public record CommunityBoardResponseDto(
@Schema(description = "커뮤니티 게시글 ID", example = "12")
Long id,
@Schema(description = "커뮤니티 게시글 ID", example = "12")
String title,
@Schema(description = "작성자(봉사자) ID", example = "123e4567-e89b-12d3-a456-426614174000")
String writerNickname,
@Schema(description = "커뮤니티 게시글 생성 일시", example = "2023-12-02T11:00:00")
LocalDateTime createdAt
) {
public static CommunityBoardResponseDto from(CommunityBoardView board) {
return new CommunityBoardResponseDto(
board.communityBoard().getId(),
board.communityBoard().getTitle(),
board.writerNickname(),
board.communityBoard().getCreatedAt()
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,40 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.somemore.community.repository.mapper.CommunityCommentView;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record CommunityCommentResponseDto(
@Schema(description = "커뮤니티 댓글 ID", example = "1234")
Long id,
@Schema(description = "작성자(봉사자) 닉네임", example = "dkdudab")
String writerNickname,
@Schema(description = "커뮤니티 댓글 내용", example = "저도 함께 하고 싶습니다.")
String content,
@Schema(description = "커뮤니티 댓글 수정 일시", example = "2023-12-02T11:00:00")
LocalDateTime updatedAt,
@Schema(description = "대댓글 목록", example = """
[
{
"id": 3,
"content": "첫 번째 댓글의 답글입니다.",
"writerNickname": "사용자2",
"createdAt": "2023-12-02T11:00:00",
"replies": []
}
]
""")
List<CommunityCommentResponseDto> replies
) {
public 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
Loading