Skip to content

Commit

Permalink
Merge pull request #56 from ssu-student-union/refactor/52-post
Browse files Browse the repository at this point in the history
[refactor] #52 청원게시물 단건 조회 시 공식답변(댓글) 포함 및 게시물 파일 생성 경로 수정
  • Loading branch information
chahyunsoo authored Aug 16, 2024
2 parents dad075b + 2b300f2 commit 5f748b0
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
import ussum.homepage.domain.comment.service.PostCommentFormatter;
import ussum.homepage.domain.comment.service.PostCommentModifier;
import ussum.homepage.domain.comment.service.PostCommentReader;
import ussum.homepage.domain.group.Group;
import ussum.homepage.domain.group.service.GroupReader;
import ussum.homepage.domain.member.Member;
import ussum.homepage.domain.member.service.MemberManager;
import ussum.homepage.domain.member.service.MemberReader;
import ussum.homepage.infra.jpa.comment.entity.CommentType;

@Service
@RequiredArgsConstructor
Expand All @@ -30,7 +25,6 @@ public class CommentService {
private final PostCommentFormatter postCommentFormatter;
private final PostCommentAppender postCommentAppender;
private final PostCommentModifier postCommentModifier;
private final MemberReader memberReader;
private final MemberManager memberManager;

public PostCommentListResponse getCommentList(Long postId, int page, int take, String type){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ public record PostOfficialCommentResponse(
String authorName,
String content,
String commentType,
String lastEditedAt
String createdAt,
String lastEditedAt,
Boolean isAuthor
) {
public static PostOfficialCommentResponse of(PostComment postComment, String authorName, String commentType) {
public static PostOfficialCommentResponse of(PostComment postComment, String authorName, String commentType, Boolean isAuthor) {
return new PostOfficialCommentResponse(
postComment.getId(),
authorName,
postComment.getContent(),
commentType,
postComment.getLastEditedAt()
);
postComment.getCreatedAt(),
postComment.getLastEditedAt(),
isAuthor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public ResponseEntity<ApiResponse<?>> createBoardPost(@UserId Long userId,
return ApiResponse.success(postManageService.createBoardPost(userId, boardCode, postCreateRequest));
}

@PostMapping("/{boardCode}/posts/files")
@PostMapping("/{boardCode}/files")
public ResponseEntity<ApiResponse<?>> createBoardPostFile(@UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestPart(value = "files") MultipartFile[] files,
@RequestParam(value = "type") String typeName){
@RequestParam(value = "type") String typeName) {
return ApiResponse.success(postManageService.createBoardPostFile(userId, boardCode, files, typeName));
}

Expand All @@ -58,7 +58,7 @@ public ResponseEntity<ApiResponse<?>> editBoardPost(@PathVariable(name = "boardC

@PostMapping("/userIdTest")
public ResponseEntity<ApiResponse<?>> apiTest(@UserId Long userId) {
System.out.println("userId = " + userId);;
System.out.println("userId = " + userId);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ussum.homepage.application.comment.service.dto.response.PostOfficialCommentResponse;
import ussum.homepage.application.post.service.dto.request.PostCreateRequest;
import ussum.homepage.application.post.service.dto.request.PostUpdateRequest;
import ussum.homepage.application.post.service.dto.request.PostUserRequest;
import ussum.homepage.application.post.service.dto.response.postDetail.*;
import ussum.homepage.application.post.service.dto.response.postList.*;
import ussum.homepage.application.post.service.dto.response.postSave.PostCreateResponse;
import ussum.homepage.application.post.service.dto.response.postSave.PostFileResponse;
import ussum.homepage.domain.comment.PostComment;
import ussum.homepage.domain.comment.service.PostCommentReader;
import ussum.homepage.domain.comment.service.PostOfficialCommentFormatter;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Category;
import ussum.homepage.domain.post.Post;
Expand All @@ -25,7 +29,6 @@
import ussum.homepage.global.common.PageInfo;
import ussum.homepage.global.error.exception.GeneralException;
import ussum.homepage.global.error.status.ErrorStatus;
import ussum.homepage.infra.jpa.post.entity.OngoingStatus;
import ussum.homepage.infra.utils.S3utils;

import java.util.List;
Expand All @@ -43,11 +46,13 @@ public class PostManageService {
private final PostReactionReader postReactionReader;
private final CategoryReader categoryReader;
private final UserReader userReader;
private final PostCommentReader postCommentReader;
private final PostFileReader postFileReader;
private final PostAppender postAppender;
private final PostFileAppender postFileAppender;
private final PostModifier postModifier;
private final PostStatusProcessor postStatusProcessor;
private final PostOfficialCommentFormatter postOfficialCommentFormatter;
private final S3utils s3utils;

private final Map<String, BiFunction<Post, Integer, ? extends PostListResDto>> postResponseMap = Map.of(
Expand All @@ -58,12 +63,12 @@ public class PostManageService {
"청원게시판", PetitionPostResponse::of
);

private final Map<String, PostDetailFunction<Post, Boolean, String, Integer, String, String, String, ? extends PostDetailResDto>> postDetailResponseMap = Map.of(
"공지사항게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList) -> NoticePostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"분실물게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, another_ignored) -> LostPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList),
"제휴게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList) -> PartnerPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"감사기구게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList) -> AuditPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"청원게시판", (post, isAuthor, authorName, likeCount, onGoingStatus, imageList, ignored) -> PetitionPostDetailResponse.of(post, isAuthor, authorName, likeCount, onGoingStatus, imageList)
private final Map<String, PostDetailFunction<Post, Boolean, String, Integer, String, String, String, PostOfficialCommentResponse, ? extends PostDetailResDto>> postDetailResponseMap = Map.of(
"공지사항게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList, another_ignored) -> NoticePostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"분실물게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, another_ignored1, another_ignored2) -> LostPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList),
"제휴게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList, another_ignored) -> PartnerPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"감사기구게시판", (post, isAuthor, authorName, ignored, categoryName, imageList, fileList, another_ignored) -> AuditPostDetailResponse.of(post, isAuthor, authorName, categoryName, imageList, fileList),
"청원게시판", (post, isAuthor, authorName, likeCount, onGoingStatus, imageList, ignored, postOfficialCommentResponseList) -> PetitionPostDetailResponse.of(post, isAuthor, authorName, likeCount, onGoingStatus, imageList, postOfficialCommentResponseList)
);


Expand Down Expand Up @@ -109,7 +114,7 @@ public PostDetailRes<?> getPost(PostUserRequest postUserRequest, String boardCod
List<String> fileList = postFileReader.getPostFileListByFileType(postFileList);


PostDetailFunction<Post, Boolean, String, Integer, String, String, String, ? extends PostDetailResDto> responseFunction = postDetailResponseMap.get(board.getName());
PostDetailFunction<Post, Boolean, String, Integer, String, String, String, PostOfficialCommentResponse, ? extends PostDetailResDto> responseFunction = postDetailResponseMap.get(board.getName());

if (responseFunction == null) {
throw new GeneralException(ErrorStatus.INVALID_BOARDCODE);
Expand All @@ -119,11 +124,15 @@ public PostDetailRes<?> getPost(PostUserRequest postUserRequest, String boardCod
if (board.getName().equals("청원게시판")) {
Integer likeCount = postReactionReader.countPostReactionsByType(post.getId(), "like");
String postOnGoingStatus = postStatusProcessor.processStatus(post);
response = responseFunction.apply(post, isAuthor, user.getName(), likeCount, postOnGoingStatus, imageList, null);
List<PostComment> officialPostComments = postCommentReader.getCommentListWithPostIdAndCommentType(userId, postId, "OFFICIAL");
List<PostOfficialCommentResponse> postOfficialCommentResponses = officialPostComments.stream()
.map(postOfficialComment -> postOfficialCommentFormatter.format(postOfficialComment, userId))
.toList();
response = responseFunction.apply(post, isAuthor, user.getName(), likeCount, postOnGoingStatus, imageList, null, postOfficialCommentResponses);
} else if (board.getName().equals("제휴게시판") || board.getName().equals("공지사항게시판") || board.getName().equals("감사기구게시판")) {
response = responseFunction.apply(post, isAuthor, user.getName(), null, category.getName(), imageList, fileList);
response = responseFunction.apply(post, isAuthor, user.getName(), null, category.getName(), imageList, fileList,null);
} else if (board.getName().equals("분실물게시판")) {
response = responseFunction.apply(post, isAuthor, user.getName(), null, category.getName(), imageList, null); //분실물 게시판은 파일첨부 제외
response = responseFunction.apply(post, isAuthor, user.getName(), null, category.getName(), imageList, null, null); //분실물 게시판은 파일첨부 제외
}

return PostDetailRes.of(response);
Expand All @@ -134,7 +143,7 @@ public PostCreateResponse createBoardPost(Long userId, String boardCode, PostCre
Board board = boardReader.getBoardWithBoardCode(boardCode);
Category category = categoryReader.getCategoryWithCode(postCreateRequest.categoryCode());
User user = userReader.getUserWithId(userId);
String onGoingStatus = Objects.equals(boardCode, "PETITION") ? "IN_PROGRESS" : null;
String onGoingStatus = Objects.equals(boardCode, "PETITION") ? category.getCategoryCode() : null;

Post post = postAppender.createPost(postCreateRequest.toDomain(board, user, category, onGoingStatus));
postFileAppender.updatePostIdForIds(postCreateRequest.postFileList(), post.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Builder;
import lombok.Getter;
import ussum.homepage.application.comment.service.dto.response.PostCommentResponse;
import ussum.homepage.application.comment.service.dto.response.PostOfficialCommentResponse;
import ussum.homepage.domain.post.Post;
import ussum.homepage.infra.jpa.post.entity.OngoingStatus;

Expand All @@ -12,17 +14,20 @@ public class PetitionPostDetailResponse extends PostDetailResDto {
private final Integer likeCount;
private final String onGoingStatus;
private final List<String> imageList;
private final List<PostOfficialCommentResponse> officialCommentList;

@Builder
private PetitionPostDetailResponse(Long postId, String categoryName, String authorName, String title, String content, String createdAt, Boolean isAuthor,
Integer likeCount, String onGoingStatus, List<String> imageList) {
Integer likeCount, String onGoingStatus, List<String> imageList, List<PostOfficialCommentResponse> officialCommentList) {
super(postId, categoryName, authorName, title, content, createdAt, isAuthor);
this.likeCount = likeCount;
this.onGoingStatus = onGoingStatus;
this.imageList = imageList;
this.officialCommentList = officialCommentList;
}

public static PetitionPostDetailResponse of(Post post, Boolean isAuthor, String authorName, Integer likeCount, String onGoingStatus, List<String> imageList) {
public static PetitionPostDetailResponse of(Post post, Boolean isAuthor, String authorName, Integer likeCount, String onGoingStatus, List<String> imageList,
List<PostOfficialCommentResponse> officialCommentList) {
return PetitionPostDetailResponse.builder()
.postId(post.getId())
.categoryName(OngoingStatus.toKorean(onGoingStatus))
Expand All @@ -34,6 +39,7 @@ public static PetitionPostDetailResponse of(Post post, Boolean isAuthor, String
.likeCount(likeCount)
.onGoingStatus(onGoingStatus)
.imageList(imageList)
.officialCommentList(officialCommentList)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ public void saveUserOnBoarding(Long userId, OnBoardingRequest request){
user.updateOnBoardingUser(request); // 이 메소드 수정 필요
userModifier.save(user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface PostCommentRepository {
List<PostComment> findAllByPostId(Long postId);
List<PostComment> findAllByPostIdOrderByLikesDesc(Long postId);
List<PostComment> findAllByPostIdOrderByCreatedAtDesc(Long postId);
List<PostComment> findAllByPostIdAndCommentType(Long postId, String commentType);
Optional<PostComment> findByPostIdAndUserId(Long postId, Long userId);
Optional<PostComment> findById(Long id);
PostComment save(PostComment postComment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
public interface PostCommentFormatter {
PostCommentResponse format(Long postId, Long userId, String commentType);
PostCommentResponse format(PostComment postComment, Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.springframework.stereotype.Service;
import ussum.homepage.domain.comment.PostComment;
import ussum.homepage.domain.comment.PostCommentRepository;
import ussum.homepage.domain.comment.service.formatter.PostCommentFormatter;
import ussum.homepage.domain.reaction.exception.PostCommentException;
import ussum.homepage.domain.reaction.service.PostCommentReactionReader;
import ussum.homepage.global.error.exception.InvalidValueException;

import java.util.List;
Expand Down Expand Up @@ -51,7 +51,11 @@ public List<PostComment> getCommentListWithPostIdAndType(Long postId, String typ
if (comments.isEmpty()) {
throw new PostCommentException(POST_COMMENT_NOT_FOUND);
}

return comments;
}

public List<PostComment> getCommentListWithPostIdAndCommentType(Long userId, Long postId, String commentType) {
return postCommentRepository.findAllByPostIdAndCommentType(postId, commentType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ussum.homepage.domain.comment.service;

import ussum.homepage.application.comment.service.dto.response.PostOfficialCommentResponse;
import ussum.homepage.domain.comment.PostComment;

public interface PostOfficialCommentFormatter {
PostOfficialCommentResponse format(PostComment postComment, Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ussum.homepage.domain.comment.service.formatter;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ussum.homepage.application.comment.service.dto.response.PostOfficialCommentResponse;
import ussum.homepage.domain.comment.PostComment;
import ussum.homepage.domain.user.User;
import ussum.homepage.domain.user.service.UserReader;


@Service
@RequiredArgsConstructor
public class PostOfficialCommentFormatter implements ussum.homepage.domain.comment.service.PostOfficialCommentFormatter {
private final UserReader userReader;

@Override
public PostOfficialCommentResponse format(PostComment postComment, Long userId) {
User user = userReader.getUserWithId(postComment.getUserId());
Boolean isAuthor = userId != null && userId.equals(postComment.getUserId());

return PostOfficialCommentResponse.of(postComment, user.getName(), postComment.getCommentType(), isAuthor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ public String updatePostOngoingStatus(Long postId, String onGoingStatus) {
}

/**
* '진행중' 청원일 때 30일이내에 좋아요 100개를 달성하지 못하면 '종료됨'
* '진행중' 청원일 때 30일이내에 좋아요 100개를 달성하면 '접수된' 청원으로 변경
* '진행중' 청원일 때 30일이 지난 시점에 좋아요 100개를 달성하지 못하면 '종료됨'
* '진행중' 청원일 때 30일이 지난 시점에 좋아요 100개를 달성하면 '접수된' 청원으로 변경
*/
private String handleInProgressStatus(Post post, Integer likeCountOfPost) {
LocalDateTime createdAt = LocalDateTime.parse(post.getCreatedAt());
if (LocalDateTime.now().isBefore(createdAt.plusDays(30))) {
// 30일 이내에 좋아요 100개를 달성하지 못한 경우
// 30일이 경과한 경우
if (LocalDateTime.now().isAfter(createdAt.plusDays(30))) {
// 30일 동안 좋아요 100개를 달성하지 못한 경우에만 종료됨 상태로 변경
if (likeCountOfPost < 100) {
return updatePostOngoingStatus(post.getId(), "COMPLETED");
} else {
} else return updatePostOngoingStatus(post.getId(), "RECEIVED");
} else {
if (likeCountOfPost >= 100) {
return updatePostOngoingStatus(post.getId(), "RECEIVED");
}
} else return "IN_PROGRESS";
}
return "IN_PROGRESS";
// 30일 이내면 아직 상태를 변경하지 않음
// return "IN_PROGRESS";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

@FunctionalInterface
public interface PostDetailFunction<T, K, U, V, W, Q, Y, R> {
R apply(T t, K k, U u, V v, W w, List<Q> q, List<Y> y);
public interface PostDetailFunction<T, K, U, V, W, Q, Y, I, R> {
R apply(T t, K k, U u, V v, W w, List<Q> q, List<Y> y, List<I> i);
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import ussum.homepage.global.config.CorsConfig;
import ussum.homepage.global.config.auth.ExceptionHandlerFilter;
import ussum.homepage.global.jwt.*;

Expand All @@ -33,12 +31,11 @@ public class SecurityConfig {
"/swagger-ui/**",
"/swagger-resources/**",
"/v3/api-docs/**",
// "/board/{boardCode}/posts/{postId}",
// "/board/posts/{postId}/comments"
"/board/{boardCode}/posts/{postId}",
"/board/posts/{postId}/comments"

};


@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand Down
Loading

0 comments on commit 5f748b0

Please sign in to comment.