Skip to content

Commit

Permalink
Merge pull request #246 from team9502/dev
Browse files Browse the repository at this point in the history
배포
  • Loading branch information
daeundada authored Jun 29, 2024
2 parents 1be6344 + 1ea4b2a commit a175948
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 166 deletions.
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.

0 comments on commit a175948

Please sign in to comment.