Skip to content

Commit

Permalink
[FEAT] 채팅방 검색기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
devchlee12 committed Jun 17, 2024
1 parent 3811d21 commit e30ad89
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
21 changes: 15 additions & 6 deletions src/main/java/out4ider/healthsenior/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,36 @@ public void joinChat(@PathVariable Long chatRoom, Principal principal) throws Ex

@ResponseBody
@GetMapping("/chatroom/list")
public List<ChatRoomResponseDto> chatRoomList(@RequestParam(value = "page", defaultValue = "0") int page, Principal principal) throws Exception {
public List<ChatRoomResponseDto> chatRoomList(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "keyword", defaultValue = "") String keyword,Principal principal) throws Exception {
SeniorUser seniorUser = seniorUserService.findByOauth2Id(principal.getName());
List<CommunityChatRoom> chatRoomList = communityChatRoomService.getChatRoomList(page,seniorUser.getUserId());
List<CommunityChatRoom> chatRoomList = communityChatRoomService.getChatRoomList(page,keyword,seniorUser.getUserId());
return chatRoomList.stream().map(CommunityChatRoom::toResponseDto).collect(Collectors.toList());

}

@ResponseBody
@GetMapping("/chatroom/list/{category}")
public List<ChatRoomResponseDto> chatRoomList(@PathVariable String category, @RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "keyword", defaultValue = "") String keyword, Principal principal) throws Exception {
SeniorUser seniorUser = seniorUserService.findByOauth2Id(principal.getName());
List<CommunityChatRoom> chatRoomList = communityChatRoomService.getChatRoomListByCategory(category,keyword,page,seniorUser.getUserId());
return chatRoomList.stream().map(CommunityChatRoom::toResponseDto).collect(Collectors.toList());

}

@ResponseBody
@GetMapping("/chatroom/mylist")
public List<ChatRoomResponseDto> myChatRoomList(@RequestParam(value = "page", defaultValue = "0") int page, Principal principal) throws Exception {
public List<ChatRoomResponseDto> myChatRoomList(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "keyword", defaultValue = "") String keyword, Principal principal) throws Exception {
String name = principal.getName();
SeniorUser seniorUser = seniorUserService.findByOauth2Id(name);
List<CommunityChatRoom> myChatRoomList = communityChatRoomService.getMyChatRoomList(page, seniorUser.getUserId());
List<CommunityChatRoom> myChatRoomList = communityChatRoomService.getMyChatRoomList(page,keyword, seniorUser.getUserId());
return myChatRoomList.stream().map(CommunityChatRoom::toResponseDto).collect(Collectors.toList());
}

@GetMapping("/chatroom/mylist/{category}")
public List<ChatRoomResponseDto> myChatRoomListWithCategory(@PathVariable String category, @RequestParam(value = "page", defaultValue = "0") int page, Principal principal) throws Exception {
public List<ChatRoomResponseDto> myChatRoomListWithCategory(@PathVariable String category, @RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "keyword", defaultValue = "") String keyword, Principal principal) throws Exception {
String name = principal.getName();
SeniorUser seniorUser = seniorUserService.findByOauth2Id(name);
List<CommunityChatRoom> myChatRoomListByCategory = communityChatRoomService.getMyChatRoomListByCategory(category, page, seniorUser.getUserId());
List<CommunityChatRoom> myChatRoomListByCategory = communityChatRoomService.getMyChatRoomListByCategory(category,keyword, page, seniorUser.getUserId());
return myChatRoomListByCategory.stream().map(CommunityChatRoom::toResponseDto).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package out4ider.healthsenior.repository;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,15 +10,16 @@
import java.util.List;

public interface CommunityChatRoomRepository extends JpaRepository<CommunityChatRoom,Long> {
public List<CommunityChatRoom> findByChatRoomIdNotIn(List<Long> chatRoomIds);
// Page<CommunityChatRoom> findByChatRoomIdNotIn(List<Long> chatRoomIds,Pageable pageable);

@Query("select c from CommunityChatRoom c where not exists (select 1 from c.communityChatRelation m where m.seniorUser.userId = ?1)")
Page<CommunityChatRoom> findByChatRoomIdNotIn(Long userId, Pageable pageable);
@Query("select c from CommunityChatRoom c where not exists (select 1 from c.communityChatRelation m where m.seniorUser.userId = ?1) and (c.title like concat('%',?2,'%') or c.description like concat('%',?2,'%'))")
Page<CommunityChatRoom> findByChatRoomIdNotIn(Long userId, String keyword, Pageable pageable);

@Query("select c from CommunityChatRoom c join c.communityChatRelation m where m.seniorUser.userId = ?1")
Page<CommunityChatRoom> findAllByUserId(Long userId,Pageable pageable);
@Query("select c from CommunityChatRoom c where not exists (select 1 from c.communityChatRelation m where m.seniorUser.userId = ?1) and c.sportKind = ?2 and (c.title like concat('%',?3,'%') or c.description like concat('%',?3,'%'))")
Page<CommunityChatRoom> findByChatRoomIdNotInByCategory(Long userId, String category, String keyword, Pageable pageable);

@Query("select c from CommunityChatRoom c join c.communityChatRelation m where m.seniorUser.userId = ?1 and c.sportKind = ?2")
Page<CommunityChatRoom> findAllByUserIdAndCategory(Long userId, String category, Pageable pageable);
@Query("select c from CommunityChatRoom c join c.communityChatRelation m where m.seniorUser.userId = ?1 and (c.title like concat('%',?2,'%') or c.description like concat('%',?2,'%'))")
Page<CommunityChatRoom> findAllByUserId(Long userId,String keyword,Pageable pageable);

@Query("select c from CommunityChatRoom c join c.communityChatRelation m where m.seniorUser.userId = ?1 and c.sportKind = ?2 and (c.title like concat('%',?3,'%') or c.description like concat('%',?3,'%'))")
Page<CommunityChatRoom> findAllByUserIdAndCategory(Long userId, String category, String keyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ public CommunityChatRoom saveChatRoom(NewChatDto newChatDto, String oauth2Id){
return communityChatRoomRepository.save(communityChatRoom);
}

public List<CommunityChatRoom> getChatRoomList(int page, Long userId){
public List<CommunityChatRoom> getChatRoomList(int page, String keyword, Long userId){
Pageable pageable = PageRequest.of(page,10, Sort.by("startDate").descending());

return communityChatRoomRepository.findByChatRoomIdNotIn(userId,pageable).getContent();
return communityChatRoomRepository.findByChatRoomIdNotIn(userId,keyword,pageable).getContent();
}

public List<CommunityChatRoom> getChatRoomListByCategory(String category,String keyword,int page,Long userId){
Pageable pageable = PageRequest.of(page,10, Sort.by("startDate").descending());

return communityChatRoomRepository.findByChatRoomIdNotInByCategory(userId,category,keyword,pageable).getContent();
}



public CommunityChatRoom getChatRoom(Long chatRoomId) throws Exception {
Optional<CommunityChatRoom> byId = communityChatRoomRepository.findById(chatRoomId);
if (byId.isEmpty()){
Expand All @@ -47,14 +55,14 @@ public CommunityChatRoom getChatRoom(Long chatRoomId) throws Exception {
return byId.get();
}

public List<CommunityChatRoom> getMyChatRoomList(int page, Long userId){
public List<CommunityChatRoom> getMyChatRoomList(int page,String keyword, Long userId){
Pageable pageable = PageRequest.of(page,10,Sort.by("startDate").descending());

return communityChatRoomRepository.findAllByUserId(userId,pageable).getContent();
return communityChatRoomRepository.findAllByUserId(userId,keyword,pageable).getContent();
}

public List<CommunityChatRoom> getMyChatRoomListByCategory(String category,int page,Long userId){
public List<CommunityChatRoom> getMyChatRoomListByCategory(String category, String keyword,int page,Long userId){
Pageable pageable = PageRequest.of(page, 10, Sort.by("startDate").descending());
return communityChatRoomRepository.findAllByUserIdAndCategory(userId,category,pageable).getContent();
return communityChatRoomRepository.findAllByUserIdAndCategory(userId,category,keyword,pageable).getContent();
}
}

0 comments on commit e30ad89

Please sign in to comment.