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

아티클 필터링 추가 #1547

Closed
wants to merge 2 commits into from
Closed
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
@@ -1,10 +1,5 @@
package wooteco.prolog.article.application;

import static java.lang.Boolean.TRUE;
import static java.util.stream.Collectors.toList;
import static wooteco.prolog.common.exception.BadRequestCode.ARTICLE_NOT_FOUND_EXCEPTION;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -17,6 +12,13 @@
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.member.domain.MemberGroupType;

import java.util.List;

import static java.lang.Boolean.TRUE;
import static java.util.stream.Collectors.toList;
import static wooteco.prolog.common.exception.BadRequestCode.ARTICLE_NOT_FOUND_EXCEPTION;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -79,4 +81,16 @@ public void bookmarkArticle(final Long id, final LoginMember loginMember,
article.removeBookmark(member);
}
}

public List<ArticleResponse> filter(final LoginMember member, final MemberGroupType course, final boolean onlyBookmarked) {
if (member.isMember() && onlyBookmarked) {
return articleRepository.findArticlesByCourseAndMember(course.getGroupName(), member.getId()).stream()
.map(ArticleResponse::from)
.collect(toList());
}

return articleRepository.findArticlesByCourse(course.getGroupName()).stream()
.map(ArticleResponse::from)
.collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package wooteco.prolog.article.domain.repository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import wooteco.prolog.article.domain.Article;

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

public interface ArticleRepository extends JpaRepository<Article, Long> {

List<Article> findAllByOrderByCreatedAtDesc();

@Query("select a from Article a join fetch a.articleBookmarks where a.id = :id")
Optional<Article> findFetchById(@Param("id") final Long id);

@Query("SELECT DISTINCT a FROM Article a " +
"JOIN GroupMember gm ON a.member.id = gm.member.id " +
"JOIN gm.group mg " +
"WHERE mg.name LIKE %:course")
List<Article> findArticlesByCourse(@Param("course") String course);

@Query("SELECT DISTINCT a FROM Article a " +
"JOIN GroupMember gm ON a.member.id = gm.member.id " +
"JOIN gm.group mg " +
"JOIN a.articleBookmarks.articleBookmarks ab " +
"WHERE mg.name LIKE %:course AND ab.memberId = :memberId")
List<Article> findArticlesByCourseAndMember(@Param("course") String course, @Param("memberId") Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package wooteco.prolog.article.ui;

import java.net.URI;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -11,11 +9,15 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wooteco.prolog.article.application.ArticleService;
import wooteco.prolog.article.domain.ArticleBookmark;
import wooteco.prolog.login.domain.AuthMemberPrincipal;
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.member.domain.MemberGroupType;

import java.net.URI;
import java.util.List;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -59,4 +61,13 @@ public ResponseEntity<Void> bookmarkArticle(@PathVariable final Long id,
articleService.bookmarkArticle(id, member, request.getBookmark());
return ResponseEntity.ok().build();
}

@GetMapping("/filter")
public ResponseEntity<List<ArticleResponse>> filterArticles(@AuthMemberPrincipal final LoginMember member,
@RequestParam("course") final MemberGroupType course,
@RequestParam("onlyBookmarked") boolean onlyBookmarked) {
final List<ArticleResponse> articleResponses = articleService.filter(member, course, onlyBookmarked);

return ResponseEntity.ok(articleResponses);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package wooteco.prolog.common;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import wooteco.prolog.member.domain.MemberGroupType;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;

@Configuration
public class WebConverterConfig implements WebMvcConfigurer {
Expand All @@ -18,5 +20,9 @@ public void addFormatters(FormatterRegistry registry) {
registry.addConverter(String.class, LocalDate.class,
source -> LocalDate.parse(source, DateTimeFormatter.BASIC_ISO_DATE)
);

registry.addConverter(String.class, MemberGroupType.class,
source -> MemberGroupType.valueOf(source.toUpperCase())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package wooteco.prolog.article.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static wooteco.prolog.login.ui.LoginMember.Authority.MEMBER;
import static wooteco.prolog.member.domain.Role.CREW;

import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -24,12 +15,28 @@
import wooteco.prolog.article.domain.Url;
import wooteco.prolog.article.domain.repository.ArticleRepository;
import wooteco.prolog.article.ui.ArticleRequest;
import wooteco.prolog.article.ui.ArticleResponse;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.login.ui.LoginMember;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.member.domain.MemberGroupType;
import wooteco.prolog.member.domain.Role;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static wooteco.prolog.login.ui.LoginMember.Authority.ANONYMOUS;
import static wooteco.prolog.login.ui.LoginMember.Authority.MEMBER;
import static wooteco.prolog.member.domain.Role.CREW;

@ExtendWith(MockitoExtension.class)
class ArticleServiceTest {

Expand Down Expand Up @@ -232,4 +239,40 @@ void remove() {
.isTrue();
}
}

@DisplayName("비로그인 사용자가 백엔드 아티클을 필터링 한다.")
@Test
void filter() {
//given
final Member member = new Member(1L, "username", "nickname", CREW, 1L, "url");
final Article article = new Article(member, new Title("title"), new Url("url"), new ImageUrl("imageUrl"));
final LoginMember unLoginMember = new LoginMember(1L, ANONYMOUS);

when(articleRepository.findArticlesByCourse(any())).thenReturn(Arrays.asList(article));

//when
final List<ArticleResponse> articleResponses = articleService.filter(unLoginMember, MemberGroupType.BACKEND, false);

//then
verify(articleRepository).findArticlesByCourse(any());
Assertions.assertThat(articleResponses.get(0).getTitle()).isEqualTo(article.getTitle().getTitle());
}

@DisplayName("로그인 유저가 북마크 백엔드 아티클을 필터링 한다.")
@Test
void filter_isBookmarked() {
//given
final Member member = new Member(1L, "username", "nickname", CREW, 1L, "url");
final Article article = new Article(member, new Title("title"), new Url("url"), new ImageUrl("imageUrl"));
final LoginMember loginMember = new LoginMember(1L, MEMBER);

when(articleRepository.findArticlesByCourseAndMember(any(), any())).thenReturn(Arrays.asList(article));

//when
final List<ArticleResponse> articleResponses = articleService.filter(loginMember, MemberGroupType.BACKEND, true);

//then
verify(articleRepository).findArticlesByCourseAndMember(any(), any());
Assertions.assertThat(articleResponses.get(0).getTitle()).isEqualTo(article.getTitle().getTitle());
}
}