Skip to content

Commit

Permalink
Merge pull request #41 from pknu-wap/feature/#37-search-keyword
Browse files Browse the repository at this point in the history
[feature]검색 기능 추가
  • Loading branch information
Zepelown authored Feb 2, 2025
2 parents c503c59 + 9e460ae commit 15d7191
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 6 deletions.
10 changes: 10 additions & 0 deletions backend/src/main/java/moadong/club/controller/ClubController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import moadong.club.service.ClubCommandService;
import moadong.club.service.ClubDetailedPageService;
import moadong.club.service.ClubSearchFilterService;
import moadong.club.service.ClubSearchService;
import moadong.global.payload.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -30,6 +31,7 @@ public class ClubController {
private final ClubCommandService clubCommandService;
private final ClubDetailedPageService clubDetailedPageService;
private final ClubSearchFilterService clubSearchFilterService;
private final ClubSearchService clubSearchService;

@PostMapping("/")
@Operation(summary = "클럽 생성", description = "클럽을 생성합니다.")
Expand Down Expand Up @@ -60,4 +62,12 @@ public ResponseEntity<?> getClubsByFilter(
ClubSearchResponse clubSearchResponse = clubSearchFilterService.getClubsByFilter(availability, classification, division);
return Response.ok(clubSearchResponse);
}

@GetMapping("/search/")
public ResponseEntity<?> searchClubsByKeyword(
@RequestParam(value = "keyword", required = true) String keyword
){
ClubSearchResponse clubSearchResponse = clubSearchService.searchClubsByKeyword(keyword);
return Response.ok(clubSearchResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface ClubInformationSearchProjection {
String getLogo();
String getDescription();
String getIntroduction();

static ClubInformationSearchProjection empty() {
return new ClubInformationSearchProjection() {
Expand All @@ -12,7 +12,7 @@ public String getLogo() {
}

@Override
public String getDescription() {
public String getIntroduction() {
return "";
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record ClubSearchResult(
String state,
String classification,
String division,
String description
String introduction
) {
public static ClubSearchResult createClubSearchResult(
String id,
Expand All @@ -21,7 +21,7 @@ public static ClubSearchResult createClubSearchResult(
String state,
String classification,
String division,
String description
String introduction
){
return ClubSearchResult.builder()
.id(id)
Expand All @@ -31,7 +31,7 @@ public static ClubSearchResult createClubSearchResult(
.state(state)
.classification(classification)
.division(division)
.description(description)
.introduction(introduction)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package moadong.club.repository;

import moadong.club.payload.dto.ClubSearchResult;

import java.util.List;

public interface ClubKeywordSearchRepository {
List<ClubSearchResult> searchResult(String keyword);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package moadong.club.repository;

import lombok.AllArgsConstructor;
import moadong.club.payload.dto.ClubSearchResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@AllArgsConstructor
public class ClubKeywordSearchRepositoryImpl implements ClubKeywordSearchRepository {
private final MongoTemplate mongoTemplate;

@Override
public List<ClubSearchResult> searchResult(String keyword) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.lookup("club_information", "_id", "clubId", "club_info"),
Aggregation.lookup("club_tags", "_id", "clubId", "club_tags"),
Aggregation.match(new Criteria().orOperator(
Criteria.where("name").regex(keyword, "i"),
Criteria.where("club_info.description").regex(keyword, "i"),
Criteria.where("club_tags.tag").regex(keyword, "i")
)),
Aggregation.unwind("club_tags", true),
Aggregation.group("_id")
.first("name").as("name")
.first("state").as("state")
.first("classification").as("classification")
.first("division").as("division")
.first("club_info").as("club_info")
.push("club_tags.tag").as("tags"),
Aggregation.project("name", "state", "classification", "division")
.and("club_info.introduction").as("introduction")
.and("club_info.logo").as("logo")
.and("tags").as("tags")
);
AggregationResults<ClubSearchResult> results = mongoTemplate.aggregate(aggregation, "club", ClubSearchResult.class);
return results.getMappedResults();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ClubSearchResponse getClubsByFilter(
.state(String.valueOf(club.getState().getDesc()))
.division(club.getDivision())
.classification(club.getClassification())
.description(informationByClubId.getDescription())
.introduction(informationByClubId.getIntroduction())
.build();

clubSearchResults.add(clubSearchResult);
Expand Down
22 changes: 22 additions & 0 deletions backend/src/main/java/moadong/club/service/ClubSearchService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package moadong.club.service;

import lombok.AllArgsConstructor;
import moadong.club.payload.dto.ClubSearchResult;
import moadong.club.payload.response.ClubSearchResponse;
import moadong.club.repository.ClubKeywordSearchRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ClubSearchService {
private final ClubKeywordSearchRepository clubKeywordSearchRepository;
public ClubSearchResponse searchClubsByKeyword(String keyword) {
List<ClubSearchResult> clubSearchResults = clubKeywordSearchRepository.searchResult(keyword);

return ClubSearchResponse.builder()
.clubs(clubSearchResults)
.build();
}
}

0 comments on commit 15d7191

Please sign in to comment.