Skip to content

Commit

Permalink
Merge pull request #27 from kusitms-28th-Meetup-E/14-mainpage-api
Browse files Browse the repository at this point in the history
feat(#14):이슈 검색 API
  • Loading branch information
eojinny authored Nov 21, 2023
2 parents 0179343 + 93b1959 commit 62c0db1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gwangjang.server.domain.Issue.application.dto.res;

import gwangjang.server.domain.Issue.domain.entity.Topic;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.*;

@Getter
@Builder
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SearchListRes {
private Long issueId;
private String issueTitle;
private String issueDetail;
private String imgUrl;
private String topicTitle;
private String topicId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gwangjang.server.domain.Issue.application.dto.res;

import lombok.*;

import java.util.List;
@Getter
@Builder
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SearchRes {

String searchKeyword;
String searchCount;

List<SearchListRes> issueResList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gwangjang.server.domain.Issue.application.mapper;

import gwangjang.server.domain.Issue.application.dto.res.SearchListRes;
import gwangjang.server.domain.Issue.domain.entity.Issue;
import gwangjang.server.global.annotation.Mapper;

@Mapper
public class IssueMapper {
public SearchListRes mapToSearchListRes(Issue issue) {
SearchListRes searchListRes = new SearchListRes();
searchListRes.setIssueId(issue.getId());
searchListRes.setIssueTitle(issue.getIssueTitle());
searchListRes.setIssueDetail(issue.getIssueDetail());
searchListRes.setImgUrl(issue.getImgUrl());

// Topic이 null이 아닌 경우에만 매핑
if (issue.getTopic() != null) {
searchListRes.setTopicTitle(issue.getTopic().getTopicTitle());
searchListRes.setTopicId(String.valueOf(issue.getTopic().getId()));
}

return searchListRes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gwangjang.server.domain.Issue.application.dto.res.IssueDetailTopicRes;
import gwangjang.server.domain.Issue.application.dto.res.IssueRes;
import gwangjang.server.domain.Issue.application.dto.res.KeywordRes;
import gwangjang.server.domain.Issue.domain.entity.Issue;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -12,4 +13,5 @@ public interface IssueCustomRepository {
Optional<IssueRes> findIssueAndTopicById(Long issueId);
Optional<KeywordRes> findKeywordsByIssueId(Long issueId);
List<IssueDetailTopicRes> getAllIssueDetailTopicRes();
List<Issue> search(String keyword);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.querydsl.core.Tuple;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import gwangjang.server.domain.Issue.application.dto.res.IssueDetailTopicRes;
import gwangjang.server.domain.Issue.application.dto.res.IssueRes;
import gwangjang.server.domain.Issue.application.dto.res.KeywordRes;
import gwangjang.server.domain.Issue.domain.entity.Issue;
import gwangjang.server.domain.Issue.domain.entity.Keyword;
import gwangjang.server.domain.Issue.domain.entity.QIssue;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -106,5 +110,22 @@ public List<IssueDetailTopicRes> getAllIssueDetailTopicRes() {
))
.collect(Collectors.toList());
}
public List<Issue> search(String keyword) {
QIssue issue = QIssue.issue;

return jpaQueryFactory
.selectFrom(issue)
.leftJoin(issue.keywords).fetchJoin()
.where(
containsIgnoreCase(issue.issueTitle, keyword)
.or(containsIgnoreCase(issue.issueDetail, keyword))
.or(containsIgnoreCase(issue.topic.topicTitle, keyword))
)
.fetch();
}

private BooleanExpression containsIgnoreCase(StringPath path, String keyword) {
return path.lower().contains(keyword.toLowerCase());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gwangjang.server.domain.Issue.domain.service;

import gwangjang.server.domain.Issue.application.dto.res.*;
import gwangjang.server.domain.Issue.application.mapper.IssueMapper;
import gwangjang.server.domain.Issue.domain.entity.Issue;
import gwangjang.server.domain.Issue.domain.entity.Keyword;
import gwangjang.server.domain.Issue.domain.entity.Topic;
Expand All @@ -10,6 +11,7 @@
import gwangjang.server.domain.Issue.domain.repository.TopicRepository;
import gwangjang.server.domain.Issue.exception.NotFoundIssueException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand All @@ -25,6 +27,8 @@ public class IssueService {

private final IssueRepository issueRepository;

private final IssueMapper issueMapper = new IssueMapper();


public IssueRes findIssueAndTopicById(Long issueId) {
return issueQueryRepository.findIssueAndTopicById(issueId).orElseThrow(NotFoundIssueException::new);
Expand Down Expand Up @@ -116,5 +120,19 @@ public List<IssueDetailTopicRes> getAllIssueDetailTopicRes(){
List<IssueDetailTopicRes> list = issueRepository.getAllIssueDetailTopicRes();
return list;
}
public SearchRes search(String keyword) {
List<Issue> searchResults = issueRepository.search(keyword);

List<SearchListRes> issueResList = searchResults.stream()
.map(issueMapper::mapToSearchListRes)
.collect(Collectors.toList());

SearchRes searchRes = new SearchRes();
searchRes.setSearchKeyword(keyword);
searchRes.setSearchCount(String.valueOf(searchResults.size()));
searchRes.setIssueResList(issueResList);

return searchRes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ public ResponseEntity<SuccessResponse<List<IssueDetailRes>>> getIssueDetailAll()
public ResponseEntity<SuccessResponse<List<IssueDetailTopicRes>>> getIssueDetailAndTopicAll() {
return ResponseEntity.ok(SuccessResponse.create(IssueResponseMessage.GET_ISSUE_SUCCESS.getMessage(),this.issueService.getAllIssueDetailTopicRes()));
}
@GetMapping("/search/{keyword}")
public ResponseEntity<SuccessResponse<SearchRes>> search(@PathVariable String keyword) {
return ResponseEntity.ok(SuccessResponse.create(IssueResponseMessage.GET_ISSUE_SUCCESS.getMessage(),this.issueService.search(keyword)));
}

}

0 comments on commit 62c0db1

Please sign in to comment.