Skip to content

Commit

Permalink
Merge pull request #149 from sharemindteam/refactor/148-post-refactor
Browse files Browse the repository at this point in the history
refactor: 일대다 상담 질문 단건 조회 리팩토링
  • Loading branch information
letskuku authored Mar 22, 2024
2 parents 5668e16 + 9175856 commit c152334
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
Expand Down Expand Up @@ -54,6 +55,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/api/v1/counselors/all/**", "/api/v1/searchWords/results", "/api/v1/reviews/all/**").permitAll()
.requestMatchers("/index.html", "/favicon.ico", "/chat/**", "/customer.html",
"/counselor.html").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/posts/{postId}").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 @@ -19,7 +19,7 @@ public interface PostService {

PostGetIsSavedResponse getIsSaved(Long postId);

PostGetResponse getPost(Long postId);
PostGetResponse getPost(Long postId, Long customerId);

List<PostGetResponse> getPostsByCustomer(Boolean filter, Long postId, Long customerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ public PostGetIsSavedResponse getIsSaved(Long postId) {
}

@Override
public PostGetResponse getPost(Long postId) {
return PostGetResponse.of(getPostByPostId(postId));
public PostGetResponse getPost(Long postId, Long customerId) {
Post post = getPostByPostId(postId);

if (customerId != 0) {
customerService.getCustomerByCustomerId(customerId);
}
post.checkReadAuthority(customerId); // TODO: 비공개 상담에 답변 단 판매자도 볼 수 있게 해야함

return PostGetResponse.of(post);
}

@Override
Expand Down
6 changes: 6 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 @@ -102,6 +102,12 @@ public void updatePost(ConsultCategory consultCategory, String title, String con
}
}

public void checkReadAuthority(Long customerId) {
if (!this.isPublic && !this.customer.getCustomerId().equals(customerId)) {
throw new PostException(PostErrorCode.POST_ACCESS_DENIED);
}
}

private void setIsPaid(Boolean isPublic) {
if (isPublic) {
this.isPaid = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class PostGetResponse {
@Schema(description = "일대다 질문 아이디")
private final Long postId;

@Schema(description = "상담 카테고리", example = "연애갈등")
private final String consultCategory;

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

Expand All @@ -30,47 +27,47 @@ 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 consultCategory, String title, String content,
Boolean isPublic, Long totalLike, Long totalScrap, String updatedAt) {
public PostGetResponse(Long postId, String title, String content, Boolean isPublic,
Long totalLike, Long totalScrap, Long totalComment, 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) {
String consultCategory = post.getConsultCategory() == null ? null
: post.getConsultCategory().getDisplayName();

return PostGetResponse.builder()
.postId(post.getPostId())
.consultCategory(consultCategory)
.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())
.consultCategory(null)
.title(null)
.content(null)
.isPublic(post.getIsPublic())
.totalLike(post.getTotalLike())
.totalScrap(post.getTotalScrap())
.totalComment(post.getTotalComment())
.updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum PostErrorCode {
POST_NOT_FOUND(HttpStatus.NOT_FOUND, "일대다 상담이 존재하지 않습니다."),
POST_ALREADY_PAID(HttpStatus.BAD_REQUEST, "이미 결제 완료된 일대다 상담입니다."),
POST_MODIFY_DENIED(HttpStatus.FORBIDDEN, "일대다 상담 질문 작성 권한이 없습니다."),
POST_ACCESS_DENIED(HttpStatus.FORBIDDEN, "일대다 상담 질문 접근 권한이 없습니다."),
POST_ALREADY_COMPLETED(HttpStatus.BAD_REQUEST, "이미 일대다 상담 질문이 최종 제출되었습니다.");

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ public ResponseEntity<PostGetIsSavedResponse> getIsSaved(@PathVariable Long post
return ResponseEntity.ok(postService.getIsSaved(postId));
}

@Operation(summary = "일대다 상담 질문 단건 조회", description = "일대다 상담 질문 단건 조회")
@Operation(summary = "일대다 상담 질문 단건 조회",
description = "- 일대다 상담 질문 단건 조회\n - 로그인한 사용자일 경우 헤더에 accessToken을 넣어주세요")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "403", description = "접근 권한이 없는 상담(다른 회원의 비공개 상담 접근 시도)",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = CustomExceptionResponse.class))
),
@ApiResponse(responseCode = "404", description = "존재하지 않는 일대다 질문 아이디로 요청됨",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = CustomExceptionResponse.class))
Expand All @@ -111,8 +116,10 @@ public ResponseEntity<PostGetIsSavedResponse> getIsSaved(@PathVariable Long post
@Parameter(name = "postId", description = "일대다 질문 아이디")
})
@GetMapping("/{postId}")
public ResponseEntity<PostGetResponse> getPost(@PathVariable Long postId) {
return ResponseEntity.ok(postService.getPost(postId));
public ResponseEntity<PostGetResponse> getPost(@PathVariable Long postId,
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
return ResponseEntity.ok(postService.getPost(postId,
customUserDetails == null ? 0 : customUserDetails.getCustomer().getCustomerId()));
}

@Operation(summary = "구매자 본인 일대다 상담 리스트 조회", description = """
Expand Down

0 comments on commit c152334

Please sign in to comment.