Skip to content

Commit

Permalink
Merge pull request #15 from whatever-mentoring/feature/3-getPost
Browse files Browse the repository at this point in the history
[feature/3-getPost] ์ฅฌ์‹œ๊ธ€ ์ƒ์„ธ ์กฐํšŒ API
  • Loading branch information
Haeun-Y authored Sep 18, 2023
2 parents 8295f09 + d743a36 commit e8203cd
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 13 deletions.
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);
}

0 comments on commit e8203cd

Please sign in to comment.