Skip to content

Commit

Permalink
Merge pull request #16 from whatever-mentoring/feature/3-scrapPost
Browse files Browse the repository at this point in the history
[feature/3-scrapPost] 쥬시글 스크랩/취소 API
  • Loading branch information
Haeun-Y committed Sep 18, 2023
2 parents e8203cd + 4f040e2 commit 0a39431
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AnswerService {
*/
public void addAnswer(Long userIdx, PostAnswerReq postAnswerReq) throws BaseException {
try {
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE);
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER));
Post post = postRepository.findById(postAnswerReq.getPostIdx()).orElseThrow(() -> new BaseException(INVALID_POST_IDX));
if (user.getRole().equals(Role.SINY)) {
Answer answer = Answer.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public enum BaseResponseStatus {

// question(2500-2501)

// scrap(2600-2699)
ZERO_SCRAP_COUNT(false, 2600, "이미 스크랩수가 0입니다."),

/**
* 3000: Response 오류
*/
Expand All @@ -46,6 +49,8 @@ public enum BaseResponseStatus {

// question(3500-3599)

// scrap(3600-3699)

/**
* 4000: DB, Server 오류
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.SUCCESS;

@RestController
@RequestMapping("/posts")
@RequiredArgsConstructor
Expand All @@ -22,7 +23,7 @@ public class PostController {
*/
@ResponseBody
@GetMapping("")
public BaseResponse<Page<GetPostsRes>> getPosts(@PageableDefault(size = 10) Pageable page,
public BaseResponse<Page<GetPostsRes>> getPosts(Pageable page,
@RequestParam(required = false) String category) {
try {
if (category.isBlank()) { // 전체 조회
Expand All @@ -45,4 +46,18 @@ public BaseResponse<GetPostRes> getPost(@PathVariable Long postIdx, Long userIdx
return new BaseResponse<>(e.getStatus());
}
}

/**
* [POST] 스크랩/취소
*/
@ResponseBody
@GetMapping("/{postIdx}")
public BaseResponse<String> scrapPost(@PathVariable Long postIdx, Long userIdx) { // TODO: 추후 getUserIdx로 수정
try {
postService.scrapPost(postIdx, userIdx);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/ewhatever/qna/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public class Post extends BaseEntity {
@ColumnDefault("false")
@Builder.Default
private Boolean isJuicy = false;

public void setScrapCount(Long scrapCount) {
this.scrapCount = scrapCount;
}
}


81 changes: 73 additions & 8 deletions src/main/java/com/ewhatever/qna/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
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.entity.Scrap;
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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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;
import static com.ewhatever.qna.common.Constant.Status.INACTIVE;

@Service
@RequiredArgsConstructor
Expand All @@ -37,6 +40,10 @@ public class PostService {

/**
* 쥬시글 전체 목록 조회
*
* @param page
* @return Page<GetPostsRes>
* @throws BaseException
*/
public Page<GetPostsRes> getPosts(Pageable page) throws BaseException {
try {
Expand All @@ -53,28 +60,34 @@ public Page<GetPostsRes> getPosts(Pageable page) throws BaseException {
}
}

// 카드 리스트 조회
private List<String> getCardList(Post post) {
List<String> cardList = new ArrayList<>();

cardList.add(post.getTitle());
cardList.add(post.getContent());
List<Answer> answers = answerRepository.findAllByPost(post);
for (Answer answer : answers) {
cardList.add(answer.getContent());
cardList.add(answer.getContent());
}
return cardList;
}

/**
* 쥬시글 카테고리 기반 목록 조회
*
* @param category
* @param page
* @return Page<GetPostsRes>
* @throws BaseException
*/
public Page<GetPostsRes> getPostsByCategory(String category, Pageable page) throws BaseException {
try {
Category categoryName = Category.valueOf(category.toUpperCase());
if (categoryName != null) {
if (categoryName != null) { //TODO: 삭제 필요. IllegalArgumentException 처리 필요.
boolean postExists = postRepository.existsByCategory(categoryName);
if (postExists) {
Page<Post> postPage = postRepository.findByCategory(categoryName, page);
Page<Post> postPage = postRepository.findByCategory(categoryName, page); //TODO: 쥬시글 여부 추가
return postPage.map(post -> new GetPostsRes(
post.getCategory().name(),
post.getLastModifiedDate(),
Expand All @@ -93,11 +106,16 @@ public Page<GetPostsRes> getPostsByCategory(String category, Pageable page) thro

/**
* 쥬시글 상세 조회
*
* @param postIdx
* @param userIdx
* @return GetPostRes
* @throws BaseException
*/
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);
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER));
return new GetPostRes(post.getCategory().toString(), getCardList(post), post.getLastModifiedDate(),
post.getCommentCount(), post.getScrapCount(), isScrap(user, post), getCommentList(post, user));

Expand All @@ -119,7 +137,7 @@ private List<GetPostRes.CommentDto> getCommentList(Post post, User user) {
List<Comment> comments = commentRepository.findAllByPost(post);

for (Comment comment : comments) {
GetPostRes.CommentDto commentDto = new GetPostRes.CommentDto(getWriter(comment.getWriter().getRole()), comment.getCreatedDate(),
GetPostRes.CommentDto commentDto = new GetPostRes.CommentDto(getWriter(comment.getWriter().getRole().toString()), comment.getCreatedDate(),
comment.getContent(), isWriter(user, comment));
commentList.add(commentDto);
}
Expand All @@ -133,8 +151,55 @@ private Boolean isWriter(User user, Comment comment) {
}

// 댓글 작성자
private String getWriter(Role role) {
private String getWriter(String role) {
if (role.equals(SENIOR)) return "익명의 시니";
else return "익명의 쥬니";
}
}


/**
* 스크랩/취소
* @param postIdx
* @param userIdx
* @throws BaseException
*/
@Transactional(rollbackFor = Exception.class)
public void scrapPost(Long postIdx, Long userIdx) throws BaseException {
try {
Post post = postRepository.findById(postIdx).orElseThrow(() -> new BaseException(INVALID_POST_IDX));
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER));
Boolean existsByPostAndUser = scrapRepository.existsByPostAndUser(post, user);

if (existsByPostAndUser) { // 스크랩 존재
Scrap scrap = scrapRepository.findByPostAndUser(post, user);
if (scrap.getStatus().equals(ACTIVE)) { // ACTIVE -> INACTIVE
Long currentScrapCount = Optional.ofNullable(post.getScrapCount()).orElse(0L);
if (currentScrapCount > 0) {
post.setScrapCount(currentScrapCount - 1L);
} else throw new BaseException(ZERO_SCRAP_COUNT);
scrap.setStatus(INACTIVE);

} else { // INACTIVE -> ACTIVE
post.setScrapCount(post.getScrapCount() + 1L);
scrap.setStatus(ACTIVE);

}
postRepository.save(post);
scrapRepository.save(scrap);
} else {// 스크랩 X -> 스크랩 생성(ACTIVE)
Scrap newScrap = Scrap.builder()
.post(post)
.user(user)
.build();
post.setScrapCount(post.getScrapCount() + 1L);

scrapRepository.save(newScrap);
postRepository.save(post);
}
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class QuestionService {
*/
public void addQuestion(Long userIdx, PostQuestionReq postQuestionReq) throws BaseException {
try {
User questioner = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE);
User questioner = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER));
Category category = Category.valueOf(postQuestionReq.getCategory());

if (questioner.getRole().equals(SINY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ 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);
Boolean existsByPostAndUser(Post post, User user);
Scrap findByPostAndUser(Post post, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserIdxAndStatusEquals(Long userIdx, String status);
Optional<User> findByUserIdxAndStatusEquals(Long userIdx, String status);
}

0 comments on commit 0a39431

Please sign in to comment.