-
Notifications
You must be signed in to change notification settings - Fork 1
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 #10 from UMC6th-TeamC/chat
Merge Chat
- Loading branch information
Showing
15 changed files
with
224 additions
and
12 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/umc/teamC/domain/chat/controller/ChatRoomController.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,34 @@ | ||
package com.umc.teamC.domain.chat.controller; | ||
|
||
import com.umc.teamC.domain.chat.dto.ChatRoomDto; | ||
import com.umc.teamC.domain.chat.service.RestChatService; | ||
import com.umc.teamC.domain.user.entity.User; | ||
import com.umc.teamC.global.common.BaseResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@CrossOrigin("*") | ||
@RequestMapping("/chat-group") | ||
public class ChatRoomController { | ||
|
||
private final RestChatService restChatService; | ||
|
||
// 채팅방 목록 조회 - 채팅방 지역, 시간, 마지막 메세지 전달 | ||
@GetMapping("") | ||
public BaseResponse<List<ChatRoomDto.ChatRoomResponseDto>> getChatRoomList( | ||
@AuthenticationPrincipal User user | ||
) { | ||
List<ChatRoomDto.ChatRoomResponseDto> chatRoomListResponseDto = | ||
restChatService.getChatRoomList(user); | ||
return BaseResponse.onSuccess(chatRoomListResponseDto); | ||
} | ||
|
||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc/teamC/domain/chat/converter/ChatRoomConverter.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,27 @@ | ||
package com.umc.teamC.domain.chat.converter; | ||
|
||
import com.umc.teamC.domain.chat.dto.ChatRoomDto; | ||
import com.umc.teamC.domain.chat.entity.ChatRoom; | ||
|
||
import java.util.List; | ||
|
||
public class ChatRoomConverter { | ||
|
||
// public static ChatRoomDto.ChatRoomListResponseDto toChatRoomListResponseDto(List<ChatRoom> chatRoomList) { | ||
// List<ChatRoomDto.ChatRoomResponseDto> chatRoomResponseDtoList = chatRoomList.stream() | ||
// .map(ChatRoomConverter::toChatRoomResponseDto) | ||
// .toList(); | ||
// return ChatRoomDto.ChatRoomListResponseDto.builder() | ||
// .ChatRoomResponseDtoList(chatRoomResponseDtoList) | ||
// .build(); | ||
// } | ||
|
||
public static ChatRoomDto.ChatRoomResponseDto toChatRoomResponseDto(ChatRoom chatRoom, String lastMessage, Integer memberCount) { | ||
return ChatRoomDto.ChatRoomResponseDto.builder() | ||
.dateTime(chatRoom.getDateTime()) | ||
.region(chatRoom.getRegion()) | ||
.lastMessage(lastMessage) | ||
.memberCount(memberCount) | ||
.build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/umc/teamC/domain/chat/dto/ChatRoomDto.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,24 @@ | ||
package com.umc.teamC.domain.chat.dto; | ||
|
||
import lombok.Builder; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class ChatRoomDto { | ||
|
||
@Builder | ||
public record ChatRoomListResponseDto ( | ||
List<ChatRoomResponseDto> ChatRoomResponseDtoList | ||
) { | ||
} | ||
|
||
@Builder | ||
public record ChatRoomResponseDto ( | ||
LocalDateTime dateTime, | ||
String region, | ||
String lastMessage, | ||
Integer memberCount | ||
) { | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/umc/teamC/domain/chat/entity/ChatRoom.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,30 @@ | ||
package com.umc.teamC.domain.chat.entity; | ||
|
||
import com.umc.teamC.domain.user.entity.User; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Builder | ||
@Getter | ||
public class ChatRoom { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long roomId; | ||
|
||
private LocalDateTime dateTime; | ||
private String region; | ||
|
||
@OneToMany(mappedBy = "chatRoom") | ||
private List<ChatRoomUser> chatRoomUserList = new ArrayList<>(); | ||
|
||
@OneToMany(mappedBy = "chatRoom") | ||
private List<Chat> chatList = new ArrayList<>(); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/umc/teamC/domain/chat/entity/ChatRoomUser.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,25 @@ | ||
package com.umc.teamC.domain.chat.entity; | ||
|
||
import com.umc.teamC.domain.user.entity.User; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Builder | ||
@Getter | ||
public class ChatRoomUser { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long chatRoomUserId; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "chat_room_id") | ||
private ChatRoom chatRoom; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/umc/teamC/domain/chat/repository/ChatRepository.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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package com.umc.teamC.domain.chat.repository; | ||
|
||
import com.umc.teamC.domain.chat.entity.Chat; | ||
import com.umc.teamC.domain.chat.entity.ChatRoom; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ChatRepository extends JpaRepository<Chat, Long> { | ||
Chat findFirstByChatRoomOrderByCreatedAtDesc(ChatRoom chatRoom); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/umc/teamC/domain/chat/repository/ChatRoomRepository.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,9 @@ | ||
package com.umc.teamC.domain.chat.repository; | ||
|
||
import com.umc.teamC.domain.chat.entity.ChatRoom; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/umc/teamC/domain/chat/repository/ChatRoomUserRepository.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.umc.teamC.domain.chat.repository; | ||
|
||
import com.umc.teamC.domain.chat.entity.ChatRoom; | ||
import com.umc.teamC.domain.chat.entity.ChatRoomUser; | ||
import com.umc.teamC.domain.user.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ChatRoomUserRepository extends JpaRepository<ChatRoomUser, Long> { | ||
Integer countByChatRoom(ChatRoom chatRoom); | ||
|
||
List<ChatRoom> findAllChatRoomByUser(User user); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/umc/teamC/domain/chat/service/RestChatService.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,39 @@ | ||
package com.umc.teamC.domain.chat.service; | ||
|
||
import com.umc.teamC.domain.chat.converter.ChatRoomConverter; | ||
import com.umc.teamC.domain.chat.dto.ChatRoomDto; | ||
import com.umc.teamC.domain.chat.entity.Chat; | ||
import com.umc.teamC.domain.chat.entity.ChatRoom; | ||
import com.umc.teamC.domain.chat.repository.ChatRepository; | ||
import com.umc.teamC.domain.chat.repository.ChatRoomRepository; | ||
import com.umc.teamC.domain.chat.repository.ChatRoomUserRepository; | ||
import com.umc.teamC.domain.user.entity.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class RestChatService { | ||
|
||
private final ChatRoomUserRepository chatRoomUserRepository; | ||
private final ChatRepository chatRepository; | ||
private final ChatRoomRepository chatRoomRepository; | ||
|
||
// 채팅방 목록 조회 - 채팅방 지역, 시간, 마지막 메세지 전달 | ||
public List<ChatRoomDto.ChatRoomResponseDto> getChatRoomList(User user) { | ||
List<ChatRoom> chatRoomList = chatRoomUserRepository.findAllChatRoomByUser(user); | ||
List<ChatRoomDto.ChatRoomResponseDto> chatRoomResponseDtoList = new ArrayList<>(); | ||
for (ChatRoom chatRoom : chatRoomList) { | ||
Chat chat = chatRepository.findFirstByChatRoomOrderByCreatedAtDesc(chatRoom); | ||
Integer count = chatRoomUserRepository.countByChatRoom(chatRoom); | ||
ChatRoomDto.ChatRoomResponseDto chatRoomResponseDto = ChatRoomConverter.toChatRoomResponseDto(chatRoom, chat.getContent(), count); | ||
chatRoomResponseDtoList.add(chatRoomResponseDto); | ||
} | ||
return chatRoomResponseDtoList; | ||
} | ||
} |
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
1 change: 0 additions & 1 deletion
1
src/main/java/com/umc/teamC/domain/user/service/UserService.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
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