Skip to content

Commit

Permalink
feature: 글 검색 api 구현 (#37)
Browse files Browse the repository at this point in the history
* feature: 글 검색 api 구현

* feature: 글 검색 api 피드백 반영

* feature: 글 검색 api 피드백 2번째 반영
  • Loading branch information
sleepyhoon authored Jul 27, 2024
1 parent 3932a3c commit e9bba3c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<List<Article>> findArticles(@RequestParam ArticleQueryOption queryOption) {
List<Article> response = articleService.findArticles(queryOption);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.goldbalance.dive.domain.article.domain;

public record ArticleQueryOption(String keyword, Category category, Duration duration) {}
Original file line number Diff line number Diff line change
@@ -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<Article> searchArticle(ArticleQueryOption queryOption);
}
Original file line number Diff line number Diff line change
@@ -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<Article> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import com.goldbalance.dive.domain.article.domain.Article;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleRepository extends JpaRepository<Article, Long> {}
public interface ArticleRepository extends JpaRepository<Article, Long>, ArticleCustomRepository {}
Original file line number Diff line number Diff line change
@@ -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<Article> findArticles(ArticleQueryOption queryOption) {
return articleRepository.searchArticle(queryOption);
}
}

0 comments on commit e9bba3c

Please sign in to comment.