11package moadong .club .repository ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
35import lombok .AllArgsConstructor ;
46import moadong .club .enums .ClubState ;
57import moadong .club .payload .dto .ClubSearchResult ;
1113import org .springframework .data .mongodb .core .query .Criteria ;
1214import org .springframework .stereotype .Repository ;
1315
14- import java .util .ArrayList ;
15- import java .util .List ;
16-
1716
1817@ Repository
1918@ AllArgsConstructor
2019public class ClubSearchRepository {
20+
2121 private final MongoTemplate mongoTemplate ;
2222
23- public List <ClubSearchResult > searchClubsByKeyword (String keyword , String recruitmentStatus , String division , String category ) {
23+ public List <ClubSearchResult > searchClubsByKeyword (String keyword , String recruitmentStatus ,
24+ String division , String category ) {
2425 List <AggregationOperation > operations = new ArrayList <>();
2526
2627 operations .add (Aggregation .match (
27- new Criteria ().andOperator (
28- Criteria .where ("state" ).is (ClubState .AVAILABLE .getName ()))
28+ new Criteria ().andOperator (
29+ Criteria .where ("state" ).is (ClubState .AVAILABLE .getName ()))
2930 ));
3031
3132 Criteria criteria = getMatchedCriteria (recruitmentStatus , division , category );
@@ -36,33 +37,37 @@ public List<ClubSearchResult> searchClubsByKeyword(String keyword, String recrui
3637
3738 if (keyword != null && !keyword .trim ().isEmpty ()) {
3839 operations .add (Aggregation .match (new Criteria ().orOperator (
39- Criteria .where ("name" ).regex (keyword , "i" ),
40- Criteria .where ("recruitmentInformation.introduction" ).regex (keyword , "i" ),
41- Criteria .where ("recruitmentInformation.description" ).regex (keyword , "i" ),
42- Criteria .where ("recruitmentInformation.tags" ).regex (keyword , "i" )
40+ Criteria .where ("name" ).regex (keyword , "i" ),
41+ Criteria .where ("recruitmentInformation.introduction" ).regex (keyword , "i" ),
42+ Criteria .where ("recruitmentInformation.description" ).regex (keyword , "i" ),
43+ Criteria .where ("recruitmentInformation.tags" ).regex (keyword , "i" )
4344 )));
4445 }
4546 operations .add (Aggregation .unwind ("club_tags" , true ));
4647
4748 operations .add (
48- Aggregation .project ("name" , "state" , "category" , "division" )
49- .and ("recruitmentInformation.introduction" ).as ("introduction" )
50- .and ("recruitmentInformation.recruitmentStatus" ).as ("recruitmentStatus" )
51- .and ("recruitmentInformation.logo" ).as ("logo" )
52- .and ("recruitmentInformation.tags" ).as ("tags" ));
49+ Aggregation .project ("name" , "state" , "category" , "division" )
50+ .and ("recruitmentInformation.introduction" ).as ("introduction" )
51+ .and ("recruitmentInformation.recruitmentStatus" ).as ("recruitmentStatus" )
52+ .and ("recruitmentInformation.logo" ).as ("logo" )
53+ .and ("recruitmentInformation.tags" ).as ("tags" ));
5354
54- operations .add (Aggregation .sort (Sort .by (Sort .Order .asc ("division" ), Sort .Order .asc ("category" ))));
55+ operations .add (
56+ Aggregation .sort (Sort .by (Sort .Order .asc ("division" ), Sort .Order .asc ("category" ))));
5557
5658 Aggregation aggregation = Aggregation .newAggregation (operations );
57- AggregationResults <ClubSearchResult > results = mongoTemplate .aggregate (aggregation , "clubs" , ClubSearchResult .class );
59+ AggregationResults <ClubSearchResult > results = mongoTemplate .aggregate (aggregation , "clubs" ,
60+ ClubSearchResult .class );
5861 return results .getMappedResults ();
5962 }
6063
61- private Criteria getMatchedCriteria (String recruitmentStatus , String division , String category ) {
64+ private Criteria getMatchedCriteria (String recruitmentStatus , String division ,
65+ String category ) {
6266 List <Criteria > criteriaList = new ArrayList <>();
6367
6468 if (recruitmentStatus != null && !"all" .equalsIgnoreCase (recruitmentStatus )) {
65- criteriaList .add (Criteria .where ("recruitmentInformation.recruitmentStatus" ).is (recruitmentStatus ));
69+ criteriaList .add (
70+ Criteria .where ("recruitmentInformation.recruitmentStatus" ).is (recruitmentStatus ));
6671 }
6772 if (division != null && !"all" .equalsIgnoreCase (division )) {
6873 criteriaList .add (Criteria .where ("division" ).is (division ));
@@ -74,6 +79,6 @@ private Criteria getMatchedCriteria(String recruitmentStatus, String division, S
7479 if (!criteriaList .isEmpty ()) {
7580 return new Criteria ().andOperator (criteriaList .toArray (new Criteria [0 ]));
7681 }
77- return null ;
82+ return new Criteria () ;
7883 }
7984}
0 commit comments