diff --git a/src/main/java/com/goldbalance/dive/domain/article/controller/ArticleController.java b/src/main/java/com/goldbalance/dive/domain/article/controller/ArticleController.java index e85b21a..96f2502 100644 --- a/src/main/java/com/goldbalance/dive/domain/article/controller/ArticleController.java +++ b/src/main/java/com/goldbalance/dive/domain/article/controller/ArticleController.java @@ -1,3 +1,22 @@ package com.goldbalance.dive.domain.article.controller; -public class ArticleController {} +import com.goldbalance.dive.domain.article.domain.Article; +import com.goldbalance.dive.domain.article.domain.ArticleQueryOption; +import com.goldbalance.dive.domain.article.service.ArticleService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/articles") +@RequiredArgsConstructor +public class ArticleController { + private final ArticleService articleService; + + @GetMapping + public ResponseEntity> findArticles(@RequestParam ArticleQueryOption queryOption) { + List
response = articleService.findArticles(queryOption); + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/goldbalance/dive/domain/article/domain/ArticleQueryOption.java b/src/main/java/com/goldbalance/dive/domain/article/domain/ArticleQueryOption.java new file mode 100644 index 0000000..0dfe0e9 --- /dev/null +++ b/src/main/java/com/goldbalance/dive/domain/article/domain/ArticleQueryOption.java @@ -0,0 +1,3 @@ +package com.goldbalance.dive.domain.article.domain; + +public record ArticleQueryOption(String keyword, Category category, Duration duration) {} diff --git a/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepository.java b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepository.java new file mode 100644 index 0000000..570e190 --- /dev/null +++ b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepository.java @@ -0,0 +1,11 @@ +package com.goldbalance.dive.domain.article.repository; + +import com.goldbalance.dive.domain.article.domain.Article; +import com.goldbalance.dive.domain.article.domain.ArticleQueryOption; +import com.querydsl.core.types.dsl.BooleanExpression; +import java.util.List; + +public interface ArticleCustomRepository { + + List
searchArticle(ArticleQueryOption queryOption); +} diff --git a/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepositoryImpl.java b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepositoryImpl.java new file mode 100644 index 0000000..e19f44e --- /dev/null +++ b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleCustomRepositoryImpl.java @@ -0,0 +1,28 @@ +package com.goldbalance.dive.domain.article.repository; + +import static com.goldbalance.dive.domain.article.domain.QArticle.*; + +import com.goldbalance.dive.domain.article.domain.Article; +import com.goldbalance.dive.domain.article.domain.ArticleQueryOption; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; +import java.util.List; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class ArticleCustomRepositoryImpl implements ArticleCustomRepository { + + private final JPAQueryFactory queryFactory; + + @Override + public List
searchArticle(ArticleQueryOption queryOption) { + return queryFactory + .selectFrom(article) + .where(containsKeyword(queryOption)) + .fetch(); + } + + private BooleanExpression containsKeyword(ArticleQueryOption queryOption) { + return queryOption.keyword() != null ? article.title.containsIgnoreCase(queryOption.keyword()) : null; + } +} diff --git a/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleRepository.java b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleRepository.java index 453f8ff..1ae7af7 100644 --- a/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleRepository.java +++ b/src/main/java/com/goldbalance/dive/domain/article/repository/ArticleRepository.java @@ -3,4 +3,4 @@ import com.goldbalance.dive.domain.article.domain.Article; import org.springframework.data.jpa.repository.JpaRepository; -public interface ArticleRepository extends JpaRepository {} +public interface ArticleRepository extends JpaRepository, ArticleCustomRepository {} diff --git a/src/main/java/com/goldbalance/dive/domain/article/service/ArticleService.java b/src/main/java/com/goldbalance/dive/domain/article/service/ArticleService.java index d49dfe7..372ac71 100644 --- a/src/main/java/com/goldbalance/dive/domain/article/service/ArticleService.java +++ b/src/main/java/com/goldbalance/dive/domain/article/service/ArticleService.java @@ -1,3 +1,21 @@ package com.goldbalance.dive.domain.article.service; -public class ArticleService {} +import com.goldbalance.dive.domain.article.domain.Article; +import com.goldbalance.dive.domain.article.domain.ArticleQueryOption; +import com.goldbalance.dive.domain.article.repository.ArticleRepository; +import jakarta.transaction.Transactional; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class ArticleService { + + private final ArticleRepository articleRepository; + + public List
findArticles(ArticleQueryOption queryOption) { + return articleRepository.searchArticle(queryOption); + } +}