Skip to content

Commit

Permalink
Merge pull request #151 from sharemindteam/feature/147-post-customer-…
Browse files Browse the repository at this point in the history
…public

feat: 구매자 공개상담 탭 api 구현
  • Loading branch information
letskuku authored Mar 24, 2024
2 parents 98aa901 + 9456d05 commit 530a163
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/index.html", "/favicon.ico", "/chat/**", "/customer.html",
"/counselor.html").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/posts/{postId}").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/posts/customers/public/**").permitAll()
.requestMatchers("/api/v1/admins/**").hasRole(ROLE_ADMIN)
.requestMatchers("/api/v1/letters/counselors/**", "/api/v1/reviews/counselors**").hasRole(ROLE_COUNSELOR)
.requestMatchers("/api/v1/chats/counselors/**").hasRole(ROLE_COUNSELOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
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 java.time.LocalDateTime;
import java.util.List;

public interface PostService {
Expand All @@ -21,7 +24,11 @@ public interface PostService {

PostGetResponse getPost(Long postId, Long customerId);

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

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

List<PostGetPopularityResponse> getPopularityPosts();

List<Long> getRandomPosts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
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.exception.PostErrorCode;
import com.example.sharemind.post.exception.PostException;
import com.example.sharemind.post.repository.PostRepository;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +30,7 @@
public class PostServiceImpl implements PostService {

private static final int POST_CUSTOMER_PAGE_SIZE = 4;
private static final int POST_POPULARITY_SIZE = 3;

private final CustomerService customerService;
private final CounselorService counselorService;
Expand Down Expand Up @@ -81,19 +86,37 @@ public PostGetResponse getPost(Long postId, Long customerId) {
if (customerId != 0) {
customerService.getCustomerByCustomerId(customerId);
}
post.checkReadAuthority(customerId); // TODO: 비공개 상담에 답변 단 판매자도 볼 수 있게 해야함
post.checkReadAuthority(customerId);

return PostGetResponse.of(post);
}

@Override
public List<PostGetResponse> getPostsByCustomer(Boolean filter, Long postId, Long customerId) {
public List<PostGetListResponse> getPostsByCustomer(Boolean filter, Long postId,
Long customerId) {
Customer customer = customerService.getCustomerByCustomerId(customerId);

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

@Override
public List<PostGetListResponse> getPublicPostsByCustomer(Long postId,
LocalDateTime finishedAt) {
return postRepository.findAllByIsPublicAndIsActivatedIsTrue(postId, finishedAt,
POST_CUSTOMER_PAGE_SIZE).stream()
.map(PostGetListResponse::of)
.toList();
}

@Override
public List<PostGetPopularityResponse> getPopularityPosts() {
return postRepository.findPopularityPosts(LocalDate.now().minusWeeks(1),
POST_POPULARITY_SIZE).stream()
.map(PostGetPopularityResponse::of)
.toList();
}

Expand All @@ -104,33 +127,35 @@ public List<Long> getRandomPosts() {

@Override
public PostGetResponse getCounselorPostContent(Long postId, Long customerId) {

Post post = checkAndGetCounselorPost(postId, customerId);

return PostGetResponse.of(post);
}

private Post getProceedingPost(Long postId) {
Post post = getPostByPostId(postId);

post.checkPostProceeding();
return post;
}

@Override
public Post checkAndGetCounselorPost(Long postId, Long customerId) {
if (checkCounselorReadAuthority(postId, customerId))
if (checkCounselorReadAuthority(postId, customerId)) {
return getPostByPostId(postId);
}

return getProceedingPost(postId);
}

private Boolean checkCounselorReadAuthority(Long postId, Long customerId){
private Boolean checkCounselorReadAuthority(Long postId, Long customerId) {
Post post = getPostByPostId(postId);

Counselor counselor = counselorService.getCounselorByCustomerId(customerId);

Comment comment = commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post, counselor);
Comment comment = commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post,
counselor);

return comment != null;
}

private Post getProceedingPost(Long postId) {
Post post = getPostByPostId(postId);

post.checkPostProceeding();
return post;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/sharemind/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -71,6 +72,9 @@ public class Post extends BaseEntity {
@Column(name = "is_completed")
private Boolean isCompleted;

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

@Builder
public Post(Customer customer, Long cost, Boolean isPublic) {
this.customer = customer;
Expand All @@ -89,6 +93,10 @@ public void updateIsPaid() {

public void updatePostStatus(PostStatus postStatus) {
this.postStatus = postStatus;

if (this.postStatus == PostStatus.COMPLETED) {
this.finishedAt = LocalDateTime.now();
}
}

public void updatePost(ConsultCategory consultCategory, String title, String content,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.example.sharemind.post.dto.response;

import com.example.sharemind.global.utils.TimeUtil;
import com.example.sharemind.post.domain.Post;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;

@Getter
public class PostGetListResponse {

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

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

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

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

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

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

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

@Schema(description = "마지막 업데이트 일시", example = "8분 전")
private final String updatedAt;

@Schema(description = "답변 완료 일시")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "Asia/Seoul")
private final LocalDateTime finishedAt;

@Builder
public PostGetListResponse(Long postId, String title, String content, Boolean isPublic,
Long totalLike, Long totalScrap, Long totalComment, String updatedAt,
LocalDateTime finishedAt) {
this.postId = postId;
this.title = title;
this.content = content;
this.isPublic = isPublic;
this.totalLike = totalLike;
this.totalScrap = totalScrap;
this.totalComment = totalComment;
this.updatedAt = updatedAt;
this.finishedAt = finishedAt;
}

public static PostGetListResponse of(Post post) {
return PostGetListResponse.builder()
.postId(post.getPostId())
.title(post.getTitle())
.content(post.getContent())
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt()))
.finishedAt(post.getFinishedAt())
.build();
}

public static PostGetListResponse ofIsNotCompleted(Post post) {
return PostGetListResponse.builder()
.postId(post.getPostId())
.title(null)
.content(null)
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt()))
.finishedAt(post.getFinishedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.sharemind.post.dto.response;

import com.example.sharemind.post.domain.Post;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

@Getter
public class PostGetPopularityResponse {

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

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

@Builder
public PostGetPopularityResponse(Long postId, String title) {
this.postId = postId;
this.title = title;
}

public static PostGetPopularityResponse of(Post post) {
return PostGetPopularityResponse.builder()
.postId(post.getPostId())
.title(post.getTitle())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class PostGetResponse {
@Schema(description = "일대다 질문 아이디")
private final Long postId;

@Schema(description = "상담 카테고리", example = "권태기")
private final String consultCategory;

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

Expand All @@ -27,47 +30,31 @@ public class PostGetResponse {
@Schema(description = "스크랩 수")
private final Long totalScrap;

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

@Schema(description = "마지막 업데이트 일시", example = "8분 전")
private final String updatedAt;

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

public static PostGetResponse of(Post post) {
return PostGetResponse.builder()
.postId(post.getPostId())
.consultCategory(post.getConsultCategory().getDisplayName())
.title(post.getTitle())
.content(post.getContent())
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt()))
.build();
}

public static PostGetResponse ofIsNotCompleted(Post post) {
return PostGetResponse.builder()
.postId(post.getPostId())
.title(null)
.content(null)
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt()))
.build();
}
Expand Down
Loading

0 comments on commit 530a163

Please sign in to comment.