Skip to content

Commit

Permalink
Merge pull request #10 from UMC6th-TeamC/chat
Browse files Browse the repository at this point in the history
Merge Chat
  • Loading branch information
chamm99 authored Jun 22, 2024
2 parents 54ed560 + 12ef962 commit 47b3989
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Map<String, Object> sendMessage(Map<String, Object> message) {
.build();
Chat chat = chatService.saveMessage(message1);
Map<String, Object> result = new HashMap<>();
result.put("id", chat.getId());
result.put("id", chat.getChatId());
result.put("sender", chat.getSender());
result.put("content", chat.getContent());
return result;
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
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;
Expand Down
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 src/main/java/com/umc/teamC/domain/chat/dto/ChatRoomDto.java
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
) {
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/umc/teamC/domain/chat/entity/Chat.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.umc.teamC.domain.chat.entity;

import com.umc.teamC.global.common.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.*;

@Entity
Expand All @@ -16,8 +13,12 @@ public class Chat extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long chatId;

private String content;
private String sender;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chat_room_id")
private ChatRoom chatRoom;
}
30 changes: 30 additions & 0 deletions src/main/java/com/umc/teamC/domain/chat/entity/ChatRoom.java
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 src/main/java/com/umc/teamC/domain/chat/entity/ChatRoomUser.java
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;
}
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);
}
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> {
}
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);
}
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;
}
}
10 changes: 7 additions & 3 deletions src/main/java/com/umc/teamC/domain/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.umc.teamC.domain.user.entity;

import com.umc.teamC.domain.chat.entity.ChatRoomUser;
import com.umc.teamC.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Entity
@Setter
@Getter
public class User extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;

@Column(nullable = false)
private String username;
Expand All @@ -22,10 +26,10 @@ public class User extends BaseEntity {

private String role;

@OneToMany(mappedBy = "user")
private List<ChatRoomUser> chatRoomUserList = new ArrayList<>();

public void update(String nickname) {
this.nickname = nickname;
}
}


Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.umc.teamC.domain.user.service;


import com.umc.teamC.domain.user.dto.UpdateUserDTO;
import com.umc.teamC.domain.user.entity.User;
import com.umc.teamC.domain.user.repository.UserRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer{
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
Expand All @@ -17,6 +18,6 @@ public void configureMessageBroker(MessageBrokerRegistry config) {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("http://localhost:3000", "http://localhost:3004").withSockJS();
registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}
}

0 comments on commit 47b3989

Please sign in to comment.