Skip to content

Commit

Permalink
Add article filter on category id
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-ezzedine committed Dec 30, 2023
1 parent 85375f2 commit da3c3c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -78,6 +80,7 @@ private static ArticlesFetchCriteria getFetchCriteria(ArticlesFetchApiCriteria f
criteria.setSortingCriteria(sortingCriteria);
}


if (!isAdmin(principal)) {
// TODO add test
criteria.setHidden(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ArticlesFetchApiCriteria {
private Boolean highlighted;
private String sortBy;
private Boolean ascOrder;
private String categoryId;

public Optional<Integer> getPage() {
return Optional.ofNullable(page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Article> page = Page.<Article>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
Expand Down

0 comments on commit da3c3c6

Please sign in to comment.