Skip to content

Commit

Permalink
Merge pull request #159 from sharemindteam/feature/153-post-counselor…
Browse files Browse the repository at this point in the history
…Side

feat: 일대다 상담 미답변 질문 상담사 사이드 조회 구현
  • Loading branch information
aeyongdodam authored Mar 26, 2024
2 parents f52b1e1 + d15e5a1 commit d36bd7e
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.example.sharemind.customer.application.CustomerService;
import com.example.sharemind.customer.domain.Customer;
import com.example.sharemind.post.application.PostService;
import com.example.sharemind.post.content.PostStatus;
import com.example.sharemind.post.domain.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -24,8 +23,6 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {

private static final Integer MAX_COMMENTS = 5;
private static final Boolean COMMENT_IS_NOT_LIKED = false;

private final PostService postService;
Expand Down Expand Up @@ -82,11 +79,7 @@ public void createComment(CommentCreateRequest commentCreateRequest, Long custom
}

commentRepository.save(commentCreateRequest.toEntity(post, counselor));

List<Comment> comments = commentRepository.findByPostAndIsActivatedIsTrue(post);
if (comments.size() == MAX_COMMENTS) {
post.updatePostStatus(PostStatus.COMPLETED);
}
post.increaseTotalComment();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.sharemind.comment.repository;

import com.example.sharemind.comment.domain.Comment;
import com.example.sharemind.counselor.domain.Counselor;

import java.util.List;

public interface CommentCustomRepository {

List<Comment> findAllByCounselorAndIsActivatedIsTrue(Counselor counselor, Boolean filter, Long postId, int size);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.sharemind.comment.repository;

import com.example.sharemind.comment.domain.Comment;
import com.example.sharemind.comment.domain.QComment;
import com.example.sharemind.counselor.domain.Counselor;
import com.example.sharemind.post.content.PostStatus;
import com.example.sharemind.post.domain.QPost;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class CommentCustomRepositoryImpl implements CommentCustomRepository {

private final JPAQueryFactory jpaQueryFactory;
private final QComment comment = QComment.comment;
private final QPost post = comment.post;

@Override
public List<Comment> findAllByCounselorAndIsActivatedIsTrue(Counselor counselor, Boolean filter, Long postId, int size) {
return jpaQueryFactory
.selectFrom(comment)
.where(
commentByCounselor(counselor),
filterCondition(filter),
postIdCondition(postId)
)
.orderBy(post.postId.desc())
.limit(size)
.fetch();
}

private BooleanExpression commentByCounselor(Counselor counselor) {
return comment.isActivated.isTrue()
.and(QComment.comment.counselor.eq(counselor));
}

private BooleanExpression filterCondition(Boolean filter) {
if (Boolean.FALSE.equals(filter)) {
return QPost.post.postStatus.eq(PostStatus.PROCEEDING);
}
return null;
}

private BooleanExpression postIdCondition(Long postId) {
if (postId != null && postId != 0) {
return QPost.post.postId.lt(postId);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentCustomRepository {

List<Comment> findByPostAndIsActivatedIsTrue(Post post);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public class Constants {
public static final Boolean IS_LETTER = false;

public static final Long FEE = 1000L;

public static final Long MAX_COMMENTS = 5L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.example.sharemind.post.domain.Post;
import com.example.sharemind.post.dto.request.PostCreateRequest;
import com.example.sharemind.post.dto.request.PostUpdateRequest;
import com.example.sharemind.post.dto.response.PostGetIsSavedResponse;
import com.example.sharemind.post.dto.response.PostGetListResponse;
import com.example.sharemind.post.dto.response.PostGetPopularityResponse;
import com.example.sharemind.post.dto.response.PostGetResponse;
import com.example.sharemind.post.dto.response.*;

import java.time.LocalDateTime;
import java.util.List;

Expand All @@ -26,6 +24,8 @@ public interface PostService {

List<PostGetListResponse> getPostsByCustomer(Boolean filter, Long postId, Long customerId);

List<PostGetCounselorListResponse> getPostsByCounselor(Boolean filter, Long postId, Long customerId);

List<PostGetListResponse> getPublicPostsByCustomer(Long postId, LocalDateTime finishedAt,
Long customerId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import com.example.sharemind.post.domain.Post;
import com.example.sharemind.post.dto.request.PostCreateRequest;
import com.example.sharemind.post.dto.request.PostUpdateRequest;
import com.example.sharemind.post.dto.response.PostGetIsSavedResponse;
import com.example.sharemind.post.dto.response.PostGetListResponse;
import com.example.sharemind.post.dto.response.PostGetPopularityResponse;
import com.example.sharemind.post.dto.response.PostGetResponse;
import com.example.sharemind.post.dto.response.*;
import com.example.sharemind.post.exception.PostErrorCode;
import com.example.sharemind.post.exception.PostException;
import com.example.sharemind.post.repository.PostRepository;
import com.example.sharemind.postLike.repository.PostLikeRepository;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -30,8 +29,10 @@
@Transactional(readOnly = true)
public class PostServiceImpl implements PostService {

private static final int POST_CUSTOMER_PAGE_SIZE = 4;
private static final int POST_PAGE_SIZE = 4;
private static final int POST_POPULARITY_SIZE = 3;
private static final int TOTAL_POSTS = 50;
private static final int POSTS_AFTER_24H_COUNT = TOTAL_POSTS / 3;
private static final Boolean POST_IS_NOT_LIKED = false;

private final CustomerService customerService;
Expand Down Expand Up @@ -103,30 +104,41 @@ public List<PostGetListResponse> getPostsByCustomer(Boolean filter, Long postId,
Customer customer = customerService.getCustomerByCustomerId(customerId);

return postRepository.findAllByCustomerAndIsActivatedIsTrue(customer, filter, postId,
POST_CUSTOMER_PAGE_SIZE).stream()
POST_PAGE_SIZE).stream()
.map(post -> (post.getIsCompleted() != null && !post.getIsCompleted())
? PostGetListResponse.ofIsNotCompleted(post) : PostGetListResponse.of(post,
postLikeRepository.existsByPostAndCustomerAndIsActivatedIsTrue(post,
customer)))
.toList();
}

@Override
public List<PostGetCounselorListResponse> getPostsByCounselor(Boolean filter, Long postId,
Long customerId) {

Counselor counselor = counselorService.getCounselorByCustomerId(customerId);
List<Comment> comments = commentRepository.findAllByCounselorAndIsActivatedIsTrue(counselor, filter, postId, POST_PAGE_SIZE);
return comments.stream()
.map(comment -> PostGetCounselorListResponse.of(comment.getPost(), comment))
.toList();
}

@Override
public List<PostGetListResponse> getPublicPostsByCustomer(Long postId, LocalDateTime finishedAt,
Long customerId) {
if (customerId != 0) {
Customer customer = customerService.getCustomerByCustomerId(customerId);

return postRepository.findAllByIsPublicAndIsActivatedIsTrue(postId, finishedAt,
POST_CUSTOMER_PAGE_SIZE).stream()
POST_PAGE_SIZE).stream()
.map(post -> PostGetListResponse.of(post,
postLikeRepository.existsByPostAndCustomerAndIsActivatedIsTrue(post,
customer)))
.toList();
}

return postRepository.findAllByIsPublicAndIsActivatedIsTrue(postId, finishedAt,
POST_CUSTOMER_PAGE_SIZE).stream()
POST_PAGE_SIZE).stream()
.map(post -> PostGetListResponse.of(post, POST_IS_NOT_LIKED))
.toList();
}
Expand All @@ -141,7 +153,25 @@ public List<PostGetPopularityResponse> getPopularityPosts() {

@Override
public List<Long> getRandomPosts() {
return postRepository.findRandomProceedingPostIds();
List<Long> postsAfter24h = postRepository.findRandomProceedingPostIdsAfter24Hours();
List<Long> postsWithin24h = postRepository.findRandomProceedingPostIdsWithin24Hours();

List<Long> randomPosts = new ArrayList<>(TOTAL_POSTS);

for (int i = 0; i < Math.min(POSTS_AFTER_24H_COUNT, postsAfter24h.size()); i++) {
randomPosts.add(postsAfter24h.get(i));
}

List<Long> remainingPosts = new ArrayList<>(postsWithin24h);
remainingPosts.addAll(postsAfter24h.subList(randomPosts.size(), postsAfter24h.size()));
Collections.shuffle(remainingPosts);

int remainingSize = TOTAL_POSTS - randomPosts.size();
for(int i = 0; i < Math.min(remainingSize,remainingPosts.size()); i++) {
randomPosts.add(remainingPosts.get(i));
}
Collections.shuffle(randomPosts);
return randomPosts;
}

@Override
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/example/sharemind/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.example.sharemind.global.constants.Constants.MAX_COMMENTS;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
Expand Down Expand Up @@ -72,6 +74,9 @@ public class Post extends BaseEntity {
@Column(name = "is_completed")
private Boolean isCompleted;

@Column(name = "published_at")
private LocalDateTime publishedAt;

@Column(name = "finished_at")
private LocalDateTime finishedAt;

Expand All @@ -94,6 +99,10 @@ public void updateIsPaid() {
public void updatePostStatus(PostStatus postStatus) {
this.postStatus = postStatus;

if (this.postStatus == PostStatus.PROCEEDING) {
this.publishedAt = LocalDateTime.now();
}

if (this.postStatus == PostStatus.COMPLETED) {
this.finishedAt = LocalDateTime.now();
}
Expand All @@ -107,6 +116,13 @@ public void decreaseTotalLike() {
this.totalLike--;
}

public void increaseTotalComment() {
this.totalComment++;
if (totalComment.equals(MAX_COMMENTS)) {
this.updatePostStatus(PostStatus.COMPLETED);
}
}

public void updatePost(ConsultCategory consultCategory, String title, String content,
Boolean isCompleted, Customer customer) {
checkUpdatability();
Expand All @@ -118,7 +134,7 @@ public void updatePost(ConsultCategory consultCategory, String title, String con
this.isCompleted = isCompleted;

if (isCompleted) {
this.postStatus = PostStatus.PROCEEDING;
updatePostStatus(PostStatus.PROCEEDING);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.example.sharemind.post.dto.response;

import com.example.sharemind.comment.domain.Comment;
import com.example.sharemind.global.utils.TimeUtil;
import com.example.sharemind.post.domain.Post;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import lombok.Builder;
import lombok.Getter;

@Getter
public class PostGetCounselorListResponse {

@Schema(description = "일대다 질문 아이디")
private final Long postId;

@Schema(description = "제목")
private final String title;

@Schema(description = "상담 내용")
private final String content;

@Schema(description = "상담 카테고리")
private final String consultCategory;

@Schema(description = "공개/비공개 여부")
private final Boolean isPublic;

@Schema(description = "좋아요 수")
private final Long totalLike;

@Schema(description = "스크랩 수")
private final Long totalScrap;

@Schema(description = "답변 수")
private final Long totalComment;

@Column(name = "게시글 등록 일자")
private final String publishedAt;

@Column(name = "셰어 채택 여부")
private final Boolean isChosen;

@Builder
public PostGetCounselorListResponse(Long postId, String title, String content, String consultCategory,
Boolean isPublic, Long totalLike, Long totalScrap, Long totalComment,
String publishedAt, Boolean isChosen) {
this.postId = postId;
this.title = title;
this.content = content;
this.consultCategory = consultCategory;
this.isPublic = isPublic;
this.totalLike = totalLike;
this.totalScrap = totalScrap;
this.totalComment = totalComment;
this.publishedAt = publishedAt;
this.isChosen = isChosen;
}

public static PostGetCounselorListResponse of(Post post, Comment comment) {
return PostGetCounselorListResponse.builder()
.postId(post.getPostId())
.title(post.getTitle())
.content(post.getContent())
.consultCategory(post.getConsultCategory().getDisplayName())
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.publishedAt(TimeUtil.getUpdatedAt(post.getPublishedAt()))
.isChosen(comment.getIsChosen())
.build();
}
}
Loading

0 comments on commit d36bd7e

Please sign in to comment.