Skip to content

Commit

Permalink
Add estimated reading time property to the article object
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-ezzedine committed Dec 27, 2023
1 parent 48233c0 commit 3c3949d
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
Expand All @@ -22,16 +21,8 @@ public void run(String... args) {
List<ArticleEntity> articles = articleMongoRepository.findAll();
for (ArticleEntity article : articles) {

if (article.getCreatedDate() == null) {
article.setCreatedDate(LocalDateTime.now());
}

if (article.getLastModifiedDate() == null) {
article.setLastModifiedDate(LocalDateTime.now());
}

if (article.getVersion() == null) {
articleMongoRepository.deleteById(article.getId());
if (article.getEstimatedReadingTime() == null) {
article.setEstimatedReadingTime("");
}

articleMongoRepository.save(article);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public class ArticleApiMapper {
public 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(article.getCreatedDate().toString())
.keywords(article.getKeywords()).createdDate(article.getCreatedDate().toString()).estimatedReadingTime(article.getEstimatedReadingTime())
.lastModifiedDate(article.getLastModifiedDate().toString()).hidden(article.isHidden()).build();
}

public static ArticleSummaryApiModel toSummaryApiModel(Article article) {
return ArticleSummaryApiModel.builder().id(article.getId()).title(article.getTitle()).description(article.getDescription())
.categoryId(article.getCategoryId()).thumbnailImageUrl(article.getThumbnailImageUrl())
.keywords(article.getKeywords()).createdDate(article.getCreatedDate().toString())
.keywords(article.getKeywords()).createdDate(article.getCreatedDate().toString()).estimatedReadingTime(article.getEstimatedReadingTime())
.lastModifiedDate(article.getLastModifiedDate().toString()).hidden(article.isHidden()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class ArticleApiModel extends ArticleSummaryApiModel {

private String content;


}
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ private static ArticlesFetchCriteria getFetchCriteria(ArticlesFetchApiCriteria f
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()).hidden(request.getHidden()).build();
.keywords(request.getKeywords()).hidden(request.getHidden()).estimatedReadingTime(request.getEstimatedReadingTime()).build();
}

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

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()).hidden(request.getHidden()).build();
.keywords(request.getKeywords()).hidden(request.getHidden()).estimatedReadingTime(request.getEstimatedReadingTime()).build();
storage.save(article);
return articleId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ArticleUpdateRequest {
private String thumbnailImageUrl;
private List<String> keywords;
private Boolean hidden;
private String estimatedReadingTime;

public Optional<String> getTitle() {
return Optional.ofNullable(title);
Expand Down Expand Up @@ -45,4 +46,8 @@ public Optional<List<String>> getKeywords() {
public Optional<Boolean> getHidden() {
return Optional.ofNullable(hidden);
}

public Optional<String> getEstimatedReadingTime() {
return Optional.ofNullable(estimatedReadingTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ArticleEntity {
private String thumbnailImageUrl;
private List<String> keywords;
private boolean hidden;
private String estimatedReadingTime;
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,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()).hidden(article.isHidden()).build();
.lastModifiedDate(article.getLastModifiedDate()).hidden(article.isHidden()).estimatedReadingTime(article.getEstimatedReadingTime()).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()).hidden(articleEntity.isHidden()).build();
.version(articleEntity.getVersion()).hidden(articleEntity.isHidden()).estimatedReadingTime(articleEntity.getEstimatedReadingTime()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public class ArticleApiTestUtil {
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;
public static final String ESTIMATED_READING_TIME = "estimatedReadingTime";

public static 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))
.hidden(HIDDEN).build();
.hidden(HIDDEN).estimatedReadingTime(ESTIMATED_READING_TIME).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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)).hidden(HIDDEN).build();
.keywords(List.of(KEYWORD)).hidden(HIDDEN).estimatedReadingTime(ESTIMATED_READING_TIME).build();
verify(articleCreator).create(request);
}

Expand Down Expand Up @@ -234,7 +234,7 @@ 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"))
.hidden(true).build();
.hidden(true).estimatedReadingTime("updatedEstimatedReadingTime").build();
verify(articleEditor).edit(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ArticleServiceTest {
public static final LocalDateTime LAST_MODIFIED_DATE = mock(LocalDateTime.class);
public static final boolean HIDDEN = true;
public static final boolean UPDATED_HIDDEN = false;
public static final String ESTIMATED_READING_TIME = UUID.randomUUID().toString();
public static final String UPDATED_ESTIMATED_READING_TIME = UUID.randomUUID().toString();
private ArticleStorage storage;
private ArticleService service;
private CategoryFetcher categoryFetcher;
Expand Down Expand Up @@ -91,6 +93,7 @@ void should_save_the_new_article_in_the_storage() throws CategoryNotFoundExcepti
assertEquals(THUMBNAIL_IMAGE_URL, argumentCaptor.getValue().getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), argumentCaptor.getValue().getKeywords());
assertEquals(HIDDEN, argumentCaptor.getValue().isHidden());
assertEquals(ESTIMATED_READING_TIME, argumentCaptor.getValue().getEstimatedReadingTime());
}

@Test
Expand All @@ -102,7 +105,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)).hidden(HIDDEN).build();
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).estimatedReadingTime(ESTIMATED_READING_TIME).build();
}
}

Expand Down Expand Up @@ -137,7 +140,7 @@ void it_should_return_the_article_details() throws ArticleNotFoundException {
assertEquals(CREATED_DATE, article.getCreatedDate());
assertEquals(LAST_MODIFIED_DATE, article.getLastModifiedDate());
assertEquals(HIDDEN, article.isHidden());

assertEquals(ESTIMATED_READING_TIME, article.getEstimatedReadingTime());
}
}

Expand Down Expand Up @@ -182,20 +185,21 @@ void the_new_changes_should_be_saved_successfully_on_the_happy_path() throws Art
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)
.hidden(UPDATED_HIDDEN).build();
.hidden(UPDATED_HIDDEN).estimatedReadingTime(UPDATED_ESTIMATED_READING_TIME).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)).hidden(UPDATED_HIDDEN).build();
.keywords(List.of(UPDATED_KEYWORD)).hidden(UPDATED_HIDDEN).estimatedReadingTime(UPDATED_ESTIMATED_READING_TIME).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).hidden(true).build();
.version(VERSION).createdDate(CREATED_DATE).lastModifiedDate(LAST_MODIFIED_DATE).hidden(true)
.estimatedReadingTime(ESTIMATED_READING_TIME).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ArticleStorageManagerIntegrationTest extends DatabaseIntegrationTest {
public static final String THUMBNAIL_IMAGE_URL = UUID.randomUUID().toString();
public static final String KEYWORD = UUID.randomUUID().toString();
public static final boolean HIDDEN = true;
public static final String ESTIMATED_READING_TIME = UUID.randomUUID().toString();
@Autowired
private ArticleMongoRepository repository;

Expand Down Expand Up @@ -74,6 +75,7 @@ void the_article_should_be_persisted_in_the_storage() {
assertEquals(THUMBNAIL_IMAGE_URL, allArticles.get(0).getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), allArticles.get(0).getKeywords());
assertEquals(HIDDEN, allArticles.get(0).isHidden());
assertEquals(ESTIMATED_READING_TIME, allArticles.get(0).getEstimatedReadingTime());
}

@Test
Expand Down Expand Up @@ -108,6 +110,7 @@ void should_override_any_existing_article_with_the_same_id() {
assertEquals(THUMBNAIL_IMAGE_URL, allArticles.get(0).getThumbnailImageUrl());
assertEquals(List.of(KEYWORD), allArticles.get(0).getKeywords());
assertEquals(HIDDEN, allArticles.get(0).isHidden());
assertEquals(ESTIMATED_READING_TIME, allArticles.get(0).getEstimatedReadingTime());
}

@Test
Expand Down Expand Up @@ -145,12 +148,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())).hidden(false).build();
.keywords(List.of(UUID.randomUUID().toString())).hidden(false).estimatedReadingTime(UUID.randomUUID().toString()).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)).hidden(HIDDEN).build();
.thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).estimatedReadingTime(ESTIMATED_READING_TIME).build();
}
}

Expand Down Expand Up @@ -181,6 +184,7 @@ void it_should_return_the_article_when_it_exists() {
assertNotNull(optionalArticle.get().getCreatedDate());
assertNotNull(optionalArticle.get().getLastModifiedDate());
assertEquals(HIDDEN, optionalArticle.get().isHidden());
assertEquals(ESTIMATED_READING_TIME, optionalArticle.get().getEstimatedReadingTime());
}
}

Expand Down Expand Up @@ -218,6 +222,7 @@ void should_return_all_the_articles_when_they_are_less_than_the_size_of_the_firs
assertNotNull(articles.getItems().get(0).getCreatedDate());
assertNotNull(articles.getItems().get(0).getLastModifiedDate());
assertEquals(HIDDEN, articles.getItems().get(0).isHidden());
assertEquals(ESTIMATED_READING_TIME, articles.getItems().get(0).getEstimatedReadingTime());
}

@Test
Expand Down Expand Up @@ -434,7 +439,7 @@ void should_return_the_list_of_matching_articles() {
}

private ArticleEntity getEntity(String id, String categoryId) {
return ArticleEntity.builder().id(id).categoryId(categoryId).title(TITLE).description(DESCRIPTION)
return ArticleEntity.builder().id(id).categoryId(categoryId).title(TITLE).description(DESCRIPTION).estimatedReadingTime(ESTIMATED_READING_TIME)
.content(CONTENT).thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN).build();
}
}
Expand Down Expand Up @@ -495,7 +500,8 @@ private static HighlightedArticleEntity getHighlightedArticle(String firstArticl

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)).hidden(HIDDEN).build();
.content(CONTENT).thumbnailImageUrl(THUMBNAIL_IMAGE_URL).keywords(List.of(KEYWORD)).hidden(HIDDEN)
.estimatedReadingTime(ESTIMATED_READING_TIME).build();
}

private ArticleEntity getEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"createdDate": "2023-12-24T12:33:42.411",
"lastModifiedDate": "2023-12-24T12:34:51.182",
"hidden": true,
"keywords": ["keyword"]
"keywords": ["keyword"],
"estimatedReadingTime": "estimatedReadingTime"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"lastModifiedDate": "2023-12-24T12:34:51.182",
"hidden": true,
"keywords": ["keyword"],
"estimatedReadingTime": "estimatedReadingTime",
"content": "articleContent"
}
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 @@ -5,5 +5,6 @@
"categoryId": "articleCategoryId",
"thumbnailImageUrl": "articleThumbnailImageUrl",
"keywords": ["keyword"],
"hidden": true
"hidden": true,
"estimatedReadingTime": "estimatedReadingTime"
}
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 @@ -5,5 +5,6 @@
"categoryId": "updatedArticleCategoryId",
"thumbnailImageUrl": "updatedArticleThumbnailImageUrl",
"keywords": ["updatedArticleKeyword"],
"hidden": true
"hidden": true,
"estimatedReadingTime": "updatedEstimatedReadingTime"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"hidden": true,
"keywords": [
"keyword"
]
],
"estimatedReadingTime": "estimatedReadingTime"
}
]

0 comments on commit 3c3949d

Please sign in to comment.