Skip to content
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
@@ -0,0 +1,54 @@
package com.example.RealMatch.chat.application.service;

import org.springframework.web.multipart.MultipartFile;

import com.example.RealMatch.chat.presentation.conversion.MessageCursor;
import com.example.RealMatch.chat.presentation.conversion.RoomCursor;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomFilterStatus;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomSort;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomTab;
import com.example.RealMatch.chat.presentation.dto.enums.ChatSystemMessageKind;
import com.example.RealMatch.chat.presentation.dto.request.ChatAttachmentUploadRequest;
import com.example.RealMatch.chat.presentation.dto.request.ChatRoomCreateRequest;
import com.example.RealMatch.chat.presentation.dto.response.ChatAttachmentUploadResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatMessageListResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatMessageResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomCreateResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomDetailResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomListResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatSystemMessagePayload;
import com.example.RealMatch.global.config.jwt.CustomUserDetails;

public interface ChatService {
ChatRoomCreateResponse createOrGetRoom(CustomUserDetails user, ChatRoomCreateRequest request);

ChatRoomListResponse getRoomList(
CustomUserDetails user,
ChatRoomTab tab,
ChatRoomFilterStatus filterStatus,
ChatRoomSort sort,
RoomCursor roomCursor,
int size
);

ChatRoomDetailResponse getRoomDetail(CustomUserDetails user, Long roomId);

ChatMessageListResponse getMessages(
CustomUserDetails user,
Long roomId,
MessageCursor messageCursor,
int size
);

ChatAttachmentUploadResponse uploadAttachment(
CustomUserDetails user,
ChatAttachmentUploadRequest request,
MultipartFile file
);

ChatMessageResponse createSystemMessage(
Long roomId,
ChatSystemMessageKind kind,
ChatSystemMessagePayload payload
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.RealMatch.chat.application.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.example.RealMatch.chat.presentation.controller.fixture.ChatFixtureFactory;
import com.example.RealMatch.chat.presentation.conversion.MessageCursor;
import com.example.RealMatch.chat.presentation.conversion.RoomCursor;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomFilterStatus;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomSort;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomTab;
import com.example.RealMatch.chat.presentation.dto.enums.ChatSystemMessageKind;
import com.example.RealMatch.chat.presentation.dto.request.ChatAttachmentUploadRequest;
import com.example.RealMatch.chat.presentation.dto.request.ChatRoomCreateRequest;
import com.example.RealMatch.chat.presentation.dto.response.ChatAttachmentUploadResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatMessageListResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatMessageResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomCreateResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomDetailResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatRoomListResponse;
import com.example.RealMatch.chat.presentation.dto.response.ChatSystemMessagePayload;
import com.example.RealMatch.global.config.jwt.CustomUserDetails;

@Service
public class ChatServiceImpl implements ChatService {

@Override
public ChatRoomCreateResponse createOrGetRoom(CustomUserDetails user, ChatRoomCreateRequest request) {
return ChatFixtureFactory.sampleRoomCreateResponse();
}

@Override
public ChatRoomListResponse getRoomList(
CustomUserDetails user,
ChatRoomTab tab,
ChatRoomFilterStatus filterStatus,
ChatRoomSort sort,
RoomCursor roomCursor,
int size
) {
return ChatFixtureFactory.sampleRoomListResponse();
}

@Override
public ChatRoomDetailResponse getRoomDetail(CustomUserDetails user, Long roomId) {
return ChatFixtureFactory.sampleRoomDetailResponse(roomId);
}

@Override
public ChatMessageListResponse getMessages(
CustomUserDetails user,
Long roomId,
MessageCursor messageCursor,
int size
) {
return ChatFixtureFactory.sampleMessageListResponse(roomId);
}

@Override
public ChatAttachmentUploadResponse uploadAttachment(
CustomUserDetails user,
ChatAttachmentUploadRequest request,
MultipartFile file
) {
return ChatFixtureFactory.sampleAttachmentUploadResponse(request, file);
}

@Override
public ChatMessageResponse createSystemMessage(
Long roomId,
ChatSystemMessageKind kind,
ChatSystemMessagePayload payload
) {
return ChatFixtureFactory.sampleSystemMessageResponse(roomId, kind, payload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.RealMatch.chat.application.service;

import org.springframework.lang.NonNull;

import com.example.RealMatch.chat.presentation.dto.enums.ChatSystemMessageKind;
import com.example.RealMatch.chat.presentation.dto.response.ChatSystemMessagePayload;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatMessageCreatedEvent;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageAck;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageCommand;

public interface ChatSocketService {
@NonNull
ChatMessageCreatedEvent createMessageEvent(ChatSendMessageCommand command);

ChatSendMessageAck createAck(ChatSendMessageCommand command, Long messageId);

ChatMessageCreatedEvent createSystemMessageEvent(
Long roomId,
ChatSystemMessageKind kind,
ChatSystemMessagePayload payload
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.RealMatch.chat.application.service;

import java.util.Objects;

import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import com.example.RealMatch.chat.presentation.controller.fixture.ChatSocketFixtureFactory;
import com.example.RealMatch.chat.presentation.dto.enums.ChatSystemMessageKind;
import com.example.RealMatch.chat.presentation.dto.response.ChatSystemMessagePayload;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatMessageCreatedEvent;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageAck;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageCommand;

@Service
public class ChatSocketServiceImpl implements ChatSocketService {

@Override
@NonNull
public ChatMessageCreatedEvent createMessageEvent(ChatSendMessageCommand command) {
return Objects.requireNonNull(
ChatSocketFixtureFactory.sampleMessageCreatedEvent(command),
"ChatMessageCreatedEvent must not be null."
);
}

@Override
public ChatSendMessageAck createAck(ChatSendMessageCommand command, Long messageId) {
return ChatSocketFixtureFactory.sampleAck(command, messageId);
}

@Override
public ChatMessageCreatedEvent createSystemMessageEvent(
Long roomId,
ChatSystemMessageKind kind,
ChatSystemMessagePayload payload
) {
throw new UnsupportedOperationException("Not implemented yet.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.RealMatch.chat.presentation.controller.fixture.ChatFixtureFactory;
import com.example.RealMatch.chat.application.service.ChatService;
import com.example.RealMatch.chat.presentation.conversion.MessageCursor;
import com.example.RealMatch.chat.presentation.conversion.RoomCursor;
import com.example.RealMatch.chat.presentation.dto.enums.ChatRoomFilterStatus;
Expand All @@ -34,12 +34,18 @@
@RequestMapping("/api/chat")
public class ChatController implements ChatSwagger {

private final ChatService chatService;

public ChatController(ChatService chatService) {
this.chatService = chatService;
}

@PostMapping("/rooms")
public CustomResponse<ChatRoomCreateResponse> createOrGetRoom(
@AuthenticationPrincipal CustomUserDetails user,
@Valid @RequestBody ChatRoomCreateRequest request
) {
return CustomResponse.ok(ChatFixtureFactory.sampleRoomCreateResponse());
return CustomResponse.ok(chatService.createOrGetRoom(user, request));
}

@GetMapping("/rooms")
Expand All @@ -51,15 +57,15 @@ public CustomResponse<ChatRoomListResponse> getRoomList(
@RequestParam(name = "cursor", required = false) RoomCursor roomCursor,
@RequestParam(defaultValue = "20") int size
) {
return CustomResponse.ok(ChatFixtureFactory.sampleRoomListResponse());
return CustomResponse.ok(chatService.getRoomList(user, tab, filterStatus, sort, roomCursor, size));
}

@GetMapping("/rooms/{roomId}")
public CustomResponse<ChatRoomDetailResponse> getRoomDetail(
@AuthenticationPrincipal CustomUserDetails user,
@PathVariable Long roomId
) {
return CustomResponse.ok(ChatFixtureFactory.sampleRoomDetailResponse(roomId));
return CustomResponse.ok(chatService.getRoomDetail(user, roomId));
}

@GetMapping("/rooms/{roomId}/messages")
Expand All @@ -69,7 +75,7 @@ public CustomResponse<ChatMessageListResponse> getMessages(
@RequestParam(name = "cursor", required = false) MessageCursor messageCursor,
@RequestParam(defaultValue = "20") int size
) {
return CustomResponse.ok(ChatFixtureFactory.sampleMessageListResponse(roomId));
return CustomResponse.ok(chatService.getMessages(user, roomId, messageCursor, size));
}

@PostMapping("/attachments")
Expand All @@ -78,6 +84,6 @@ public CustomResponse<ChatAttachmentUploadResponse> uploadAttachment(
@Valid @RequestPart("request") ChatAttachmentUploadRequest request,
@RequestPart("file") MultipartFile file
) {
return CustomResponse.ok(ChatFixtureFactory.sampleAttachmentUploadResponse(request, file));
return CustomResponse.ok(chatService.uploadAttachment(user, request, file));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.example.RealMatch.chat.presentation.controller;

import java.util.Objects;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import com.example.RealMatch.chat.presentation.controller.fixture.ChatSocketFixtureFactory;
import com.example.RealMatch.chat.application.service.ChatSocketService;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatMessageCreatedEvent;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageAck;
import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageCommand;
Expand All @@ -19,16 +17,18 @@
public class ChatSocketController {

private final SimpMessagingTemplate messagingTemplate;
private final ChatSocketService chatSocketService;

public ChatSocketController(SimpMessagingTemplate messagingTemplate) {
public ChatSocketController(SimpMessagingTemplate messagingTemplate, ChatSocketService chatSocketService) {
this.messagingTemplate = messagingTemplate;
this.chatSocketService = chatSocketService;
}

@MessageMapping("/chat.send")
@SendToUser("/queue/chat.ack")
public ChatSendMessageAck sendMessage(@Valid @Payload ChatSendMessageCommand command) {
ChatMessageCreatedEvent event = ChatSocketFixtureFactory.sampleMessageCreatedEvent(command);
messagingTemplate.convertAndSend("/topic/rooms/" + command.roomId(), Objects.requireNonNull(event));
return ChatSocketFixtureFactory.sampleAck(command, event.message().messageId());
ChatMessageCreatedEvent event = chatSocketService.createMessageEvent(command);
messagingTemplate.convertAndSend("/topic/rooms/" + command.roomId(), event);
return chatSocketService.createAck(command, event.message().messageId());
}
}
Loading