|
1 | 1 | package org.jungppo.bambooforest.chat.dto;
|
2 | 2 |
|
| 3 | +import org.jungppo.bambooforest.chat.exception.ChatMessageValidationException; |
| 4 | + |
3 | 5 | import com.fasterxml.jackson.annotation.JsonCreator;
|
4 | 6 | import com.fasterxml.jackson.annotation.JsonProperty;
|
5 | 7 |
|
|
13 | 15 | @AllArgsConstructor(access = AccessLevel.PRIVATE)
|
14 | 16 | public class ChatMessageDto {
|
15 | 17 | @NotNull(message = "Message type cannot be null")
|
16 |
| - private MessageType type; |
| 18 | + private ChatMessageType type; |
17 | 19 |
|
18 | 20 | @NotBlank(message = "Message cannot be blank")
|
19 | 21 | private String message;
|
20 | 22 |
|
21 |
| - public enum MessageType { |
22 |
| - ENTER, TALK, LEAVE //입장, 채팅, 퇴장 |
| 23 | + private String token; // 인증 토큰 |
| 24 | + |
| 25 | + private String roomId; // 룸 ID |
| 26 | + |
| 27 | + private Long memberId; // 멤버 ID |
| 28 | + |
| 29 | + public enum ChatMessageType { |
| 30 | + AUTH, ENTER, TALK, LEAVE //인증, 입장, 채팅, 퇴장 |
23 | 31 | }
|
24 | 32 |
|
25 | 33 | @JsonCreator
|
26 | 34 | public static ChatMessageDto of(
|
27 |
| - @JsonProperty("type") MessageType type, |
28 |
| - @JsonProperty("message") String message |
| 35 | + @JsonProperty("type") ChatMessageType type, |
| 36 | + @JsonProperty("message") String message, |
| 37 | + @JsonProperty("token") String token, |
| 38 | + @JsonProperty("roomId") String roomId, |
| 39 | + @JsonProperty("memberId") Long memberId |
29 | 40 | ) {
|
30 |
| - return new ChatMessageDto(type, message); |
| 41 | + ChatMessageDto dto = new ChatMessageDto(type, message, token, roomId, memberId); |
| 42 | + dto.validate(); |
| 43 | + return dto; |
| 44 | + } |
| 45 | + |
| 46 | + private void validate() { |
| 47 | + if (this.type == ChatMessageType.AUTH) { |
| 48 | + validateField(this.token); |
| 49 | + validateField(this.roomId); |
| 50 | + validateField(this.memberId); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void validateField(String field) { |
| 55 | + if (field == null || field.isBlank()) throw new ChatMessageValidationException(); |
| 56 | + } |
| 57 | + |
| 58 | + private void validateField(Long field) { |
| 59 | + if (field == null) throw new ChatMessageValidationException(); |
31 | 60 | }
|
32 | 61 | }
|
0 commit comments