From da3c3c6af451da7284536bb61d8e65386e208f27 Mon Sep 17 00:00:00 2001 From: Mohammed Ezzedine Date: Sat, 30 Dec 2023 16:38:39 +0200 Subject: [PATCH] Add article filter on category id --- .../article/api/ArticleController.java | 5 ++++- .../article/api/ArticlesFetchApiCriteria.java | 1 + .../api/ArticleControllerIntegrationTest.java | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticleController.java b/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticleController.java index b7ea9f3..67b8797 100644 --- a/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticleController.java +++ b/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticleController.java @@ -67,7 +67,9 @@ private static boolean isAdmin(Principal principal) { } private static ArticlesFetchCriteria getFetchCriteria(ArticlesFetchApiCriteria fetchCriteria, Principal principal) { - ArticlesFetchCriteria criteria = ArticlesFetchCriteria.builder().highlighted(fetchCriteria.getHighlighted()).build(); + ArticlesFetchCriteria criteria = ArticlesFetchCriteria.builder().highlighted(fetchCriteria.getHighlighted()) + .categoryId(fetchCriteria.getCategoryId()).build(); + if (fetchCriteria.getPage().isPresent()) { PaginationCriteria paginationCriteria = PaginationCriteria.builder().startingPageIndex(fetchCriteria.getPage().get()).maximumPageSize(fetchCriteria.getSize().orElse(10)).build(); criteria.setPaginationCriteria(paginationCriteria); @@ -78,6 +80,7 @@ private static ArticlesFetchCriteria getFetchCriteria(ArticlesFetchApiCriteria f criteria.setSortingCriteria(sortingCriteria); } + if (!isAdmin(principal)) { // TODO add test criteria.setHidden(false); diff --git a/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticlesFetchApiCriteria.java b/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticlesFetchApiCriteria.java index ceb41c9..339f535 100644 --- a/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticlesFetchApiCriteria.java +++ b/src/main/java/me/ezzedine/mohammed/personalspace/article/api/ArticlesFetchApiCriteria.java @@ -15,6 +15,7 @@ public class ArticlesFetchApiCriteria { private Boolean highlighted; private String sortBy; private Boolean ascOrder; + private String categoryId; public Optional getPage() { return Optional.ofNullable(page); diff --git a/src/test/java/me/ezzedine/mohammed/personalspace/article/api/ArticleControllerIntegrationTest.java b/src/test/java/me/ezzedine/mohammed/personalspace/article/api/ArticleControllerIntegrationTest.java index 9acb891..b3502a0 100644 --- a/src/test/java/me/ezzedine/mohammed/personalspace/article/api/ArticleControllerIntegrationTest.java +++ b/src/test/java/me/ezzedine/mohammed/personalspace/article/api/ArticleControllerIntegrationTest.java @@ -148,6 +148,24 @@ void should_default_to_the_ascending_order_sorting_when_not_mentioned() throws E assertEquals(loadResource("article/api/all_articles_details_response.json"), response); } } + + @Nested + @DisplayName("When filtering on the category id") + class FetchingArticlesByCategoryIdIntegrationTest { + @Test + @DisplayName("should return only the articles of the category") + void should_return_only_the_articles_of_the_category() throws Exception { + Page
page = Page.
builder().totalSize(14).items(List.of(getArticle())).build(); + when(articleFetcher.fetchAll(ArticlesFetchCriteria.builder().hidden(false).categoryId("categoryId").build())).thenReturn(page); + + String response = mockMvc.perform(get("/articles") + .param("categoryId", "categoryId")) + .andExpect(status().is2xxSuccessful()) + .andReturn().getResponse().getContentAsString(); + + assertEquals(loadResource("article/api/all_articles_details_response.json"), response); + } + } } @Nested