Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

배포 #246

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import team9502.sinchulgwinong.domain.chat.dto.request.ChatRequestDTO;
Expand All @@ -18,13 +20,12 @@
import static team9502.sinchulgwinong.global.response.SuccessCode.*;

@RestController
@RequestMapping("/chats")
@RequiredArgsConstructor
public class ChatController {

private final ChatService chatService;

@PostMapping("/cp-user/{cpUserId}")
@PostMapping("/chats/cp-user/{cpUserId}")
public ResponseEntity<GlobalApiResponse<ChatRoomResponseDTO>> createChatRoom(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable("cpUserId") Long cpUserId) {
Expand All @@ -42,7 +43,7 @@ public ResponseEntity<GlobalApiResponse<ChatRoomResponseDTO>> createChatRoom(
);
}

@GetMapping("/chat-rooms")
@GetMapping("/chats/chat-rooms")
public ResponseEntity<GlobalApiResponse<List<ChatRoomResponseDTO>>> getChatRooms(
@AuthenticationPrincipal UserDetailsImpl userDetails) {

Expand All @@ -56,25 +57,17 @@ public ResponseEntity<GlobalApiResponse<List<ChatRoomResponseDTO>>> getChatRooms
);
}

@PostMapping("/chat-room/{chatRoomId}")
public ResponseEntity<GlobalApiResponse<ChatMessageResponseDTO>> sendMessage(
@MessageMapping("/chats/chat-room/{chatRoomId}")
@SendTo("/topic/chatroom/{chatRoomId}")
public ChatMessageResponseDTO saveAndSendMessage(
@RequestBody @Valid ChatRequestDTO chatRequestDTO,
@PathVariable(name = "chatRoomId") Long chatRoomId,
@AuthenticationPrincipal UserDetailsImpl userDetails) {

ChatMessageResponseDTO chatMessageResponseDTO =
chatService.saveAndPublishMessage(userDetails, chatRequestDTO, chatRoomId);

return ResponseEntity.status(SUCCESS_SEND_AND_SAVE_CHAT.getHttpStatus())
.body(
GlobalApiResponse.of(
SUCCESS_SEND_AND_SAVE_CHAT.getMessage(),
chatMessageResponseDTO
)
);
return chatService.saveAndSendMessage(userDetails, chatRequestDTO, chatRoomId);
}

@GetMapping("/chat-room/{chatRoomId}")
@GetMapping("/chats/chat-room/{chatRoomId}")
public ResponseEntity<GlobalApiResponse<List<ChatMessageResponseDTO>>> getChatMessages(
@PathVariable(name = "chatRoomId") Long chatRoomId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public ChatMessageResponseDTO(ChatMessage chatMessage) {
this.message = chatMessage.getMessage();
this.createAt = chatMessage.getCreatedAt();

if(chatMessage.getCompanyUser() != null){
if (chatMessage.getCompanyUser() != null) {
this.cpUserId = chatMessage.getCompanyUser().getCpUserId();
}
else{
} else {
this.cpUserId = null;
}

if (chatMessage.getUser() != null){
if (chatMessage.getUser() != null) {
this.userId = chatMessage.getUser().getUserId();
}
else{
} else {
this.userId = null;
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import lombok.RequiredArgsConstructor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team9502.sinchulgwinong.domain.chat.dto.request.ChatRequestDTO;
Expand All @@ -21,14 +22,15 @@
import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class ChatService {

private final ChatMessageRepository chatMessageRepository;
private final CompanyUserRepository companyUserRepository;
private final ChatRoomRepository chatRoomRepository;
private final MessagePublisher messagePublisher;
private final SimpMessagingTemplate messagingTemplate;

@Transactional
public ChatRoomResponseDTO createChatRoom(User user, Long cpUserId) {
Expand All @@ -44,6 +46,8 @@ public ChatRoomResponseDTO createChatRoom(User user, Long cpUserId) {

chatRoomRepository.save(chatRoom);

notifyChatRoomUpdate();

return new ChatRoomResponseDTO(chatRoom);
}

Expand All @@ -62,12 +66,17 @@ public List<ChatRoomResponseDTO> getChatRooms(UserDetailsImpl userDetails) {
.collect(Collectors.toList());
}

@Transactional
public ChatMessageResponseDTO saveAndPublishMessage(
UserDetailsImpl userDetails,
ChatRequestDTO chatRequestDTO,
Long chatRoomId) {
public void notifyChatRoomUpdate() {
List<ChatRoomResponseDTO> chatRooms = chatRoomRepository.findAll()
.stream()
.map(ChatRoomResponseDTO::new)
.collect(Collectors.toList());

messagingTemplate.convertAndSend("/topic/chatrooms", chatRooms);
}

@Transactional
public ChatMessageResponseDTO saveAndSendMessage(UserDetailsImpl userDetails, ChatRequestDTO chatRequestDTO, Long chatRoomId) {
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new ApiException(ErrorCode.CHAT_NOT_FOUND));

Expand All @@ -76,38 +85,36 @@ public ChatMessageResponseDTO saveAndPublishMessage(
chatMessage.setChatRoom(chatRoom);

if (userDetails.getUser() != null) {

User user = (User) userDetails.getUser();
chatMessage.setUser(user);
chatMessage.setCompanyUser(null);

} else if (userDetails.getCpUserId() != null) {

CompanyUser companyUser = companyUserRepository.findById(userDetails.getCpUserId())
.orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND));

chatMessage.setCompanyUser(companyUser);
chatMessage.setUser(null);
}

chatMessageRepository.save(chatMessage);
messagePublisher.publish(chatMessage);

chatRoom.setChatCheck(true);
chatRoomRepository.save(chatRoom);

messagingTemplate.convertAndSend("/topic/chatroom/" + chatRoomId, new ChatMessageResponseDTO(chatMessage));

notifyChatRoomUpdate();

return new ChatMessageResponseDTO(chatMessage);
}

@Transactional
public List<ChatMessageResponseDTO> getChatMessages(Long chatRoomId) {

ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
.orElseThrow(()-> new ApiException(ErrorCode.CHAT_NOT_FOUND));
.orElseThrow(() -> new ApiException(ErrorCode.CHAT_NOT_FOUND));

List<ChatMessage> messages = chatMessageRepository.findByChatRoom_ChatRoomId(chatRoomId);

if(chatRoom.isChatCheck()){
if (chatRoom.isChatCheck()) {
chatRoom.setChatCheck(false);
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.