Skip to content

Commit 2aea3db

Browse files
committed
2 parents e372445 + e451015 commit 2aea3db

5 files changed

Lines changed: 52 additions & 33 deletions

File tree

src/main/java/com/coastee/server/chatroom/domain/repository/ChatRoomQuerydslRepository.java

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.coastee.server.chatroom.domain.ChatRoom;
44
import com.coastee.server.chatroom.domain.ChatRoomType;
55
import com.coastee.server.global.util.QuerydslUtil;
6-
import com.coastee.server.hashtag.domain.HashTag;
76
import com.coastee.server.server.domain.Server;
87
import com.querydsl.core.types.dsl.BooleanExpression;
98
import com.querydsl.jpa.JPQLQuery;
@@ -30,10 +29,10 @@ public Page<ChatRoom> findByServerAndTypeAndKeywordAndTagList(
3029
final Server server,
3130
final ChatRoomType chatRoomType,
3231
final String keyword,
33-
final List<HashTag> tagList,
32+
final List<String> tagNameList,
3433
final Pageable pageable
3534
) {
36-
List<ChatRoom> chatRoomList = findByServerAndTypeAndKeywordAndTagList(server, chatRoomType, keyword, tagList)
35+
List<ChatRoom> chatRoomList = findByServerAndTypeAndKeywordAndTagList(server, chatRoomType, keyword, tagNameList)
3736
.orderBy(QuerydslUtil.getSort(pageable, chatRoom))
3837
.offset(pageable.getOffset())
3938
.limit(pageable.getPageSize())
@@ -42,7 +41,7 @@ public Page<ChatRoom> findByServerAndTypeAndKeywordAndTagList(
4241
JPAQuery<Long> countQuery = query
4342
.select(chatRoom.count())
4443
.from(chatRoom)
45-
.where(chatRoom.in(findByServerAndTypeAndKeywordAndTagList(server, chatRoomType, keyword, tagList)));
44+
.where(chatRoom.in(findByServerAndTypeAndKeywordAndTagList(server, chatRoomType, keyword, tagNameList)));
4645

4746
return PageableExecutionUtils.getPage(chatRoomList, pageable, countQuery::fetchOne);
4847
}
@@ -51,39 +50,70 @@ private JPQLQuery<ChatRoom> findByServerAndTypeAndKeywordAndTagList(
5150
final Server server,
5251
final ChatRoomType chatRoomType,
5352
final String keyword,
54-
final List<HashTag> tagList
53+
final List<String> tagNameList
5554
) {
5655
return query
5756
.selectFrom(chatRoom)
57+
.join(chatRoom.user)
5858
.where(
5959
chatRoom.server.eq(server)
6060
.and(chatRoom.chatRoomType.eq(chatRoomType))
61-
.and(likeKeyword(keyword))
62-
.and(eqTagList(tagList))
61+
.and(containsKeyword(keyword))
62+
.and(eqTagList(tagNameList))
6363
);
6464
}
6565

6666
private BooleanExpression containsKeyword(final String keyword) {
6767
if (keyword == null || keyword.isBlank()) {
6868
return null;
6969
}
70-
return chatRoom.title.like(keyword);
70+
return chatRoom.title.contains(keyword)
71+
.or(chatRoom.content.contains(keyword))
72+
.or(chatRoom.user.nickname.contains(keyword))
73+
.or(containsTagList(List.of(keyword)))
74+
;
7175
}
7276

73-
private BooleanExpression eqTagList(final List<HashTag> tagList) {
74-
if (tagList == null || tagList.isEmpty()) {
77+
private BooleanExpression eqTagList(
78+
final List<String> tagNameList
79+
) {
80+
if (tagNameList == null || tagNameList.isEmpty()) {
81+
return null;
82+
}
83+
84+
BooleanExpression condition = tagNameList.stream()
85+
.map(hashTag.content::eq)
86+
.reduce(BooleanExpression::or)
87+
.orElse(null);
88+
89+
return chatRoom.in(
90+
query
91+
.select(chatRoomTag.chatRoom).from(chatRoomTag)
92+
.join(hashTag).on(condition.and(hashTag.eq(chatRoomTag.hashTag)))
93+
.where(chatRoom.eq(chatRoomTag.chatRoom))
94+
.groupBy(chatRoomTag.chatRoom)
95+
.having(chatRoomTag.chatRoom.count().eq(Long.valueOf(tagNameList.size())))
96+
);
97+
}
98+
99+
private BooleanExpression containsTagList(
100+
final List<String> tagNameList
101+
) {
102+
if (tagNameList == null || tagNameList.isEmpty()) {
75103
return null;
76104
}
105+
106+
BooleanExpression condition = tagNameList.stream()
107+
.map(hashTag.content::contains)
108+
.reduce(BooleanExpression::or)
109+
.orElse(null);
110+
77111
return chatRoom.in(
78112
query
79113
.select(chatRoomTag.chatRoom).from(chatRoomTag)
80-
.join(hashTag).on(
81-
hashTag.eq(chatRoomTag.hashTag)
82-
.and(hashTag.in(tagList))
83-
)
114+
.join(hashTag).on(condition.and(hashTag.eq(chatRoomTag.hashTag)))
84115
.where(chatRoom.eq(chatRoomTag.chatRoom))
85116
.groupBy(chatRoomTag.chatRoom)
86-
.having(chatRoomTag.chatRoom.count().eq(Long.valueOf(tagList.size())))
87117
);
88118
}
89119
}

src/main/java/com/coastee/server/chatroom/facade/ChatRoomFacade.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.coastee.server.chatroom.dto.request.ChatRoomCreateRequest;
1313
import com.coastee.server.chatroom.service.ChatRoomEntryService;
1414
import com.coastee.server.chatroom.service.ChatRoomService;
15-
import com.coastee.server.hashtag.domain.HashTag;
1615
import com.coastee.server.hashtag.service.HashTagService;
1716
import com.coastee.server.image.domain.DirName;
1817
import com.coastee.server.image.service.BlobStorageService;
@@ -28,8 +27,6 @@
2827
import org.springframework.transaction.annotation.Transactional;
2928
import org.springframework.web.multipart.MultipartFile;
3029

31-
import java.util.ArrayList;
32-
import java.util.HashSet;
3330
import java.util.List;
3431
import java.util.Map;
3532

@@ -170,15 +167,11 @@ private ChatRoomElements findAllByServer(
170167
final List<String> tagNameList,
171168
final Pageable pageable
172169
) {
173-
List<HashTag> hashTagList = new ArrayList<>();
174-
if (tagNameList != null && !tagNameList.isEmpty())
175-
hashTagList = hashTagService.findAllByContentIn(new HashSet<>(tagNameList));
176-
177170
Page<ChatRoom> chatRoomPage = chatRoomService.findByServerAndTypeAndKeywordAndTagList(
178171
server,
179172
type,
180173
keyword,
181-
hashTagList,
174+
tagNameList,
182175
pageable
183176
);
184177
Map<Long, Boolean> hasEnteredMap = chatRoomEntryService.findHasEnteredByChatRoomList(

src/main/java/com/coastee/server/chatroom/service/ChatRoomService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ public Page<ChatRoom> findByServerAndTypeAndKeywordAndTagList(
107107
final Server server,
108108
final ChatRoomType type,
109109
final String keyword,
110-
final List<HashTag> tagList,
110+
final List<String> tagNameList,
111111
final Pageable pageable
112112
) {
113113
return chatRoomQueryDSLRepository.findByServerAndTypeAndKeywordAndTagList(
114114
server,
115115
type,
116116
keyword,
117-
tagList,
117+
tagNameList,
118118
pageable
119119
);
120120
}

src/main/java/com/coastee/server/server/facade/ServerFacade.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,22 @@ public ServerHomeResponse getHomeWithConditions(
8484
PageRequest.of(0, 10)
8585
);
8686

87-
List<HashTag> searchTagList = new ArrayList<>();
88-
if (tagNameList != null && !tagNameList.isEmpty())
89-
searchTagList = hashTagService.findAllByContentIn(new HashSet<>(tagNameList));
9087
Page<ChatRoom> groupPage = chatRoomService.findByServerAndTypeAndKeywordAndTagList(
9188
server,
9289
ChatRoomType.GROUP,
9390
keyword,
94-
searchTagList,
91+
tagNameList,
9592
PageRequest.of(0, 3, Sort.by(DESC, "createdDate"))
9693
);
9794
Page<ChatRoom> meetingPage = chatRoomService.findByServerAndTypeAndKeywordAndTagList(
9895
server,
9996
ChatRoomType.MEETING,
10097
keyword,
101-
searchTagList,
98+
tagNameList,
10299
PageRequest.of(0, 3, Sort.by(DESC, "remainCount"))
103100
);
104101
if (isSearch(keyword, tagNameList))
105102
return new ServerHomeResponse(hashTagList, groupPage, meetingPage);
106-
107103
Page<Notice> noticePage = noticeService.findAllByServer(
108104
server,
109105
PageRequest.of(0, 10, Sort.by(DESC, "createdDate"))

src/test/java/com/coastee/server/server/controller/ServerHomeControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ void getHome() throws Exception {
113113
.header(ACCESS_TOKEN_HEADER, ACCESS_TOKEN)
114114
.contentType(MediaType.APPLICATION_JSON_VALUE)
115115
.param("keyword", "검색하는키워드")
116-
.param("tagList", "#검색", "#해시태그")
116+
.param("tags", "#검색", "#해시태그")
117117
.filter(
118118
document("get-server-home",
119119
pathParameters(
120120
parameterWithName("serverId").description("서버 아이디")
121121
),
122122
queryParameters(
123123
parameterWithName("keyword").description("검색 키워드"),
124-
parameterWithName("tagList").description("검색 해시태그")
124+
parameterWithName("tags").description("검색 해시태그")
125125
),
126126
requestHeaders(
127127
headerWithName(ACCESS_TOKEN_HEADER).description("액세스 토큰")

0 commit comments

Comments
 (0)