-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from DuruDuru-UMC-7th/119-feat-채팅방-목록-조회api
채팅방 목록 조회 API 작성 완료
- Loading branch information
Showing
7 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/com/backend/DuruDuru/global/converter/ChattingConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.backend.DuruDuru.global.converter; | ||
|
||
import com.backend.DuruDuru.global.domain.entity.Chatting; | ||
import com.backend.DuruDuru.global.domain.entity.ChattingRoom; | ||
import com.backend.DuruDuru.global.domain.entity.Message; | ||
import com.backend.DuruDuru.global.web.dto.Chatting.ChattingResponseDTO; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ChattingConverter { | ||
|
||
public static ChattingResponseDTO.ChattingRoomDetailDTO toChattingRoomDetailDTO(ChattingRoom chattingRoom, Long currentMemberId) { | ||
// 거래유형 가져오기 | ||
String tradeType = chattingRoom.getTrade().getTradeType().toString(); | ||
|
||
// 위치정보 가져오기 | ||
String location = ""; | ||
if (chattingRoom.getTrade().getMember().getTown() != null) { | ||
location = chattingRoom.getTrade().getMember().getTown().getEupmyeondong(); | ||
} | ||
|
||
// 최신 메시지 찾기 | ||
Message lastMessage = null; | ||
for (Chatting chatting : chattingRoom.getChattings()) { | ||
for (Message message : chatting.getMessages()) { | ||
if (lastMessage == null || message.getSentTime().isAfter(lastMessage.getSentTime())) { | ||
lastMessage = message; | ||
} | ||
} | ||
} | ||
String lastMessageContent = lastMessage != null ? lastMessage.getContent() : ""; | ||
LocalDateTime lastMessageDate = lastMessage != null ? lastMessage.getSentTime() : null; | ||
LocalDateTime sentTime = lastMessageDate; // 마지막 메시지의 전송시간을 보낸 시간으로 사용 | ||
|
||
// 읽은 메시지 계산 | ||
int unreadCount = 0; | ||
for (Chatting chatting : chattingRoom.getChattings()) { | ||
for (Message message : chatting.getMessages()) { | ||
if (!message.isRead() && !message.getMember().getMemberId().equals(currentMemberId)) { | ||
unreadCount++; | ||
} | ||
} | ||
} | ||
|
||
// 마지막 메시지가 존재하면 발신자 닉네임 가져오기 | ||
String username = (lastMessage != null) | ||
? lastMessage.getMember().getNickName() | ||
: chattingRoom.getTrade().getMember().getNickName(); | ||
|
||
return ChattingResponseDTO.ChattingRoomDetailDTO.builder() | ||
.chatRoomId(chattingRoom.getChattingRoomId()) | ||
.username(username) | ||
.tradeType(tradeType) | ||
.location(location) | ||
.lastMessage(lastMessageContent) | ||
.lastMessageDate(lastMessageDate) | ||
.unreadCount(unreadCount) | ||
.sentTime(sentTime) | ||
.build(); | ||
} | ||
|
||
//ChattingRoom엔티티를 ChattingRoomListDTO로 변환 | ||
public static ChattingResponseDTO.ChattingRoomListDTO toChattingRoomListDTO(List<ChattingRoom> chattingRooms, Long currentMemberId) { | ||
List<ChattingResponseDTO.ChattingRoomDetailDTO> detailDTOList = chattingRooms.stream() | ||
.map(chattingRoom -> toChattingRoomDetailDTO(chattingRoom, currentMemberId)) | ||
.collect(Collectors.toList()); | ||
|
||
return ChattingResponseDTO.ChattingRoomListDTO.builder() | ||
.chatRooms(detailDTOList) | ||
.count(detailDTOList.size()) | ||
.build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/backend/DuruDuru/global/repository/ChattingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.backend.DuruDuru.global.repository; | ||
|
||
import com.backend.DuruDuru.global.domain.entity.ChattingRoom; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface ChattingRepository extends JpaRepository<ChattingRoom, Long> { | ||
@Query("select distinct cr from ChattingRoom cr join cr.chattings c where c.member.memberId = :memberId") | ||
List<ChattingRoom> findChattingRoomsByMemberId(@Param("memberId") Long memberId); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/backend/DuruDuru/global/service/ChattingService/ChattingQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.backend.DuruDuru.global.service.ChattingService; | ||
|
||
import com.backend.DuruDuru.global.web.dto.Chatting.ChattingResponseDTO; | ||
|
||
public interface ChattingQueryService { | ||
ChattingResponseDTO.ChattingRoomListDTO getChattingRoomList(Long memberId); | ||
} |
26 changes: 26 additions & 0 deletions
26
...in/java/com/backend/DuruDuru/global/service/ChattingService/ChattingQueryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.backend.DuruDuru.global.service.ChattingService; | ||
|
||
import com.backend.DuruDuru.global.converter.ChattingConverter; | ||
import com.backend.DuruDuru.global.domain.entity.ChattingRoom; | ||
import com.backend.DuruDuru.global.repository.ChattingRepository; | ||
import com.backend.DuruDuru.global.web.dto.Chatting.ChattingResponseDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ChattingQueryServiceImpl implements ChattingQueryService { | ||
|
||
private final ChattingRepository chattingRoomRepository; | ||
|
||
//회원이 참여한 채팅방 조회 | ||
@Override | ||
public ChattingResponseDTO.ChattingRoomListDTO getChattingRoomList(Long memberId) { | ||
List<ChattingRoom> chattingRooms = chattingRoomRepository.findChattingRoomsByMemberId(memberId); | ||
return ChattingConverter.toChattingRoomListDTO(chattingRooms, memberId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/backend/DuruDuru/global/web/dto/Chatting/ChattingRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.backend.DuruDuru.global.web.dto.Chatting; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class ChattingRequestDTO { | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/backend/DuruDuru/global/web/dto/Chatting/ChattingResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.backend.DuruDuru.global.web.dto.Chatting; | ||
|
||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class ChattingResponseDTO { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class ChattingRoomDetailDTO { | ||
private Long chatRoomId; | ||
private String username; | ||
private String tradeType; | ||
private String location; | ||
private String lastMessage; | ||
private LocalDateTime lastMessageDate; | ||
private int unreadCount; | ||
private LocalDateTime sentTime; | ||
} | ||
|
||
//ChttingRoom상세 정보 담아서 개수와 함께 반환 하는 DTO | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public static class ChattingRoomListDTO { | ||
private int count; // 채팅방 개수 | ||
private List<ChattingRoomDetailDTO> chatRooms; // 채팅방 상세 정보 리스트 | ||
} | ||
} |