Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/3-getPost] 쥬시글 상세 조회 API #15

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.stereotype.Service;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.*;
import static com.ewhatever.qna.common.Constant.ACTIVE;
import static com.ewhatever.qna.common.Constant.Status.ACTIVE;

@Service
public class AnswerService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.ewhatever.qna.comment.repository;

import com.ewhatever.qna.comment.entity.Comment;
import com.ewhatever.qna.post.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Long countByWriter_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Comment> findByWriter_UserIdxAndStatusEquals(Long userIdx, String status, Pageable pageable);
List<Comment> findAllByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.transaction.annotation.Transactional;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.*;
import static com.ewhatever.qna.common.Constant.INACTIVE;
import static com.ewhatever.qna.common.Constant.Status.INACTIVE;

@Service
@RequiredArgsConstructor
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/ewhatever/qna/common/Constant.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.ewhatever.qna.common;

public class Constant {
public static final String ACTIVE = "active";
public static final String INACTIVE = "inactive";
public static final String LOGOUT = "logout";
public static class Status {
public static final String ACTIVE = "active";
public static final String INACTIVE = "inactive";
public static final String LOGOUT = "logout";
}

public static class Role {
public static final String SENIOR = "senior";
public static final String JUNIOR = "junior";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import com.ewhatever.qna.post.dto.GetPostRes;
import com.ewhatever.qna.post.dto.GetPostsRes;
import com.ewhatever.qna.post.service.PostService;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,7 +18,7 @@ public class PostController {
private final PostService postService;

/**
* [GET] 주씨글 목록 조회
* [GET] 쥬시글 목록 조회
*/
@ResponseBody
@GetMapping("")
Expand All @@ -31,4 +32,17 @@ public BaseResponse<Page<GetPostsRes>> getPosts(@PageableDefault(size = 10) Page
return new BaseResponse<>(e.getStatus());
}
}

/**
* [GET] 쥬시글 상세 조회
*/
@ResponseBody
@GetMapping("/{postIdx}")
public BaseResponse<GetPostRes> getPost(@PathVariable Long postIdx, Long userIdx) { // TODO: 추후 getUserIdx로 수정
try {
return new BaseResponse<>(postService.getPost(postIdx, userIdx));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/ewhatever/qna/post/dto/GetPostRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ewhatever.qna.post.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GetPostRes {
private String category;
private List<String> cardList; // 질문 제목, 질문 상세, 답변(0-3개)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd", timezone = "Asia/Seoul")
private LocalDateTime date;
private Long commentCount;
private Long scrapCount;
private Boolean isScrap;
private List<CommentDto> commentList;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class CommentDto {
private String writer;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd HH:mm", timezone = "Asia/Seoul")
private LocalDateTime date;
private String content;
private Boolean isWriter;
}
}
4 changes: 0 additions & 4 deletions src/main/java/com/ewhatever/qna/post/dto/PostResponse.java

This file was deleted.

63 changes: 61 additions & 2 deletions src/main/java/com/ewhatever/qna/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import com.ewhatever.qna.answer.entity.Answer;
import com.ewhatever.qna.answer.repository.AnswerRepository;
import com.ewhatever.qna.comment.entity.Comment;
import com.ewhatever.qna.comment.repository.CommentRepository;
import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.enums.Category;
import com.ewhatever.qna.common.enums.Role;
import com.ewhatever.qna.post.dto.GetPostRes;
import com.ewhatever.qna.post.dto.GetPostsRes;
import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.post.repository.PostRepository;
import com.ewhatever.qna.scrap.repository.ScrapRepository;
import com.ewhatever.qna.user.entity.User;
import com.ewhatever.qna.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -16,15 +23,20 @@
import java.util.List;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.*;
import static com.ewhatever.qna.common.Constant.Role.SENIOR;
import static com.ewhatever.qna.common.Constant.Status.ACTIVE;

@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final AnswerRepository answerRepository;
private final UserRepository userRepository;
private final ScrapRepository scrapRepository;
private final CommentRepository commentRepository;

/**
* 주씨글 전체 목록 조회
* 쥬시글 전체 목록 조회
*/
public Page<GetPostsRes> getPosts(Pageable page) throws BaseException {
try {
Expand Down Expand Up @@ -54,7 +66,7 @@ private List<String> getCardList(Post post) {
}

/**
* 주씨글 카테고리 기반 목록 조회
* 쥬시글 카테고리 기반 목록 조회
*/
public Page<GetPostsRes> getPostsByCategory(String category, Pageable page) throws BaseException {
try {
Expand All @@ -78,4 +90,51 @@ public Page<GetPostsRes> getPostsByCategory(String category, Pageable page) thro
throw new BaseException(DATABASE_ERROR);
}
}

/**
* 쥬시글 상세 조회
*/
public GetPostRes getPost(Long postIdx, Long userIdx) throws BaseException {
try {
Post post = postRepository.findById(postIdx).orElseThrow(() -> new BaseException(INVALID_POST_IDX));
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE);
return new GetPostRes(post.getCategory().toString(), getCardList(post), post.getLastModifiedDate(),
post.getCommentCount(), post.getScrapCount(), isScrap(user, post), getCommentList(post, user));

} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

// 스크랩 여부
private Boolean isScrap(User user, Post post) {
return scrapRepository.existsByPostAndUserAndStatusEquals(post, user, ACTIVE);
}

// 댓글 list 조회
private List<GetPostRes.CommentDto> getCommentList(Post post, User user) {
List<GetPostRes.CommentDto> commentList = new ArrayList<>();
List<Comment> comments = commentRepository.findAllByPost(post);

for (Comment comment : comments) {
GetPostRes.CommentDto commentDto = new GetPostRes.CommentDto(getWriter(comment.getWriter().getRole()), comment.getCreatedDate(),
comment.getContent(), isWriter(user, comment));
commentList.add(commentDto);
}
return commentList;
}

// 댓글 작성자 여부
private Boolean isWriter(User user, Comment comment) {
if (comment.getWriter().equals(user)) return true;
else return false;
}

// 댓글 작성자
private String getWriter(Role role) {
if (role.equals(SENIOR)) return "익명의 시니";
else return "익명의 쥬니";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.stereotype.Service;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.*;
import static com.ewhatever.qna.common.Constant.ACTIVE;
import static com.ewhatever.qna.common.Constant.Status.ACTIVE;
import static com.ewhatever.qna.common.enums.Role.SINY;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.ewhatever.qna.scrap.repository;

import com.ewhatever.qna.post.entity.Post;
import com.ewhatever.qna.scrap.entity.Scrap;
import com.ewhatever.qna.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ScrapRepository extends JpaRepository<Scrap, Long> {
Long countByUser_UserIdxAndStatusEquals(Long userIdx, String status);
Page<Scrap> findByUser_UserIdxAndStatusEquals(Long userIdx, String status, Pageable pageable);
Boolean existsByPostAndUserAndStatusEquals(Post post, User user, String status);
}