Skip to content

Commit

Permalink
배포후 발생한 이슈들을 수정한다
Browse files Browse the repository at this point in the history
배포후 발생한 이슈들을 수정한다
  • Loading branch information
liswktjs authored Oct 13, 2022
2 parents d84de95 + 65e12d0 commit d064d0a
Show file tree
Hide file tree
Showing 95 changed files with 1,587 additions and 1,773 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
import com.woowacourse.gongseek.article.domain.repository.ArticleRepository;
import com.woowacourse.gongseek.article.domain.repository.ArticleRepositoryCustom;
import com.woowacourse.gongseek.article.domain.repository.PagingArticleRepository;
import com.woowacourse.gongseek.article.domain.repository.dto.ArticleDto;
import com.woowacourse.gongseek.article.domain.repository.dto.ArticlePreviewDto;
import com.woowacourse.gongseek.article.exception.ArticleNotFoundException;
import com.woowacourse.gongseek.article.presentation.dto.ArticleIdResponse;
import com.woowacourse.gongseek.article.presentation.dto.ArticlePageResponse;
import com.woowacourse.gongseek.article.presentation.dto.ArticlePreviewResponse;
import com.woowacourse.gongseek.article.presentation.dto.ArticleRequest;
import com.woowacourse.gongseek.article.presentation.dto.ArticleResponse;
import com.woowacourse.gongseek.article.presentation.dto.ArticleUpdateRequest;
import com.woowacourse.gongseek.article.presentation.dto.ArticleUpdateResponse;
import com.woowacourse.gongseek.auth.exception.NotAuthorException;
import com.woowacourse.gongseek.auth.exception.NotMemberException;
import com.woowacourse.gongseek.auth.presentation.dto.AppMember;
import com.woowacourse.gongseek.comment.domain.repository.CommentRepository;
import com.woowacourse.gongseek.like.domain.repository.LikeRepository;
import com.woowacourse.gongseek.like.presentation.dto.LikeResponse;
import com.woowacourse.gongseek.member.domain.Member;
import com.woowacourse.gongseek.member.domain.repository.MemberRepository;
import com.woowacourse.gongseek.member.exception.MemberNotFoundException;
import com.woowacourse.gongseek.tag.application.TagService;
import com.woowacourse.gongseek.tag.domain.Tags;
import com.woowacourse.gongseek.vote.domain.repository.VoteRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -40,9 +42,12 @@ public class ArticleService {
private final ArticleRepository articleRepository;
private final ArticleRepositoryCustom articleRepositoryCustom;
private final PagingArticleRepository pagingArticleRepository;
private final MemberRepository memberRepository;
private final TempArticleService tempArticleService;
private final MemberRepository memberRepository;
private final CommentRepository commentRepository;
private final VoteRepository voteRepository;
private final TagService tagService;
private final LikeRepository likeRepository;

public ArticleIdResponse save(AppMember appMember, ArticleRequest articleRequest) {
validateGuest(appMember);
Expand All @@ -68,63 +73,73 @@ private Member getMember(AppMember appMember) {
.orElseThrow(() -> new MemberNotFoundException(appMember.getPayload()));
}

public ArticleDto getOne(AppMember appMember, Long id) {
articleRepository.addViews(id);
return articleRepositoryCustom.findByIdWithAll(id, appMember.getPayload())
public ArticleResponse getOne(AppMember appMember, Long id) {
Article article = getArticle(id);

List<String> tagNames = article.getTagNames();
article.addViews();
LikeResponse likeResponse = new LikeResponse(isLike(article, appMember), getLikeCount(article));

return checkGuest(article, tagNames, appMember, voteRepository.existsByArticleId(article.getId()),
likeResponse);
}

private Article getArticle(Long id) {
return articleRepository.findByIdWithAll(id)
.orElseThrow(() -> new ArticleNotFoundException(id));
}

private boolean isLike(Article article, AppMember appMember) {
return likeRepository.existsByArticleIdAndMemberId(article.getId(), appMember.getPayload());
}

private Long getLikeCount(Article article) {
return likeRepository.countByArticleId(article.getId());
}

private ArticleResponse checkGuest(Article article, List<String> tagNames, AppMember appMember, boolean hasVote,
LikeResponse likeResponse) {
if (appMember.isGuest()) {
return ArticleResponse.of(article, tagNames, false, hasVote, likeResponse);
}
return ArticleResponse.of(article, tagNames, article.isAuthor(getMember(appMember)), hasVote, likeResponse);
}

@Transactional(readOnly = true)
public ArticlePageResponse getAll(Long cursorId, Integer cursorViews, String category, String sortType,
Pageable pageable, AppMember appMember) {
Slice<ArticlePreviewDto> articles = pagingArticleRepository.findAllByPage(cursorId, cursorViews, category,
sortType,
appMember.getPayload(), pageable);
Map<Long, List<String>> tags = findTagNames(articles);
List<ArticlePreviewResponse> articleResponses = getArticlePreviewResponses(articles, tags);
return new ArticlePageResponse(articleResponses, articles.hasNext());
}

private List<ArticlePreviewResponse> getArticlePreviewResponses(Slice<ArticlePreviewDto> articles,
Map<Long, List<String>> tags) {
return articles.stream()
.map(article -> new ArticlePreviewResponse(article, tags.get(article.getId())))
.collect(Collectors.toList());
}
Slice<Article> articles = pagingArticleRepository.findAllByPage(cursorId, cursorViews, category, sortType,
pageable);
List<ArticlePreviewResponse> responses = createResponse(appMember, articles);

private Map<Long, List<String>> findTagNames(Slice<ArticlePreviewDto> articles) {
List<Long> foundArticleIds = getArticleIds(articles);
return articleRepositoryCustom.findTags(foundArticleIds);
return new ArticlePageResponse(responses, articles.hasNext());
}

private List<Long> getArticleIds(Slice<ArticlePreviewDto> articles) {
return articles.stream()
.map(ArticlePreviewDto::getId)
private List<ArticlePreviewResponse> createResponse(AppMember appMember, Slice<Article> articles) {
return articles.getContent().stream()
.map(it -> getArticlePreviewResponse(it, appMember))
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public ArticlePageResponse getAllByLikes(Long cursorId, Long likes, String category, Pageable pageable,
AppMember appMember) {
Slice<ArticlePreviewDto> articles = pagingArticleRepository.findAllByLikes(cursorId, likes, category,
appMember.getPayload(),
pageable);
Map<Long, List<String>> tagNames = findTagNames(articles);
List<ArticlePreviewResponse> articleResponses = getArticlePreviewResponses(articles, tagNames);
return new ArticlePageResponse(articleResponses, articles.hasNext());
private ArticlePreviewResponse getArticlePreviewResponse(Article article, AppMember appMember) {
List<String> tagNames = article.getTagNames();
return ArticlePreviewResponse.of(article, tagNames, getCommentCount(article),
new LikeResponse(isLike(article, appMember), getLikeCount(article)));
}

private long getCommentCount(Article article) {
return commentRepository.countByArticleId(article.getId());
}

@Transactional(readOnly = true)
public ArticlePageResponse searchByText(Long cursorId, Pageable pageable, String searchText,
AppMember appMember) {
public ArticlePageResponse searchByText(Long cursorId, Pageable pageable, String searchText, AppMember appMember) {
if (searchText.isBlank()) {
return new ArticlePageResponse(new ArrayList<>(), false);
}
Slice<ArticlePreviewDto> articles = pagingArticleRepository.searchByContainingText(cursorId, searchText,
appMember.getPayload(), pageable);
Map<Long, List<String>> tagNames = findTagNames(articles);
List<ArticlePreviewResponse> articleResponses = getArticlePreviewResponses(articles, tagNames);
return new ArticlePageResponse(articleResponses, articles.hasNext());
Slice<Article> articles = pagingArticleRepository.searchByContainingText(cursorId, searchText, pageable);
List<ArticlePreviewResponse> responses = createResponse(appMember, articles);

return new ArticlePageResponse(responses, articles.hasNext());
}

@Transactional(readOnly = true)
Expand All @@ -133,24 +148,10 @@ public ArticlePageResponse searchByAuthor(Long cursorId, Pageable pageable, Stri
if (authorName.isBlank()) {
return new ArticlePageResponse(new ArrayList<>(), false);
}
Slice<ArticlePreviewDto> articles = pagingArticleRepository.searchByAuthor(cursorId, authorName,
appMember.getPayload(), pageable);
Map<Long, List<String>> tagNames = findTagNames(articles);
List<ArticlePreviewResponse> articleResponses = getArticlePreviewResponses(articles, tagNames);
return new ArticlePageResponse(articleResponses, articles.hasNext());
}
Slice<Article> articles = pagingArticleRepository.searchByAuthor(cursorId, authorName, pageable);

@Transactional(readOnly = true)
public ArticlePageResponse searchByTag(Long cursorId, Pageable pageable, String tagsText, AppMember appMember) {
Slice<ArticlePreviewDto> articles = pagingArticleRepository.searchByTag(cursorId, appMember.getPayload(),
extract(tagsText), pageable);
Map<Long, List<String>> tagNames = findTagNames(articles);
List<ArticlePreviewResponse> articleResponses = getArticlePreviewResponses(articles, tagNames);
return new ArticlePageResponse(articleResponses, articles.hasNext());
}

private List<String> extract(String tagsText) {
return Arrays.asList(tagsText.split(","));
List<ArticlePreviewResponse> response = createResponse(appMember, articles);
return new ArticlePageResponse(response, articles.hasNext());
}

public ArticleUpdateResponse update(AppMember appMember, ArticleUpdateRequest articleUpdateRequest, Long id) {
Expand Down Expand Up @@ -190,15 +191,32 @@ private void validateAuthor(Article article, Member member) {
}
}

private Article getArticle(Long id) {
return articleRepository.findById(id)
.orElseThrow(() -> new ArticleNotFoundException(id));
}

public void delete(AppMember appMember, Long id) {
Article article = checkAuthorization(appMember, id);
articleRepository.delete(article);
List<Long> deletedTagIds = getDeletedTagIds(article.getTagIds());
tagService.deleteAll(deletedTagIds);
}

@Transactional(readOnly = true)
public ArticlePageResponse searchByTag(Long cursorId, Pageable pageable, String tagsText, AppMember appMember) {
Slice<Article> articles = pagingArticleRepository.searchByTag(cursorId, extract(tagsText), pageable);
List<ArticlePreviewResponse> response = createResponse(appMember, articles);
return new ArticlePageResponse(response, articles.hasNext());
}

private List<String> extract(String tagsText) {
return Arrays.asList(tagsText.split(","));
}

@Transactional(readOnly = true)
public ArticlePageResponse getAllByLikes(Long cursorId, Long likes, String category, Pageable pageable,
AppMember appMember) {
Slice<Article> articles = pagingArticleRepository.findAllByLikes(cursorId, likes, category, pageable);
List<ArticlePreviewResponse> response = articles.getContent().stream()
.map(it -> getArticlePreviewResponse(it, appMember))
.collect(Collectors.toList());

return new ArticlePageResponse(response, articles.hasNext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import com.woowacourse.gongseek.article.domain.Article;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ArticleRepository extends JpaRepository<Article, Long> {

List<Article> findAllByMemberId(Long memberId);
@Query("select distinct a from Article a "
+ "join fetch a.member "
+ "left join fetch a.articleTags.value at "
+ "left join fetch at.tag "
+ "where a.id = :id")
Optional<Article> findByIdWithAll(@Param("id") Long id);

@Modifying(clearAutomatically = true)
@Query(value = "UPDATE Article a SET a.views.value = a.views.value + 1 WHERE a.id = :articleId")
void addViews(@Param("articleId") Long articleId);
List<Article> findAllByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
package com.woowacourse.gongseek.article.domain.repository;

import com.woowacourse.gongseek.article.domain.repository.dto.ArticleDto;
import com.woowacourse.gongseek.article.domain.repository.dto.MyPageArticleDto;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface ArticleRepositoryCustom {

List<MyPageArticleDto> findAllByMemberIdWithCommentCount(Long memberId);

Optional<ArticleDto> findByIdWithAll(Long id, Long memberId);

List<String> findTagNamesByArticleId(Long articleId);

boolean existsArticleByTagId(Long tagId);

Map<Long, List<String>> findTags(List<Long> articleIds);
}
Loading

0 comments on commit d064d0a

Please sign in to comment.