-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from ssu-student-union/feat/45-post
[feat] : 게시판 조회 별 dto 반환 로직 구현
- Loading branch information
Showing
24 changed files
with
422 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/ussum/homepage/application/post/controller/PostManageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
src/main/java/ussum/homepage/application/post/service/PostManageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...a/ussum/homepage/application/post/service/dto/response/postList/AuditPostResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../java/ussum/homepage/application/post/service/dto/response/postList/LostPostResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ava/ussum/homepage/application/post/service/dto/response/postList/NoticePostResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...va/ussum/homepage/application/post/service/dto/response/postList/PartnerPostResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...a/ussum/homepage/application/post/service/dto/response/postList/PetitionPostResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/ussum/homepage/application/post/service/dto/response/postList/PostListRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...in/java/ussum/homepage/application/post/service/dto/response/postList/PostListResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.