Skip to content

Commit

Permalink
refactor: 아티클 타입 변경 및 response 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
donghae-kim committed Sep 20, 2023
1 parent 262d414 commit b1a13d1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.article.domain.Article;
import wooteco.prolog.article.domain.ArticleFilterType;
import wooteco.prolog.article.domain.repository.ArticleRepository;
import wooteco.prolog.article.ui.ArticleRequest;
import wooteco.prolog.article.ui.ArticleResponse;
Expand All @@ -12,7 +13,6 @@
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;

Expand All @@ -38,13 +38,6 @@ public Long create(final ArticleRequest articleRequest, final LoginMember loginM
return articleRepository.save(article).getId();
}

public List<ArticleResponse> getAll() {
return articleRepository.findAllByOrderByCreatedAtDesc()
.stream()
.map(ArticleResponse::from)
.collect(toList());
}

@Transactional
public void update(final Long id, final ArticleRequest articleRequest,
final LoginMember loginMember) {
Expand Down Expand Up @@ -82,15 +75,15 @@ public void bookmarkArticle(final Long id, final LoginMember loginMember,
}
}

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

return articleRepository.findArticlesByCourse(course.getGroupName()).stream()
.map(ArticleResponse::from)
.map(article -> ArticleResponse.of(article,member.getId()))
.collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package wooteco.prolog.article.domain;

import lombok.Getter;

@Getter
public enum ArticleFilterType {
ALL(""),
ANDROID("안드로이드"),
BACKEND("백엔드"),
FRONTEND("프론트엔드");

private final String groupName;

ArticleFilterType(String groupName) {
this.groupName = groupName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
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.ArticleFilterType;
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;
Expand All @@ -33,12 +33,6 @@ public ResponseEntity<Void> createArticles(@RequestBody final ArticleRequest art
return ResponseEntity.created(URI.create("/articles/" + id)).build();
}

@GetMapping
public ResponseEntity<List<ArticleResponse>> getArticles() {
final List<ArticleResponse> allArticles = articleService.getAll();
return ResponseEntity.ok(allArticles);
}

@PutMapping("/{id}")
public ResponseEntity<Void> updateArticle(@RequestBody final ArticleRequest articleRequest,
@AuthMemberPrincipal final LoginMember member,
Expand All @@ -62,11 +56,11 @@ public ResponseEntity<Void> bookmarkArticle(@PathVariable final Long id,
return ResponseEntity.ok().build();
}

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

return ResponseEntity.ok(articleResponses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ public class ArticleResponse {
private final String title;
private final String url;
private final String imageUrl;
private final boolean isBookmarked;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private final LocalDateTime createdAt;

private ArticleResponse() {
this(null, null, null, null, null, null);
this(null, null, null, null, null, false, null);
}

public static ArticleResponse from(final Article article) {
public static ArticleResponse of(final Article article, final Long memberId) {
final Long id = article.getId();
final String nickName = article.getMember().getNickname();
final String title = article.getTitle().getTitle();
final String url = article.getUrl().getUrl();
final String imageUrl = article.getImageUrl().getUrl();
final boolean isBookmarked = article.getArticleBookmarks().containBookmark(memberId);
final LocalDateTime createdAt = article.getCreatedAt();
return new ArticleResponse(id, nickName, title, url, imageUrl, createdAt);
return new ArticleResponse(id, nickName, title, url, imageUrl, isBookmarked, createdAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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 wooteco.prolog.article.domain.ArticleFilterType;

import java.time.LocalDate;
import java.time.Month;
Expand All @@ -21,8 +21,8 @@ public void addFormatters(FormatterRegistry registry) {
source -> LocalDate.parse(source, DateTimeFormatter.BASIC_ISO_DATE)
);

registry.addConverter(String.class, MemberGroupType.class,
source -> MemberGroupType.valueOf(source.toUpperCase())
registry.addConverter(String.class, ArticleFilterType.class,
source -> ArticleFilterType.valueOf(source.toUpperCase())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import wooteco.prolog.article.domain.Article;
import wooteco.prolog.article.domain.ArticleBookmarks;
import wooteco.prolog.article.domain.ArticleFilterType;
import wooteco.prolog.article.domain.ImageUrl;
import wooteco.prolog.article.domain.Title;
import wooteco.prolog.article.domain.Url;
Expand All @@ -20,7 +21,6 @@
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;
Expand Down Expand Up @@ -251,7 +251,7 @@ void filter() {
when(articleRepository.findArticlesByCourse(any())).thenReturn(Arrays.asList(article));

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

//then
verify(articleRepository).findArticlesByCourse(any());
Expand All @@ -269,7 +269,7 @@ void filter_isBookmarked() {
when(articleRepository.findArticlesByCourseAndMember(any(), any())).thenReturn(Arrays.asList(article));

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

//then
verify(articleRepository).findArticlesByCourseAndMember(any(), any());
Expand Down

0 comments on commit b1a13d1

Please sign in to comment.