Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: 프리뷰 사용하는 부분 통합 #311

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -351,7 +351,7 @@ private void sendMemberNewPostAlert(Post post) {
// 이벤트 저장
AlertEvent event = saveEvent(
EventCategory.FOLLOWING_MEMBER_NEW_POST,
PostConverter.toSimplePostResponseDto(post),
PostConverter.toPostPreviewDTO(post),
receiverIdList
);

Expand Down Expand Up @@ -383,7 +383,7 @@ private void sendTeamNewPostAlertToTeamFollowers(Post post) {
// 이벤트 저장
AlertEvent event = saveEvent(
EventCategory.FOLLOWING_TEAM_NEW_POST,
PostConverter.toSimplePostResponseDto(post),
PostConverter.toPostPreviewDTO(post),
receiverIdList
);

Expand Down Expand Up @@ -415,7 +415,7 @@ private void sendTeamNewPostAlertToTeamMembers(Post post) {
// 이벤트 저장
AlertEvent event = saveEvent(
EventCategory.MY_TEAM_NEW_POST,
PostConverter.toSimplePostResponseDto(post),
PostConverter.toPostPreviewDTO(post),
receiverIdList
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.codiary.backend.domain.post.controller;

import com.codiary.backend.domain.alert.service.AlertService;
import com.codiary.backend.domain.category.dto.CategoryResponseDTO;
import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.domain.member.security.CustomMemberDetails;
import com.codiary.backend.domain.member.service.MemberCommandService;
Expand All @@ -20,7 +19,6 @@
import com.codiary.backend.global.apiPayload.exception.GeneralException;
import com.codiary.backend.global.jwt.JwtTokenProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -103,9 +101,12 @@ public ApiResponse<?> deletePost(@PathVariable Long postId) {
//특정 게시글 조회
@GetMapping("/{postId}")
@Operation(summary = "특정 게시글 조회 API", description = "특정 게시글을 조회합니다.")
public ApiResponse<PostResponseDTO.PostPreviewDTO> findPost(@PathVariable Long postId){
Object request;
Post findPost = postQueryService.findById(postId);
public ApiResponse<PostResponseDTO.PostPreviewDTO> findPost(
@PathVariable Long postId,
@AuthenticationPrincipal CustomMemberDetails memberDetails
) {
Long memberId = (memberDetails != null) ? memberDetails.getId() : 0;
Post findPost = postQueryService.findById(postId, memberId);
return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toPostPreviewDTO(findPost));
}

Expand Down Expand Up @@ -186,7 +187,7 @@ public ApiResponse<PostResponseDTO.PostAdjacentDTO> findAdjacentPosts(@PathVaria
// 전체 인기글 or 최신글 조회
@Operation(summary = "공개글 리스트 조회", description = "popular/latest 입력 시 인기글/최신글 조회")
@GetMapping("/list")
public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> getPostList(
public ApiResponse<Page<PostResponseDTO.PostPreviewDTO>> getPostList(
@PageableDefault(size = 9) Pageable pageable
) {
Page<Post> postPage = postService.getPostList(pageable);
Expand All @@ -196,12 +197,12 @@ public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> getPostList(
// 카테고리 인기글 조회
@Operation(summary = "카테고리 인기글/최신글 조회 (popular/latest 입력 (기본 popular)")
@GetMapping("/category/{category_id}")
public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> getCategoryPopularPosts(
public ApiResponse<Page<PostResponseDTO.PostPreviewDTO>> getCategoryPopularPosts(
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PathVariable("category_id") Long categoryId,
@PageableDefault(size = 9, sort = "popular") Pageable pageable
) {
Long memberId = memberDetails.getId();
Long memberId = (memberDetails != null) ? memberDetails.getId() : 0;
Page<Post> postPage = postService.getCategoryPosts(memberId, categoryId, pageable);
return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toPostListResponseDto(postPage));
}
Expand All @@ -221,7 +222,7 @@ public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> getCategoryPopul
// 게시글 검색 결과 페이지네이션
@Operation(summary = "게시글 검색 결과 페이지네이션", description = "게시글(제목/내용) 키워드 검색 결과를 페이지네이션하여 반환합니다.")
@GetMapping("/search")
public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> searchPost(
public ApiResponse<Page<PostResponseDTO.PostPreviewDTO>> searchPost(
@RequestParam(value = "keyword", defaultValue = "", required = false) String keyword,
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 9) Pageable pageable
Expand Down Expand Up @@ -249,7 +250,7 @@ public ApiResponse<PostResponseDTO.UpdatePostResultDTO> setPostCategory(
// 게시글 검색 (저자 이름, 팀 이름, 프로젝트 이름으로 검색)
@GetMapping("/search_by_name")
@Operation(summary = "게시글 검색 (저자 이름, 팀 이름, 프로젝트 이름으로 검색)")
public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> searchByName(
public ApiResponse<Page<PostResponseDTO.PostPreviewDTO>> searchByName(
@RequestParam(value = "author", defaultValue = "", required = false) String authorName,
@RequestParam(value = "team", defaultValue = "", required = false) String teamName,
@RequestParam(value = "project", defaultValue = "", required = false) String projectName,
Expand Down Expand Up @@ -292,16 +293,20 @@ public ApiResponse<String> cancelBookmark(

@GetMapping("/following/paging")
@Operation(summary = "팔로잉한 멤버/팀의 게시글 리스트 페이징 조회 API", description = "팔로잉한 멤버/팀의 게시글 리스트를 페이징으로 조회합니다. **첫 페이지는 0부터 입니다.**")
public ApiResponse<PostResponseDTO.PostPreviewListDTO> findPostByFollowing(@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 9) Pageable pageable) {
public ApiResponse<PostResponseDTO.PostPreviewListDTO> findPostByFollowing(
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 9) Pageable pageable
) {
Page<Post> posts = postQueryService.getPostsByFollowing(memberDetails.getId(), pageable);
return ApiResponse.onSuccess(SuccessStatus.POST_OK, PostConverter.toPostPreviewListDTO(posts));
}

@GetMapping("/bookmark/paging")
@Operation(summary = "북마크한 게시글 조회")
public ApiResponse<Page<PostResponseDTO.SimplePostResponseDTO>> getBookmarkPost(@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 9) Pageable pageable) {
public ApiResponse<Page<PostResponseDTO.PostPreviewDTO>> getBookmarkPost(
@AuthenticationPrincipal CustomMemberDetails memberDetails,
@PageableDefault(size = 9) Pageable pageable
) {
return ApiResponse.onSuccess(SuccessStatus.POST_OK,
PostConverter.toPostListResponseDto(postQueryService.getBookmarkPost(memberDetails.getId(), pageable)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@

public class PostConverter {

public static Page<PostResponseDTO.SimplePostResponseDTO> toPostListResponseDto(Page<Post> postList) {
return postList.map(PostConverter::toSimplePostResponseDto);
public static Page<PostResponseDTO.PostPreviewDTO> toPostListResponseDto(Page<Post> postList) {
return postList.map(PostConverter::toPostPreviewDTO);
}

public static PostResponseDTO.SimplePostResponseDTO toSimplePostResponseDto(Post post) {
return PostResponseDTO.SimplePostResponseDTO.builder()
public static PostResponseDTO.PostPreviewDTO toPostPreviewDTO(Post post) {
return PostResponseDTO.PostPreviewDTO.builder()
.id(post.getPostId())
.title(post.getPostTitle())
.body(post.getPostBody())
.author(post.getMember() != null ? post.getMember().getNickname() : null)
.authorImageUrl((post.getMember() != null && post.getMember().getImage() != null)
? post.getMember().getImage().getImageUrl()
: "")
.thumbnailImageUrl(post.getThumbnailImage() != null ? post.getThumbnailImage().getFileUrl() : "")
.teamProfileImageUrl((post.getTeam() != null && post.getTeam().getProfileImage() != null)
? post.getTeam().getProfileImage().getImageUrl()
: "")
.teamBannerImageUrl((post.getTeam() != null && post.getTeam().getBannerImage() != null)
? post.getTeam().getBannerImage().getImageUrl()
: "")
.thumbnailImageUrl((post.getThumbnailImage() != null) ? post.getThumbnailImage().getFileUrl() : "")
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())

.authorId(post.getMember().getMemberId())
.authorName(post.getMember().getNickname())
.authorImageUrl((post.getMember().getImage() != null) ? post.getMember().getImage().getImageUrl() : "")

.numberOfCoauthor(post.getAuthorList().size())

.teamExist((post.getTeam() != null) ? true : false)
.teamId((post.getTeam() != null) ? post.getTeam().getTeamId() : null)
.teamName((post.getTeam() != null) ? post.getTeam().getName() : null)
.teamProfileImageUrl((post.getTeam() != null && post.getTeam().getProfileImage() != null)
? post.getTeam().getProfileImage().getImageUrl()
: null)
.build();
}

Expand Down Expand Up @@ -118,45 +121,6 @@ public static PostResponseDTO.UpdatePostResultDTO toUpdatePostResultDTO(Post pos
.build();
}

// Post 조회
public static PostResponseDTO.PostPreviewDTO toPostPreviewDTO(Post post) {
List<String> postCategories = post.getCategoriesList().stream()
.map(Category::getName)
.collect(Collectors.toList());

return PostResponseDTO.PostPreviewDTO.builder()
.postId(post.getPostId())
.memberId(post.getMember().getMemberId())
.authorNickname(post.getMember().getNickname())
.teamId(post.getTeam() != null ? post.getTeam().getTeamId() : null)
.projectId(post.getProject() != null ? post.getProject().getProjectId() : null)
.postTitle(post.getPostTitle())
.postBody(post.getPostBody())
.postStatus(post.getPostStatus())
.postCategory(String.join(", ", postCategories))
.coauthorIds(post.getAuthorList().stream()
.map(author -> author.getMember().getMemberId())
.collect(Collectors.toSet()))
.postAccess(post.getPostAccess())
.authorProfileImageUrl((post.getMember().getImage() != null)
? post.getMember().getImage().getImageUrl()
: "")
.thumbnailImageUrl((post.getThumbnailImage() != null)
? post.getThumbnailImage().getFileUrl()
: "")
.teamProfileImageUrl((post.getTeam() != null && post.getTeam().getProfileImage() != null)
? post.getTeam().getProfileImage().getImageUrl()
: "")
.teamBannerImageUrl((post.getTeam() != null && post.getTeam().getBannerImage() != null)
? post.getTeam().getBannerImage().getImageUrl()
: "")
.postFileList(PostFileConverter.toPostFileListDTO(post.getPostFileList()))
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.authorNickname(post.getMember().getNickname())
.build();
}

// Post 전체 리스트 조회
public static PostResponseDTO.PostPreviewListDTO toPostPreviewListDTO(Page<Post> posts) {
List<PostResponseDTO.PostPreviewDTO> postPreviewDTOList = posts.getContent().stream()
Expand All @@ -173,8 +137,6 @@ public static PostResponseDTO.PostPreviewListDTO toPostPreviewListDTO(Page<Post>
.build();
}



// 저자별 Post 조회
public static PostResponseDTO.MemberPostPreviewDTO toMemberPostPreviewDTO(Post post) {
List<String> postCategories = post.getCategoriesList().stream()
Expand Down Expand Up @@ -445,52 +407,13 @@ public static PostResponseDTO.MemberPostInTeamPreviewListDTO toMemberPostInTeamP
.build();
}

// 특정 Post의 인접한 Post 조회 (이전, 다음 Post 조회)
public static PostResponseDTO.PostAdjacentDTO.PostAdjacentPreviewDTO toPostAdjacentPreviewDTO(Post post) {
if (post == null) return null;

List<String> postCategories = post.getCategoriesList().stream()
.map(Category::getName)
.collect(Collectors.toList());

return PostResponseDTO.PostAdjacentDTO.PostAdjacentPreviewDTO.builder()
.postId(post.getPostId())
.memberId(post.getMember().getMemberId())
.authorNickname(post.getMember().getNickname())
.teamId(post.getTeam() != null ? post.getTeam().getTeamId() : null)
.projectId(post.getProject() != null ? post.getProject().getProjectId() : null)
.postTitle(post.getPostTitle())
.postBody(post.getPostBody())
.postStatus(post.getPostStatus())
.postCategory(String.join(", ", postCategories))
.coauthorIds(post.getAuthorList().stream()
.map(author -> author.getMember().getMemberId())
.collect(Collectors.toSet()))
.postAccess(post.getPostAccess())
.authorProfileImageUrl((post.getMember().getImage() != null)
? post.getMember().getImage().getImageUrl()
: "")
.thumbnailImageUrl((post.getThumbnailImage() != null)
? post.getThumbnailImage().getFileUrl()
: "")
.teamProfileImageUrl((post.getTeam() != null && post.getTeam().getProfileImage() != null)
? post.getTeam().getProfileImage().getImageUrl()
: "")
.teamBannerImageUrl((post.getTeam() != null && post.getTeam().getBannerImage() != null)
? post.getTeam().getBannerImage().getImageUrl()
: "")
.postFileList(PostFileConverter.toPostFileListDTO(post.getPostFileList()))
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.build();
}

// 인접 post 조회
public static PostResponseDTO.PostAdjacentDTO toPostAdjacentDTO(Post.PostAdjacent adjacent) {
return PostResponseDTO.PostAdjacentDTO.builder()
.hadOlder(adjacent.getOlderPost() != null)
.hasLater(adjacent.getLaterPost() != null)
.olderPost(toPostAdjacentPreviewDTO(adjacent.getOlderPost()))
.laterPost(toPostAdjacentPreviewDTO(adjacent.getLaterPost()))
.olderPost(toPostPreviewDTO(adjacent.getOlderPost()))
.laterPost(toPostPreviewDTO(adjacent.getLaterPost()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ public class PostResponseDTO {
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record SimplePostResponseDTO(
public record PostPreviewDTO(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DTO 통합하니 훨씬 좋네요 👍

// post 자체 정보
Long id,
String title,
String body,
String author,
String authorImageUrl,
String thumbnailImageUrl,
String teamProfileImageUrl,
String teamBannerImageUrl,
LocalDateTime createdAt,
LocalDateTime updatedAt
LocalDateTime updatedAt,
Integer numberOfBookmark,
// author 정보
Long authorId,
String authorName,
String authorImageUrl,
// coauthor 정보
Integer numberOfCoauthor,
// team 정보
Boolean teamExist, // true 인 경우 아래 내용 작성
Long teamId,
String teamName,
String teamProfileImageUrl
) {
}

Expand Down Expand Up @@ -89,32 +98,6 @@ public static class UpdatePostResultDTO {
LocalDateTime updatedAt;
}


@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record PostPreviewDTO ( // Post 조회
Long postId,
Long memberId,
String authorNickname,
String authorProfileImageUrl,
Long teamId,
String teamProfileImageUrl,
String teamBannerImageUrl,
Long projectId,
String postTitle,
String postBody,
String thumbnailImageUrl,
Boolean postStatus,
String postCategory,
Set<Long> coauthorIds,
PostAccess postAccess,
PostFileResponseDTO.PostFileListDTO postFileList,
LocalDateTime createdAt,
LocalDateTime updatedAt
){
}

@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down Expand Up @@ -327,8 +310,8 @@ public record MemberPostInTeamPreviewListDTO(
public record PostAdjacentDTO(
Boolean hasLater,
Boolean hadOlder,
PostAdjacentPreviewDTO laterPost,
PostAdjacentPreviewDTO olderPost
PostPreviewDTO laterPost,
PostPreviewDTO olderPost
) {
@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

public interface PostRepository extends JpaRepository<Post, Long>, PostRepositoryCustom {

Page<Post> findAllByPostTitleContainingIgnoreCaseOrderByCreatedAtDesc(String postTitle, Pageable pageable);
Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable);
Page<Post> findByMemberOrderByCreatedAtDescPostIdDesc(Member member, Pageable pageable);
Page<Post> findByTeamOrderByCreatedAtDescPostIdDesc(Team team, Pageable pageable);
Page<Post> findByProjectAndMemberOrderByCreatedAtDescPostIdDesc(Project project, Member member, Pageable pageable);
Expand All @@ -25,15 +23,6 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostRepositor
Page<Post> findByTeamAndMemberOrderByCreatedAtDescPostIdDesc(Team team, Member member, Pageable pageable);
Page<Post> findByTeamAndAuthorList_MemberOrderByCreatedAtDescPostIdDesc(Team team, Member member, Pageable pageable);

Optional<Post> findTopByMemberAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Member member, Long postId);

Optional<Post> findTopByMemberAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Member member, Long postId);

Optional<Post> findTopByTeamAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Team team, Long postId);

Optional<Post> findTopByTeamAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Team team, Long postId);


boolean existsByTeam(Team team);
boolean existsByProject(Project project);
boolean existsByMember(Member member);
Expand Down
Loading
Loading