Skip to content

Commit

Permalink
[FEAT] MyPage 본인이 작성한 게시글 및 댓글 조회, docker volume 설정 (#132)
Browse files Browse the repository at this point in the history
* feat: Mypage 게시글 필터링

* style: PostController 엔드포인트 myPage로 변경

* feat: MyPage 댓글 조회 Dto 생성

* style: Mapping Url /my-page 변경, UserIdExpression -> AuthorIdExpression

* feat: MyPage 댓글 조회(offset기반 페이지네이션) 구현

* chore: ddl-auto create에서 update로 변경

* chore: maria, redis 명명된 volume 설정

* refactor: MyPage 작성한 게시글 조회 함수 변경

* chore: Application 재실행 시 data.sql 실행 X

* style: 필드명 변경

* feat: 마이페이지 댓글 조회 (1게시글 1댓글)
  • Loading branch information
injae-348 authored Sep 8, 2024
1 parent 33d6e4a commit a75afb3
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 17 deletions.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ services:
MYSQL_DATABASE: ${MARIA_DATABASE}
MYSQL_USER: ${MARIA_USER}
MYSQL_PASSWORD: ${MARIA_PASSWORD}
volumes:
- mariadb_data:/var/lib/mysql # 명명된 볼륨 사용

command:
[
"--character-set-server=utf8mb4",
Expand All @@ -27,6 +30,8 @@ services:
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD}
REDIS_HOST: ${REDIS_HOST}
volumes:
- redis_data:/data # 명명된 볼륨을 사용
ports:
- "${REDIS_PORT}:${REDIS_PORT}"
restart: always
Expand All @@ -42,3 +47,6 @@ services:
- mariadb
- redis

volumes:
mariadb_data:
redis_data:
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import econo.buddybridge.comment.dto.CommentCustomPage;
import econo.buddybridge.comment.dto.CommentReqDto;
import econo.buddybridge.comment.dto.MyPageCommentCustomPage;
import econo.buddybridge.comment.service.CommentService;
import econo.buddybridge.common.annotation.AllowAnonymous;
import econo.buddybridge.post.entity.PostType;
import econo.buddybridge.utils.api.ApiResponse;
import econo.buddybridge.utils.api.ApiResponse.CustomBody;
import econo.buddybridge.utils.api.ApiResponseGenerator;
Expand All @@ -13,15 +15,7 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
Expand All @@ -31,6 +25,20 @@ public class CommentController {

private final CommentService commentService;

@Operation(summary = "내가 작성한 댓글 조회", description = "내가 작성한 게시글의 댓글을 조회합니다.")
@GetMapping("/my-page")
public ApiResponse<CustomBody<MyPageCommentCustomPage>> getCommentsMyPage(
@RequestParam("page") Integer page,
@RequestParam("size") Integer size,
@RequestParam(defaultValue = "DESC", required = false) String sort,
@RequestParam(value = "post-type", required = false) PostType postType,
HttpServletRequest request
) {
Long memberId = SessionUtils.getMemberId(request);
MyPageCommentCustomPage comments = commentService.getMyPageComments(memberId, page, size, sort, postType);
return ApiResponseGenerator.success(comments, HttpStatus.OK);
}

@Operation(summary = "댓글 조회", description = "게시글의 댓글을 조회합니다.")
@GetMapping("/{post-id}")
@AllowAnonymous
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package econo.buddybridge.comment.dto;

import java.util.List;

public record MyPageCommentCustomPage(
List<MyPageCommentResDto> content,
Long totalElements,
Boolean last
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package econo.buddybridge.comment.dto;

import com.querydsl.core.annotations.QueryProjection;
import econo.buddybridge.member.entity.DisabilityType;
import econo.buddybridge.post.entity.AssistanceType;
import econo.buddybridge.post.entity.PostStatus;
import econo.buddybridge.post.entity.PostType;
import lombok.Builder;

import java.time.LocalDateTime;

@Builder
public record MyPageCommentResDto(
String content,
Long commentId,
Long postId,
String postTitle,
PostStatus postStatus,
PostType postType,
DisabilityType disabilityType,
AssistanceType assistanceType,
LocalDateTime postCreatedAt
) {

@QueryProjection
public MyPageCommentResDto {
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package econo.buddybridge.comment.repository;

import econo.buddybridge.comment.dto.CommentCustomPage;
import econo.buddybridge.comment.dto.MyPageCommentCustomPage;
import econo.buddybridge.post.entity.Post;
import econo.buddybridge.post.entity.PostType;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

@Repository
public interface CommentRepositoryCustom {
CommentCustomPage findByPost(Post post, Long cursor, Pageable page);

MyPageCommentCustomPage findByMemberId(Long memberId, Integer page, Integer size, String sort, PostType postType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import econo.buddybridge.comment.dto.CommentCustomPage;
import econo.buddybridge.comment.dto.CommentResDto;
import econo.buddybridge.comment.dto.QAuthorDto;
import econo.buddybridge.comment.dto.QCommentResDto;
import econo.buddybridge.comment.dto.*;
import econo.buddybridge.comment.entity.QComment;
import econo.buddybridge.post.entity.Post;
import econo.buddybridge.post.entity.PostType;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static econo.buddybridge.comment.entity.QComment.comment;
import static econo.buddybridge.post.entity.QPost.post;
import static org.springframework.data.domain.Sort.Order;

@RequiredArgsConstructor
Expand Down Expand Up @@ -58,6 +59,50 @@ public CommentCustomPage findByPost(Post post, Long cursor, Pageable page) {
return new CommentCustomPage(content, nextCursor, nextPage);
}

@Override
public MyPageCommentCustomPage findByMemberId(Long memberId, Integer page, Integer size, String sort, PostType postType) {

QComment subComment = new QComment("subComment");

List<MyPageCommentResDto> content = queryFactory
.select(new QMyPageCommentResDto( // MyPageCommentResDto를 생성
comment.content,
comment.id,
comment.post.id,
comment.post.title,
comment.post.postStatus,
comment.post.postType,
comment.post.disabilityType,
comment.post.assistanceType,
comment.createdAt
))
.from(comment) // comment를 기준으로 조회
.where(
buildPostTypeExpression(postType),
comment.author.id.eq(memberId)
)
.orderBy(comment.createdAt.desc())
.limit(size)
.offset((long) page * size)
.fetch();

// 댓글 단 게시글 수 조회
Long totalElements = queryFactory
.select(comment.id.count())
.from(comment)
.where(
buildPostTypeExpression(postType),
comment.author.id.eq(memberId)
)
.fetchOne();

return new MyPageCommentCustomPage(content, totalElements, content.size() < size);
}

private BooleanExpression buildPostTypeExpression(PostType postType) {
return postType == null ? null : post.postType.eq(postType);
}

private Order getPageOrder(Pageable page) {
return page.getSort().get().findFirst().orElseThrow(() -> new IllegalArgumentException("정렬 조건을 가져야 합니다."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import econo.buddybridge.comment.dto.CommentCustomPage;
import econo.buddybridge.comment.dto.CommentReqDto;
import econo.buddybridge.comment.dto.MyPageCommentCustomPage;
import econo.buddybridge.comment.entity.Comment;
import econo.buddybridge.comment.exception.CommentDeleteNotAllowedException;
import econo.buddybridge.comment.exception.CommentInvalidDirectionException;
Expand Down Expand Up @@ -33,6 +34,11 @@ public class CommentService {
private final CommentRepositoryCustom commentRepositoryCustom;
private final EmitterService emitterService;

@Transactional(readOnly = true) // MyPage 댓글 조회
public MyPageCommentCustomPage getMyPageComments(Long memberId, Integer page, Integer size, String sort, PostType postType) {
return commentRepositoryCustom.findByMemberId(memberId, page - 1, size, sort, postType);
}

@Transactional(readOnly = true) // 댓글 조회
public CommentCustomPage getComments(Long postId, Integer size, String order, Long cursor) {
Post post = findPostByIdOrThrow(postId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.time.LocalDateTime;

// TODO: 쿼리변수로 매칭 상태받아와 해당 매칭만 표현가능한 API로 변경
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chat/matchings")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public class PostController {

private final PostService postService;

@Operation(summary = "내가 작성한 게시글 조회", description = "내가 작성한 게시글 목록을 조회합니다.")
@GetMapping("/my-page")
public ApiResponse<ApiResponse.CustomBody<PostCustomPage>> getAllPostsMyPage(
@RequestParam(value = "post-type", required = false) PostType postType,
@RequestParam("page") Integer page,
@RequestParam("size") Integer size,
@RequestParam(defaultValue = "desc", required = false) String sort,
HttpServletRequest request
) {
Long memberId = SessionUtils.getMemberId(request);
PostCustomPage posts = postService.getPostsMyPage(memberId, page, size, sort, postType);
return ApiResponseGenerator.success(posts, HttpStatus.OK);
}

// 커스텀 페이지네이션을 사용한 전체 게시글 조회
@Operation(summary = "게시글 목록 조회", description = "게시글 목록을 조회합니다. 게시글 유형, 상태, 장애 유형, 지원 유형으로 필터링할 수 있습니다.")
@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface PostRepositoryCustom {

PostCustomPage findPosts(Integer page, Integer size, String sort, PostType postType,
PostStatus postStatus, DisabilityType disabilityType, AssistanceType assistanceType);

PostCustomPage findPostsMyPage(Long memberId, Integer page, Integer size, String sort, PostType postType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class PostRepositoryImpl implements PostRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public PostCustomPage findPosts(Integer page, Integer size, String sort, PostType postType, PostStatus postStatus, DisabilityType disabilityType, AssistanceType assistanceType) {
public PostCustomPage findPosts(Integer page, Integer size, String sort, PostType postType,
PostStatus postStatus, DisabilityType disabilityType, AssistanceType assistanceType) {
List<PostResDto> postResDtos = queryFactory
.selectFrom(post)
.where(buildPostStatusExpression(postStatus), buildPostTypeExpression(postType),
Expand All @@ -45,6 +46,33 @@ public PostCustomPage findPosts(Integer page, Integer size, String sort, PostTyp
return new PostCustomPage(postResDtos, totalElements, postResDtos.size() < size);
}

@Override
public PostCustomPage findPostsMyPage(Long memberId, Integer page, Integer size, String sort, PostType postType) {

List<PostResDto> content = queryFactory
.selectFrom(post)
.where(buildMemberIdExpression(memberId), buildPostTypeExpression(postType))
.offset((long) page * size)
.limit(size)
.orderBy(buildOrderSpecifier(sort))
.fetch()
.stream()
.map(PostResDto::new)
.toList();

Long totalElements = queryFactory
.select(post.count())
.from(post)
.where(buildMemberIdExpression(memberId), buildPostTypeExpression(postType))
.fetchOne();

return new PostCustomPage(content, totalElements, content.size() < size);
}

private BooleanExpression buildMemberIdExpression(Long memberId) {
return memberId == null ? null : post.author.id.eq(memberId);
}

private BooleanExpression buildPostTypeExpression(PostType postType) {
return postType == null ? null : post.postType.eq(postType);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/econo/buddybridge/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public PostResDto findPost(Long postId) {
return new PostResDto(post);
}

@Transactional(readOnly = true) // 내가 작성한 게시글 조회
public PostCustomPage getPostsMyPage(Long memberId, Integer page, Integer size, String sort, PostType postType) {
return postRepositoryCustom.findPostsMyPage(memberId, page - 1, size, sort, postType);
}

@Transactional(readOnly = true)
public PostCustomPage getPosts(Integer page, Integer size, String sort, PostType postType, PostStatus postStatus,
DisabilityType disabilityType, AssistanceType assistanceType) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring:

jpa:
hibernate:
ddl-auto: create
ddl-auto: update # create에서 update로 변경
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
defer-datasource-initialization: true
Expand All @@ -30,7 +30,7 @@ spring:
sql:
init:
data-locations: classpath:data.sql
mode: always
mode: NEVER # ALAWAYS에서 NEVER로 변경

server:
servlet:
Expand Down

0 comments on commit a75afb3

Please sign in to comment.