Skip to content

Commit

Permalink
Add the ability to hide articles
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-ezzedine committed Dec 24, 2023
1 parent 10698ba commit fefc605
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class ArticleApiModel {
private List<String> keywords;
private String createdDate;
private String lastModifiedDate;
private Boolean hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public ResponseEntity<Void> editArticle(String id, ArticleUpdateApiRequest reque
private static ArticleUpdateRequest toDomainModel(String id, ArticleUpdateApiRequest request) {
return ArticleUpdateRequest.builder().id(id).title(request.getTitle()).description(request.getDescription())
.content(request.getContent()).categoryId(request.getCategoryId()).thumbnailImageUrl(request.getThumbnailImageUrl())
.keywords(request.getKeywords()).build();
.keywords(request.getKeywords()).hidden(request.getHidden()).build();
}

private static ArticleApiModel toApiModel(Article article) {
return ArticleApiModel.builder().id(article.getId()).title(article.getTitle()).description(article.getDescription())
.content(article.getContent()).categoryId(article.getCategoryId()).thumbnailImageUrl(article.getThumbnailImageUrl())
.keywords(article.getKeywords()).createdDate(getDateAsString(article.getCreatedDate())).lastModifiedDate(getDateAsString(article.getLastModifiedDate())).build();
.keywords(article.getKeywords()).createdDate(getDateAsString(article.getCreatedDate()))
.lastModifiedDate(getDateAsString(article.getLastModifiedDate())).hidden(article.isHidden()).build();
}

private static String getDateAsString(LocalDateTime createdDate) {
Expand All @@ -79,7 +80,7 @@ private static String getDateAsString(LocalDateTime createdDate) {

private static ArticleCreationRequest toDomainModel(ArticleCreationApiRequest request) {
return ArticleCreationRequest.builder().title(request.getTitle()).content(request.getContent())
.description(request.getDescription()).categoryId(request.getCategoryId())
.description(request.getDescription()).categoryId(request.getCategoryId()).hidden(request.getHidden())
.thumbnailImageUrl(request.getThumbnailImageUrl()).keywords(request.getKeywords()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class ArticleCreationApiRequest {
private String thumbnailImageUrl;
@NonNull
private List<String> keywords;
@NonNull
private Boolean hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class ArticleUpdateApiRequest {
private String content;
private String thumbnailImageUrl;
private List<String> keywords;
private Boolean hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Article {
private String thumbnailImageUrl;
@Builder.Default
private List<String> keywords = new ArrayList<>();
private boolean hidden;
private LocalDateTime createdDate;
private LocalDateTime lastModifiedDate;
private Long version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class ArticleCreationRequest {
private String thumbnailImageUrl;
@NonNull
private List<String> keywords;
@NonNull
private Boolean hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void edit(ArticleUpdateRequest request) throws ArticleNotFoundException,
request.getContent().ifPresent(article::setContent);
request.getThumbnailImageUrl().ifPresent(article::setThumbnailImageUrl);
request.getKeywords().ifPresent(article::setKeywords);
request.getHidden().ifPresent(article::setHidden);

storage.save(article);
}
Expand All @@ -62,7 +63,7 @@ private String saveArticleInStorage(ArticleCreationRequest request) {
String articleId = idGenerator.generate();
Article article = Article.builder().id(articleId).content(request.getContent()).categoryId(request.getCategoryId())
.description(request.getDescription()).title(request.getTitle()).thumbnailImageUrl(request.getThumbnailImageUrl())
.keywords(request.getKeywords()).build();
.keywords(request.getKeywords()).hidden(request.getHidden()).build();
storage.save(article);
return articleId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ArticleUpdateRequest {
private String content;
private String thumbnailImageUrl;
private List<String> keywords;
private Boolean hidden;

public Optional<String> getTitle() {
return Optional.ofNullable(title);
Expand All @@ -40,4 +41,8 @@ public Optional<String> getThumbnailImageUrl() {
public Optional<List<String>> getKeywords() {
return Optional.ofNullable(keywords);
}

public Optional<Boolean> getHidden() {
return Optional.ofNullable(hidden);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ArticleEntity {
private String categoryId;
private String thumbnailImageUrl;
private List<String> keywords;
private boolean hidden;
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ private static ArticleEntity toEntity(Article article) {
return ArticleEntity.builder().id(article.getId()).title(article.getTitle()).description(article.getDescription())
.content(article.getContent()).categoryId(article.getCategoryId()).thumbnailImageUrl(article.getThumbnailImageUrl())
.keywords(article.getKeywords()).version(article.getVersion()).createdDate(article.getCreatedDate())
.lastModifiedDate(article.getLastModifiedDate()).build();
.lastModifiedDate(article.getLastModifiedDate()).hidden(article.isHidden()).build();
}

private static Article fromEntity(ArticleEntity articleEntity) {
return Article.builder().id(articleEntity.getId()).content(articleEntity.getContent()).title(articleEntity.getTitle())
.description(articleEntity.getDescription()).categoryId(articleEntity.getCategoryId())
.thumbnailImageUrl(articleEntity.getThumbnailImageUrl()).keywords(articleEntity.getKeywords())
.createdDate(articleEntity.getCreatedDate()).lastModifiedDate(articleEntity.getLastModifiedDate())
.version(articleEntity.getVersion()).build();
.version(articleEntity.getVersion()).hidden(articleEntity.isHidden()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ArticleControllerIntegrationTest {
public static final String KEYWORD = "keyword";
public static final String CREATED_DATE = "2023-12-24T12:33:42.411";
public static final String LAST_MODIFIED_DATE = "2023-12-24T12:34:51.182";
public static final boolean HIDDEN = true;
@Autowired
private MockMvc mockMvc;

Expand Down Expand Up @@ -136,7 +137,7 @@ void should_return_a_created_success_status_on_the_happy_path() throws Exception

ArticleCreationRequest request = ArticleCreationRequest.builder().title(TITLE).description(DESCRIPTION)
.content(CONTENT).categoryId(CATEGORY_ID).thumbnailImageUrl(ARTICLE_THUMBNAIL_IMAGE_URL)
.keywords(List.of(KEYWORD)).build();
.keywords(List.of(KEYWORD)).hidden(HIDDEN).build();
verify(articleCreator).create(request);
}

Expand Down Expand Up @@ -178,7 +179,8 @@ void should_return_a_success_status_on_the_happy_path() throws Exception {

ArticleUpdateRequest request = ArticleUpdateRequest.builder().id(ARTICLE_ID).title("updatedArticleTitle")
.categoryId("updatedArticleCategoryId").content("updatedArticleContent").description("updatedArticleDescription")
.thumbnailImageUrl("updatedArticleThumbnailImageUrl").keywords(List.of("updatedArticleKeyword")).build();
.thumbnailImageUrl("updatedArticleThumbnailImageUrl").keywords(List.of("updatedArticleKeyword"))
.hidden(true).build();
verify(articleEditor).edit(request);
}

Expand Down Expand Up @@ -210,6 +212,7 @@ void should_return_a_not_found_status_when_the_chosen_category_does_not_exist()
private Article getArticle() {
return Article.builder().id(ARTICLE_ID).title(TITLE).description(DESCRIPTION).content(CONTENT)
.categoryId(CATEGORY_ID).thumbnailImageUrl(ARTICLE_THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD))
.version(1L).createdDate(LocalDateTime.parse(CREATED_DATE)).lastModifiedDate(LocalDateTime.parse(LAST_MODIFIED_DATE)).build();
.version(1L).createdDate(LocalDateTime.parse(CREATED_DATE)).lastModifiedDate(LocalDateTime.parse(LAST_MODIFIED_DATE))
.hidden(HIDDEN).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ArticleServiceTest {
public static final Long VERSION = 1L;
public static final LocalDateTime CREATED_DATE = mock(LocalDateTime.class);
public static final LocalDateTime LAST_MODIFIED_DATE = mock(LocalDateTime.class);
public static final boolean HIDDEN = true;
public static final boolean UPDATED_HIDDEN = false;
private ArticleStorage storage;
private ArticleService service;
private CategoryFetcher categoryFetcher;
Expand Down Expand Up @@ -89,6 +91,7 @@ void should_save_the_new_article_in_the_storage() throws CategoryNotFoundExcepti
assertEquals(DESCRIPTION, argumentCaptor.getValue().getDescription());
assertEquals(THUMBNAIL_IMAGE_URL, argumentCaptor.getValue().getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), argumentCaptor.getValue().getKeywords());
assertEquals(HIDDEN, argumentCaptor.getValue().isHidden());
}

@Test
Expand All @@ -100,7 +103,7 @@ void should_return_the_id_of_the_newly_created_article() throws CategoryNotFound

private static ArticleCreationRequest getRequest() {
return ArticleCreationRequest.builder().categoryId(CATEGORY_ID).title(TITLE).content(CONTENT).description(DESCRIPTION)
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).build();
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).build();
}
}

Expand Down Expand Up @@ -134,6 +137,8 @@ void it_should_return_the_article_details() throws ArticleNotFoundException {
assertEquals(VERSION, article.getVersion());
assertEquals(CREATED_DATE, article.getCreatedDate());
assertEquals(LAST_MODIFIED_DATE, article.getLastModifiedDate());
assertEquals(HIDDEN, article.isHidden());

}
}

Expand Down Expand Up @@ -177,20 +182,21 @@ void the_new_changes_should_be_saved_successfully_on_the_happy_path() throws Art
service.edit(getRequest());
Article updatedArticle = Article.builder().id(ARTICLE_ID).categoryId(UPDATED_CATEGORY_ID).title(UPDATED_TITLE)
.content(UPDATED_CONTENT).description(UPDATED_DESCRIPTION).thumbnailImageUrl(UPDATED_THUMBNAIL_IMAGE_URL)
.keywords(List.of(UPDATED_KEYWORD)).version(VERSION).createdDate(CREATED_DATE).lastModifiedDate(LAST_MODIFIED_DATE).build();
.keywords(List.of(UPDATED_KEYWORD)).version(VERSION).createdDate(CREATED_DATE).lastModifiedDate(LAST_MODIFIED_DATE)
.hidden(UPDATED_HIDDEN).build();
verify(storage).save(updatedArticle);
}

private static ArticleUpdateRequest getRequest() {
return ArticleUpdateRequest.builder().id(ARTICLE_ID).categoryId(UPDATED_CATEGORY_ID).title(UPDATED_TITLE)
.content(UPDATED_CONTENT).description(UPDATED_DESCRIPTION).thumbnailImageUrl(UPDATED_THUMBNAIL_IMAGE_URL)
.keywords(List.of(UPDATED_KEYWORD)).build();
.keywords(List.of(UPDATED_KEYWORD)).hidden(UPDATED_HIDDEN).build();
}
}

private Article getArticle() {
return Article.builder().id(ARTICLE_ID).title(TITLE).description(DESCRIPTION).content(CONTENT)
.categoryId(CATEGORY_ID).thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD))
.version(VERSION).createdDate(CREATED_DATE).lastModifiedDate(LAST_MODIFIED_DATE).build();
.version(VERSION).createdDate(CREATED_DATE).lastModifiedDate(LAST_MODIFIED_DATE).hidden(true).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ArticleStorageManagerIntegrationTest extends DatabaseIntegrationTest {
public static final String CONTENT = UUID.randomUUID().toString();
public static final String THUMBNAIL_IMAGE_URL = UUID.randomUUID().toString();
public static final String KEYWORD = UUID.randomUUID().toString();
public static final boolean HIDDEN = true;
@Autowired
private ArticleMongoRepository repository;

Expand Down Expand Up @@ -62,6 +63,7 @@ void the_article_should_be_persisted_in_the_storage() {
assertEquals(CONTENT, allArticles.get(0).getContent());
assertEquals(THUMBNAIL_IMAGE_URL, allArticles.get(0).getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), allArticles.get(0).getKeywords());
assertEquals(HIDDEN, allArticles.get(0).isHidden());
}

@Test
Expand Down Expand Up @@ -95,6 +97,7 @@ void should_override_any_existing_article_with_the_same_id() {
assertEquals(CONTENT, allArticles.get(0).getContent());
assertEquals(THUMBNAIL_IMAGE_URL, allArticles.get(0).getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), allArticles.get(0).getKeywords());
assertEquals(HIDDEN, allArticles.get(0).isHidden());
}

@Test
Expand Down Expand Up @@ -132,12 +135,12 @@ void should_not_update_the_created_date_when_overriding_an_existing_article() {
private static ArticleEntity getRandomArticleEntity() {
return ArticleEntity.builder().id(ID).title(UUID.randomUUID().toString()).categoryId(UUID.randomUUID().toString())
.content(UUID.randomUUID().toString()).description(UUID.randomUUID().toString()).thumbnailImageUrl(UUID.randomUUID().toString())
.keywords(List.of(UUID.randomUUID().toString())).build();
.keywords(List.of(UUID.randomUUID().toString())).hidden(false).build();
}

private Article getArticle() {
return Article.builder().id(ID).categoryId(CATEGORY_ID).title(TITLE).description(DESCRIPTION).content(CONTENT)
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).build();
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).build();
}
}

Expand Down Expand Up @@ -167,6 +170,7 @@ void it_should_return_the_article_when_it_exists() {
assertEquals(List.of(KEYWORD), optionalArticle.get().getKeywords());
assertNotNull(optionalArticle.get().getCreatedDate());
assertNotNull(optionalArticle.get().getLastModifiedDate());
assertEquals(HIDDEN, optionalArticle.get().isHidden());
}
}

Expand Down Expand Up @@ -198,6 +202,8 @@ void should_return_a_list_of_one_article_when_only_one_exists() {
assertEquals(List.of(KEYWORD), articles.getItems().get(0).getKeywords());
assertNotNull(articles.getItems().get(0).getCreatedDate());
assertNotNull(articles.getItems().get(0).getLastModifiedDate());
assertEquals(HIDDEN, articles.getItems().get(0).isHidden());

}

@Test
Expand Down Expand Up @@ -259,7 +265,7 @@ void should_return_the_articles_sorted_in_descending_order_of_creation_date() {

private ArticleEntity getEntity(String id) {
return ArticleEntity.builder().id(id).categoryId(CATEGORY_ID).title(TITLE).description(DESCRIPTION)
.content(CONTENT).thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).build();
.content(CONTENT).thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).build();
}

private ArticleEntity getEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"thumbnailImageUrl": "articleThumbnailImageUrl",
"keywords": ["keyword"],
"createdDate": "2023-12-24T12:33:42.411",
"lastModifiedDate": "2023-12-24T12:34:51.182"
"lastModifiedDate": "2023-12-24T12:34:51.182",
"hidden": true
}
]
}
3 changes: 2 additions & 1 deletion src/test/resources/article/api/article_details_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"thumbnailImageUrl": "articleThumbnailImageUrl",
"keywords": ["keyword"],
"createdDate": "2023-12-24T12:33:42.411",
"lastModifiedDate": "2023-12-24T12:34:51.182"
"lastModifiedDate": "2023-12-24T12:34:51.182",
"hidden": true
}
3 changes: 2 additions & 1 deletion src/test/resources/article/api/create_article_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"content": "articleContent",
"categoryId": "articleCategoryId",
"thumbnailImageUrl": "articleThumbnailImageUrl",
"keywords": ["keyword"]
"keywords": ["keyword"],
"hidden": true
}
3 changes: 2 additions & 1 deletion src/test/resources/article/api/edit_article_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"content": "updatedArticleContent",
"categoryId": "updatedArticleCategoryId",
"thumbnailImageUrl": "updatedArticleThumbnailImageUrl",
"keywords": ["updatedArticleKeyword"]
"keywords": ["updatedArticleKeyword"],
"hidden": true
}

0 comments on commit fefc605

Please sign in to comment.