Skip to content

Commit

Permalink
Merge pull request #46 from ssu-student-union/feat/45-post
Browse files Browse the repository at this point in the history
[feat] : 게시판 조회 별 dto 반환 로직 구현
  • Loading branch information
chahyunsoo authored Aug 2, 2024
2 parents 7110d96 + f3d18bc commit 1acece6
Show file tree
Hide file tree
Showing 24 changed files with 422 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class QPostEntity extends EntityPathBase<PostEntity> {

public final DateTimePath<java.time.LocalDateTime> lastEditedAt = createDateTime("lastEditedAt", java.time.LocalDateTime.class);

public final EnumPath<Status> status = createEnum("status", Status.class);

public final StringPath thumbnailImage = createString("thumbnailImage");

public final StringPath title = createString("title");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ussum.homepage.application.post.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ussum.homepage.application.post.service.PostManageService;
import ussum.homepage.application.post.service.PostService;
import ussum.homepage.application.post.service.dto.response.PostListResponse;
import ussum.homepage.global.ApiResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/board")
public class PostManageController {

private final PostManageService postManageService;

@GetMapping("/{boardCode}/posts")
public ResponseEntity<ApiResponse<?>> getBoardPostsList(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "take") int take,
@PathVariable(name = "boardCode") String boardCode) {

// PostListResponse postList = postService.getPostList(PageRequest.of(page, take, Sort.by("id").descending()), boardCode);
return ApiResponse.success(postManageService.getPostList(page, take, boardCode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package ussum.homepage.application.post.service;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ussum.homepage.application.post.service.dto.response.postList.*;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Post;
import ussum.homepage.domain.post.service.BoardReader;
import ussum.homepage.domain.post.service.PostReader;
import ussum.homepage.domain.postlike.service.PostReactionReader;
import ussum.homepage.global.common.PageInfo;

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

import static ussum.homepage.global.error.status.ErrorStatus.POST_NOT_FOUND;

@Service
@RequiredArgsConstructor
public class PostManageService {

private final BoardReader boardReader;
private final PostReader postReader;
private final PostReactionReader postReactionReader;

private final Map<String, BiFunction<Post, Integer, ? extends PostListResDto>> postResponseMap = Map.of(
"공지사항게시판", (post, ignored) -> NoticePostResponse.of(post),
"분실물게시판", (post, ignored) -> LostPostResponse.of(post),
"제휴게시판", (post, ignored) -> PartnerPostResponse.of(post),
"감사기구게시판", (post, ignored) -> AuditPostResponseDto.of(post),
"청원게시판", PetitionPostResponse::of
);

public PostListRes<?> getPostList(int page, int take, String boardCode) {
Board board = boardReader.getBoardWithBoardCode(boardCode);
Pageable pageable = PageInfo.of(page, take);
Page<Post> postList = postReader.getPostListByBoardId(board.getId(), pageable);
PageInfo pageInfo = PageInfo.of(postList);

BiFunction<Post, Integer, ? extends PostListResDto> responseFunction = postResponseMap.get(board.getName());

if (responseFunction == null) {
throw new IllegalArgumentException("Unknown board type: " + board.getName());
}

List<? extends PostListResDto> responseList = postList.getContent().stream()
.map(post -> {
if (board.getName().equals("청원게시판")) {
Integer likeCount = postReactionReader.countPostReactionsByType(post.getId(), "like");
return responseFunction.apply(post, likeCount);
} else {
return responseFunction.apply(post, null);
}
})
.toList();

return PostListRes.of(responseList, pageInfo);
}
}
//스위치 사용 로직
/*
package ussum.homepage.application.post.service;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ussum.homepage.application.post.service.dto.response.postList.*;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Post;
import ussum.homepage.domain.post.service.BoardReader;
import ussum.homepage.domain.post.service.PostReader;
import ussum.homepage.global.common.PageInfo;
import java.util.List;
import static ussum.homepage.global.error.status.ErrorStatus.POST_NOT_FOUND;
@Service
@RequiredArgsConstructor
public class PostManageService {
private final BoardReader boardReader;
private final PostReader postReader;
public PostListRes<?> getPostList(int page, int take, String boardCode) {
Board board = boardReader.getBoardWithBoardCode(boardCode);
Pageable pageable = PageInfo.of(page, take);
Page<Post> postList = postReader.getPostListByBoardId(board.getId(), pageable);
PageInfo pageInfo = PageInfo.of(postList);
switch (board.getName()) {
case "공지사항":
return getNoticePostList(postList, pageInfo);
case "분실물":
return getLostPostList(postList, pageInfo);
case "제휴":
return getPartnerPostList(postList, pageInfo);
case "감사기구":
return getAuditPostList(postList, pageInfo);
case "청원":
// return getPetitionPostList(postList, pageInfo);
default:
throw new EntityNotFoundException(String.valueOf(POST_NOT_FOUND));
}
}
private PostListRes<NoticePostResponse> getNoticePostList(Page<Post> postList, PageInfo pageInfo) {
List<NoticePostResponse> responseList = postList.getContent().stream()
.map(NoticePostResponse::of)
.toList();
return PostListRes.of(responseList, pageInfo);
}
private PostListRes<LostPostResponse> getLostPostList(Page<Post> postList, PageInfo pageInfo) {
List<LostPostResponse> responseList = postList.getContent().stream()
.map(LostPostResponse::of)
.toList();
return PostListRes.of(responseList, pageInfo);
}
private PostListRes<PartnerPostResponse> getPartnerPostList(Page<Post> postList, PageInfo pageInfo) {
List<PartnerPostResponse> responseList = postList.getContent().stream()
.map(PartnerPostResponse::of)
.toList();
return PostListRes.of(responseList, pageInfo);
}
private PostListRes<AuditPostResponseDto> getAuditPostList(Page<Post> postList, PageInfo pageInfo) {
List<AuditPostResponseDto> responseList = postList.getContent().stream()
.map(AuditPostResponseDto::of)
.toList();
return PostListRes.of(responseList, pageInfo);
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Post toDomain(Board board, User user, Category category) {
null,
null,
null,
null,
user.getId(), //이건 채워넣어야 함, user쪽 개발되면
board.getId(),
category.getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public Post toDomain(Post post, Board board, Category category) {
content,
post.getViewCount(),
thumbnailImage,
post.getStatus(),
LocalDateTime.parse(post.getCreatedAt()),
LocalDateTime.parse(post.getUpdatedAt()),
LocalDateTime.now(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;

@Getter
public class AuditPostResponseDto extends PostListResDto {
private final String thumbNail;
private final String status;

@Builder
private AuditPostResponseDto(String postId, String title, String content, String date, String thumbNail, String status) {
super(postId, title, content, date);
this.thumbNail = thumbNail;
this.status = status;
}

public static AuditPostResponseDto of(Post post) {
return AuditPostResponseDto.builder()
.postId(post.getId().toString())
.title(post.getTitle())
.content(post.getContent())
.date(post.getCreatedAt().toString())
.thumbNail(post.getThumbnailImage())
.status(post.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;


@Getter
public class LostPostResponse extends PostListResDto{
private final String thumbNail;


@Builder
private LostPostResponse(String postId, String title, String content, String date, String thumbNail) {
super(postId, title, content, date);
this.thumbNail = thumbNail;

}

public static LostPostResponse of(Post post) {
return LostPostResponse.builder()
.postId(post.getId().toString())
.title(post.getTitle())
.content(post.getContent())
.date(post.getCreatedAt().toString())
.thumbNail(post.getThumbnailImage())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;

@Getter
public class NoticePostResponse extends PostListResDto{
private final String thumbNail;
private final String status;

@Builder
private NoticePostResponse(String postId, String title, String content, String date, String thumbNail, String status) {
super(postId, title, content, date);
this.thumbNail = thumbNail;
this.status = status;
}

public static NoticePostResponse of(Post post) {
return NoticePostResponse.builder()
.postId(post.getId().toString())
.title(post.getTitle())
.content(post.getContent())
.date(post.getCreatedAt().toString())
.thumbNail(post.getThumbnailImage())
.status(post.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;

@Getter
public class PartnerPostResponse extends PostListResDto{
private final String thumbNail;
private final String status;

@Builder
private PartnerPostResponse(String postId, String title, String content, String date, String thumbNail, String status) {
super(postId, title, content, date);
this.thumbNail = thumbNail;
this.status = status;
}

public static PartnerPostResponse of(Post post) {
return PartnerPostResponse.builder()
.postId(post.getId().toString())
.title(post.getTitle())
.content(post.getContent())
.date(post.getCreatedAt().toString())
.thumbNail(post.getThumbnailImage())
.status(post.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Post;


@Getter
public class PetitionPostResponse extends PostListResDto{
private final Integer likeCount;
private final String status;

@Builder
private PetitionPostResponse(String postId, String title, String content, String date, Integer likeCount, String status) {
super(postId, title, content, date);
this.likeCount = likeCount;
this.status = status;
}

public static PetitionPostResponse of(Post post, Integer likeCount) {
return PetitionPostResponse.builder()
.postId(post.getId().toString())
.title(post.getTitle())
.content(post.getContent())
.date(post.getCreatedAt().toString())
.likeCount(likeCount)
.status(post.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.AccessLevel;
import lombok.Builder;
import ussum.homepage.global.common.PageInfo;

import java.util.List;

@Builder(access = AccessLevel.PRIVATE)
public record PostListRes<T extends PostListResDto>(
List<T> postListResDto,
PageInfo pageInfo
) {
public static <T extends PostListResDto> PostListRes<T> of(List<T> postListResDto, PageInfo pageInfo) {
return new PostListRes<>(postListResDto, pageInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ussum.homepage.application.post.service.dto.response.postList;

import lombok.Getter;

@Getter
public abstract class PostListResDto {
protected String postId;
protected String title;
protected String content;
protected String date;

protected PostListResDto(String postId, String title, String content, String date) {
this.postId = postId;
this.title = title;
this.content = content;
this.date = date;
}
}
Loading

0 comments on commit 1acece6

Please sign in to comment.