Skip to content

Commit

Permalink
Polish (#1598)
Browse files Browse the repository at this point in the history
* polish

* feat : adds new testcases

* fix input type

* fix wildcard import

* polish db operations

* fix : spotless

* concise code, consider leveraging streams directly
  • Loading branch information
rajadilipkolli authored Dec 25, 2024
1 parent c4b3134 commit d249308
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ ResponseEntity<Object> saveArticle(@RequestBody ArticleDTO articleDTO) {
}

@DeleteMapping("/{id}")
ResponseEntity<Object> deleteArticle(@PathVariable Long id) {
return this.articleService
.findById(id)
.map(
article -> {
articleService.deleteById(article.getId());
return ResponseEntity.accepted().build();
})
.orElseGet(() -> ResponseEntity.notFound().build());
ResponseEntity<Void> deleteArticle(@PathVariable Long id) {
boolean exists = this.articleService.existsById(id);
if (exists) {
this.articleService.deleteById(id);
return ResponseEntity.accepted().build();
} else {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.example.demo.readreplica.domain;

import static com.example.demo.readreplica.domain.CommentDTO.convertToComment;

import com.example.demo.readreplica.entities.Article;
import com.example.demo.readreplica.entities.Comment;
import java.time.LocalDateTime;
import java.util.List;

Expand All @@ -16,11 +13,7 @@ public record ArticleDTO(
public Article convertToArticle() {
Article article =
new Article().setAuthored(authored).setTitle(title).setPublished(published);
commentDTOs.forEach(
commentDTO -> {
Comment comment = convertToComment(commentDTO);
article.addComment(comment);
});
commentDTOs.stream().map(CommentDTO::convertToComment).forEach(article::addComment);
return article;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface ArticleRepository extends JpaRepository<Article, Long> {

@Transactional(readOnly = true)
@Query("select a from Article a left join fetch a.comments where a.id = :articleId ")
Optional<Article> findByArticleId(@Param("articleId") Long articleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public Optional<ArticleDTO> findArticleById(Long id) {
return this.articleRepository.findByArticleId(id).map(this::convertToArticleDTO);
}

public boolean existsById(Long id) {
return articleRepository.existsById(id);
}

@Transactional
public Long saveArticle(ArticleDTO articleDTO) {
Article article = articleDTO.convertToArticle();
Article savedArticle = this.articleRepository.save(article);
return savedArticle.getId();
}

public Optional<Article> findById(Long id) {
return articleRepository.findById(id);
}

@Transactional
public void deleteById(Long id) {
articleRepository.deleteById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ spring:
fail_on_pagination_over_collection_fetch: true
in_clause_parameter_padding: true
plan_cache_max_size: 4096
mvc:
problemdetails:
enabled: true
threads:
virtual:
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.demo.readreplica.domain.CommentDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -55,7 +56,12 @@ void findArticleById() {
}

@Test
void saveArticleRetriveAndDelete() throws JsonProcessingException {
void shouldReturn404WhenFetchingNonExistingArticle() {
mvcTester.get().uri("/articles/99999").assertThat().hasStatus(HttpStatus.NOT_FOUND);
}

@Test
void saveRetrieveAndDeleteArticle() throws JsonProcessingException {
ArticleDTO articleDTO =
new ArticleDTO(
"junitTitle",
Expand Down Expand Up @@ -90,9 +96,13 @@ void saveArticleRetriveAndDelete() throws JsonProcessingException {
assertThat(response.authored())
.isNotNull()
.isInstanceOf(LocalDateTime.class);
assertThat(response.authored().toLocalDate())
.isEqualTo(LocalDate.now().minusDays(1));
assertThat(response.published())
.isNotNull()
.isInstanceOf(LocalDateTime.class);
assertThat(response.published().toLocalDate())
.isEqualTo(LocalDate.now());
assertThat(response.commentDTOs())
.isNotNull()
.hasSize(1)
Expand All @@ -101,4 +111,9 @@ void saveArticleRetriveAndDelete() throws JsonProcessingException {

mvcTester.delete().uri(location.get()).assertThat().hasStatus(HttpStatus.ACCEPTED);
}

@Test
void cantDeleteArticleWhenArticleNotFound() {
mvcTester.delete().uri("/articles/99999").assertThat().hasStatus(HttpStatus.NOT_FOUND);
}
}

0 comments on commit d249308

Please sign in to comment.