Skip to content

Commit a54bdbc

Browse files
authored
Merge pull request #102 from CommitField/Feature/89
Fix/89
2 parents 6025330 + a0a424c commit a54bdbc

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/controller/ChatRoomController.javaโ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,30 @@ public GlobalResponse<Object> getMyHeartRoomList(
213213
return GlobalResponse.success("์ข‹์•„์š” ๋ˆ„๋ฅธ ์ฑ„ํŒ…๋ฐฉ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์™„๋ฃŒ", list);
214214
}
215215

216+
// ์ฑ„ํŒ…๋ฐฉ ์ œ๋ชฉ ๊ฒ€์ƒ‰ ์กฐํšŒ
217+
@GetMapping("/room/search")
218+
@LoginCheck
219+
public GlobalResponse<Object> searchRoomName(
220+
@RequestParam(name = "roomName") String roomName,
221+
Pageable pageable) {
222+
223+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
224+
225+
if (authentication instanceof OAuth2AuthenticationToken) {
226+
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
227+
Long userId = principal.getId(); // Extract userId from the principal
228+
229+
if (roomName.isEmpty()) {
230+
throw new IllegalArgumentException("์›ํ•˜๋Š” ์ฑ„ํŒ…๋ฐฉ์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”.");
231+
}
232+
233+
List<ChatRoomDto> list = chatRoomService.searchRoomByTitle(roomName, userId, pageable);
234+
return GlobalResponse.success(list);
235+
} else {
236+
throw new IllegalArgumentException("๋กœ๊ทธ์ธ ํ›„์— ์ด์šฉํ•ด ์ฃผ์„ธ์š”.");
237+
}
238+
}
239+
216240

217241

218242

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/repository/ChatRoomRepository.javaโ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
3636
@Query("SELECT c FROM ChatRoom c WHERE c.id IN :ids ORDER BY c.createdAt DESC")
3737
Page<ChatRoom> findChatRoomByInId(@Param("ids") List<Long> ids, Pageable pageable);
3838

39+
@Query("SELECT c FROM ChatRoom c WHERE c.title LIKE CONCAT('%', :title, '%')")
40+
Page<ChatRoom> findChatRoomWithPartOfTitle(@Param("title") String title, Pageable pageable);
41+
3942
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomService.javaโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public interface ChatRoomService {
3535
List<ChatRoomDto> myHeartRoomList(Long userId, Pageable pageable);
3636

3737
void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest);
38+
39+
List<ChatRoomDto> searchRoomByTitle(String roomName, Long userId, Pageable pageable);
3840
}

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomServiceImpl.javaโ€Ž

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.TimeUnit;
3030
import java.util.stream.Collectors;
3131

32+
import static cmf.commitField.global.error.ErrorCode.NOT_FOUND_ROOM;
3233
import static java.time.LocalDateTime.now;
3334

3435
@Service
@@ -138,7 +139,7 @@ public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest)
138139

139140
// room ์กฐํšŒ
140141
ChatRoom chatRoom = chatRoomRepository.findById(roomId) // lock (๊ธฐ์กด)
141-
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ROOM));
142+
.orElseThrow(() -> new CustomException(NOT_FOUND_ROOM));
142143

143144
// user_chatroom ํ˜„์žฌ ์ธ์› ์นด์šดํŠธ (๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง)
144145
Long currentUserCount = userChatRoomRepository.countNonLockByChatRoomId(roomId); // lock (๊ธฐ์กด)
@@ -205,16 +206,14 @@ public void outRoom(Long userId, Long roomId) {
205206
return;
206207
}
207208
// ๋ฐฉ์žฅ์ด๋ผ๋ฉด ๋ฐฉ ์‚ญ์ œ
208-
chatMessageRepository.deleteChatMsgByChatRoom_Id(roomId); //๋ฐฉ ์‚ญ์ œ ์‹œ ์ฑ„ํŒ…๋„ ๋‹ค ์‚ญ์ œ(ํ•„์š” ์‹œ)
209+
chatMessageRepository.deleteChatMsgByChatRoom_Id(roomId); //๋ฐฉ ์‚ญ์ œ ์‹œ ์ฑ„ํŒ…๋„ ๋‹ค ์‚ญ์ œ
210+
// ๋ฐฉ ์‚ญ์ œ์‹œ ์ฑ„ํƒฑ ๋ฉ”์„ธ์ง€ ์ „์ฒด ์‚ญ์ œ(ํฌํ•จ)
209211
userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
212+
213+
//์ฑ„ํŒ…๋ฐฉ ์‚ญ์ œ
210214
chatRoomRepository.deleteById(roomId);
211215

212-
// ๋ฐฉ์˜ ์ƒ์„ฑ์ž์™€ ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ
213-
boolean isCreator = Objects.equals(room.getRoomCreator(), userId);
214216

215-
// ๋ฐฉ์žฅ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ํ•ญ์ƒ ์‚ฌ์šฉ์ž-์ฑ„ํŒ…๋ฐฉ ์—ฐ๊ฒฐ๋งŒ ์ œ๊ฑฐ
216-
// ๋ฐฉ์ด ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ  ๋ชฉ๋ก์— ๊ณ„์† ํ‘œ์‹œ๋จ
217-
userChatRoomRepository.deleteUserChatRoomByChatRoom_IdAndUserId(roomId, userId);
218217
}
219218

220219
// ๋ฐฉ ์‚ญ์ œ๋Š” ๋ณ„๋„์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌ
@@ -250,6 +249,31 @@ public void updateRoom(Long roomId, ChatRoomUpdateRequest chatRoomUpdateRequest,
250249
chatRoomRepository.save(room);
251250
}
252251

252+
@Override
253+
@Transactional(readOnly = true)
254+
public List<ChatRoomDto> searchRoomByTitle(String roomName, Long userId, Pageable pageable) {
255+
getUser(userId);
256+
Page<ChatRoom> search = chatRoomRepository.findChatRoomWithPartOfTitle(roomName, pageable);
257+
258+
List<ChatRoom> searchRoomList = search.toList();
259+
List<ChatRoomDto> chatRoomDtos = new ArrayList<>();
260+
if (searchRoomList.isEmpty()) {
261+
throw new CustomException(NOT_FOUND_ROOM);
262+
}
263+
264+
for (ChatRoom chatRoom : searchRoomList) {
265+
ChatRoomDto build = ChatRoomDto.builder()
266+
.id(chatRoom.getId())
267+
.title(chatRoom.getTitle())
268+
.heartCount(chatRoom.getHearts().size())
269+
.currentUserCount((long) chatRoom.getUserChatRooms().size())
270+
.userCountMax(chatRoom.getUserCountMax())
271+
.build();
272+
chatRoomDtos.add(build);
273+
}
274+
return chatRoomDtos;
275+
}
276+
253277
private User getUser(Long userId) {
254278
return userRepository.findById(userId)
255279
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatRoom/service/CocurrencyChatRoomTest.javaโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//import cmf.commitField.domain.chat.chatRoom.entity.ChatRoom;
44
//import cmf.commitField.domain.chat.chatRoom.repository.ChatRoomRepository;
55
//import cmf.commitField.domain.chat.userChatRoom.repository.UserChatRoomRepository;
6+
//import cmf.commitField.domain.user.service.UserService;
67
//import org.springframework.beans.factory.annotation.Autowired;
78
//
89
//import java.util.ArrayList;

โ€Žsrc/main/java/cmf/commitField/domain/chat/userChatRoom/repository/UserChatRoomRepository.javaโ€Ž

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long
2323
void deleteUserChatRoomByChatRoom_Id(Long chatRoomId);
2424
// ํŠน์ • ๋ฐฉ์—์„œ ํŠน์ • ์‚ฌ์šฉ์ž๋งŒ ์‚ญ์ œ
2525
void deleteUserChatRoomByChatRoom_IdAndUserId(Long chatRoomId, Long userId);
26-
// ํŠน์ • ๋ฐฉ๊ณผ ์‚ฌ์šฉ์ž ๊ด€๊ณ„ ์‚ญ์ œ
27-
void deleteUserChatRoomByUserId(Long userId);
28-
// ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด๋‹น ๋ฐฉ์— ์ฐธ์—ฌํ•œ ์—ฌ๋ถ€ ํ™•์ธ
29-
boolean existsByChatRoomIdAndUserId(Long roomId, Long userId);
26+
3027
// ํŠน์ • ๋ฐฉ์— ์ฐธ์—ฌํ•œ ๋ชจ๋“  UserChatRoom ๊ด€๊ณ„ ์กฐํšŒ
3128
List<UserChatRoom> findByChatRoom_Id(Long chatRoomId);
3229
@Query("select u.user.id from UserChatRoom u where u.chatRoom.id = ?1")
@@ -36,7 +33,6 @@ public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long
3633
//out room ์กฐํšŒ
3734
List<UserChatRoom> findUserByChatRoomId(Long roomId);
3835

39-
Optional<UserChatRoom> findByUserId(Long userId);
4036
//์ฑ„ํŒ…๋ฐฉ joinํ•œ user
4137
Optional<UserChatRoom> findByUserIdAndChatRoomId(Long userId, Long chatRoomId);
4238

0 commit comments

Comments
ย (0)