Skip to content

Commit

Permalink
✨ [feat] 태그로 내가 스크랩한 게시글 조회(미리보기) 서비스 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
y2hscmtk committed Aug 4, 2024
1 parent d19448f commit a120e90
Show file tree
Hide file tree
Showing 33 changed files with 55 additions and 1 deletion.
Binary file modified .gradle/8.8/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.8/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.winner_cat.domain.article.entity;

import com.winner_cat.domain.member.entity.Member;
import com.winner_cat.domain.scrap.entity.Scrap;
import com.winner_cat.global.entity.BaseEntity;
import com.winner_cat.global.enums.statuscode.ErrorStatus;
import com.winner_cat.global.exception.GeneralException;
Expand Down Expand Up @@ -32,6 +33,9 @@ public class Article extends BaseEntity {
@OneToMany(mappedBy = "article")
private List<ArticleTag> tags = new ArrayList<>();

@OneToMany(mappedBy = "article")
private List<Scrap> scrapList = new ArrayList<>();

public void changeTags(ArrayList<ArticleTag> articleTag) {
this.tags = articleTag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public interface ArticleTagRepository extends JpaRepository<ArticleTag,Long> {
// 태그 이름으로 묶어서 내림차순 추출
@Query("SELECT at.tag.tagName, COUNT(at) FROM ArticleTag at WHERE at.article IN :articles GROUP BY at.tag.tagName ORDER BY COUNT(at) DESC")
List<Object[]> findTopTagsByArticles(@Param("articles") List<Article> articles);

// 회원이 스크랩한 게시글 중 특정 태그에 해당하는 게시글 페이징 조회
@Query("SELECT at FROM ArticleTag at JOIN at.article a JOIN a.scrapList sc WHERE sc.member = :member and at.tag = :tag ORDER BY a.createdAt DESC")
Page<ArticleTag> findScrapArticleTagPageByTag(Member member, Tag tag, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


public interface ScrapRepository extends JpaRepository<Scrap, Long> {

@Query("SELECT sc FROM Scrap sc JOIN sc.article ac WHERE sc.member = :member ORDER BY ac.createdAt DESC")
Page<Scrap> findByMember(Member member, Pageable pageable);
// 회원이 게시글을 스크랩했는지 유무
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.winner_cat.domain.scrap.service;

import com.winner_cat.domain.article.dto.ArticlePreviewDto;
import com.winner_cat.domain.article.dto.TagResponseDto;
import com.winner_cat.domain.article.entity.Article;
import com.winner_cat.domain.article.entity.ArticleTag;
import com.winner_cat.domain.article.entity.Tag;
import com.winner_cat.domain.article.repository.ArticleRepository;
import com.winner_cat.domain.article.repository.ArticleTagRepository;
import com.winner_cat.domain.article.repository.TagRepository;
import com.winner_cat.domain.member.entity.Member;
import com.winner_cat.domain.member.repository.MemberRepository;
import com.winner_cat.domain.scrap.dto.ScrapDto;
Expand All @@ -22,6 +25,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
Expand All @@ -30,6 +34,8 @@ public class ScrapService {
private final ScrapRepository scrapRepository;
private final MemberRepository memberRepository;
private final ArticleRepository articleRepository;
private final TagRepository tagRepository;
private final ArticleTagRepository articleTagRepository;

public ResponseEntity<?> scrapArticle(String email, Long articleId) {
// 1. 회원 조회
Expand Down Expand Up @@ -92,6 +98,47 @@ public ResponseEntity<?> getAllMyScrapArticles(String email, Pageable pageable)
return ResponseEntity.ok().body(ApiResponse.onSuccess(result));
}

/**
* 태그로 내가 스크랩한 게시글 조회(미리보기)
*/
public ResponseEntity<?> getAllMyScrapArticlesByTag(String email, String tagName, Pageable pageable) {
// 1. 사용자 정보 얻어오기
Member member = memberRepository.findMemberByEmail(email)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));
// 2. 태그 정보 얻어오기
Tag tag = tagRepository.findByTagName(tagName)
.orElseThrow(() -> new GeneralException(ErrorStatus.TAG_NOT_FOUND));
// 3. 해당 회원이 스크랩한 태그 정보 조회
Page<ArticleTag> scrapArticleTagPageByTag = articleTagRepository.findScrapArticleTagPageByTag(member, tag, pageable);
int totalPages = scrapArticleTagPageByTag.getTotalPages();
List<ArticlePreviewDto.AllArticlePreview> articlePreviewList = new ArrayList<>();
for (ArticleTag articleTag : scrapArticleTagPageByTag.getContent()) {
Article article = articleTag.getArticle();
// 관련 태그들 얻어오기
List<TagResponseDto> tagResponseDtoList = article.getTags().stream()
.map(at -> TagResponseDto.builder()
.tagName(at.getTag().getTagName())
.colorCode(at.getTag().getColorCode())
.build())
.collect(Collectors.toList());

ArticlePreviewDto.AllArticlePreview result = ArticlePreviewDto.AllArticlePreview
.builder()
.articleId(article.getId())
.title(article.getTitle())
.tagList(tagResponseDtoList)
.build();
articlePreviewList.add(result);
}

// 4. DTO 생성 및 반환
ArticlePreviewDto.AllArticlePreviewResponse result = ArticlePreviewDto.AllArticlePreviewResponse.builder()
.totalPages(totalPages)
.articlePreviewList(articlePreviewList)
.build();
return ResponseEntity.ok(ApiResponse.onSuccess(result));
}

/**
* 스크랩 취소
*/
Expand Down

0 comments on commit a120e90

Please sign in to comment.