From 4fe9169d20e556252c025937f289110165b9a389 Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 15:57:38 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat=20:=20MongoDB=20=EC=97=B0=EA=B2=B0=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index d535fd7..d038542 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ repositories { } dependencies { -// implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' implementation group: 'org.webjars', name: 'stomp-websocket', version: '2.3.3-1' From 3641fb58d657744db0643416002c0a10de7894de Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 15:58:44 +0900 Subject: [PATCH 2/6] feat : MongoDB connection info --- src/main/resources/application.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 11f75f0..8c1d380 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,9 @@ spring: application: name: festime_BE - + data: + mongodb: + uri: ${MONGO_HOST} rabbit: host: ${RABBIT_HOST} From c1fc461944df21eb37825f408f6b7c9012282cd4 Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 15:59:01 +0900 Subject: [PATCH 3/6] Add : message Repository --- .../java/Be9room/festime/repository/MessageRepository.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/Be9room/festime/repository/MessageRepository.java diff --git a/src/main/java/Be9room/festime/repository/MessageRepository.java b/src/main/java/Be9room/festime/repository/MessageRepository.java new file mode 100644 index 0000000..dbb0120 --- /dev/null +++ b/src/main/java/Be9room/festime/repository/MessageRepository.java @@ -0,0 +1,7 @@ +package Be9room.festime.repository; + +import Be9room.festime.domain.Message; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface MessageRepository extends MongoRepository { +} From 7be515db843e0a492e69df410f48314517b5bd19 Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 15:59:14 +0900 Subject: [PATCH 4/6] feat : MessageService --- .../festime/service/MessageService.java | 11 ++++++ .../festime/service/MessageServiceImpl.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/Be9room/festime/service/MessageService.java create mode 100644 src/main/java/Be9room/festime/service/MessageServiceImpl.java diff --git a/src/main/java/Be9room/festime/service/MessageService.java b/src/main/java/Be9room/festime/service/MessageService.java new file mode 100644 index 0000000..e28d8bd --- /dev/null +++ b/src/main/java/Be9room/festime/service/MessageService.java @@ -0,0 +1,11 @@ +package Be9room.festime.service; + +import Be9room.festime.domain.Message; +import Be9room.festime.dto.MessageDto; + +import java.util.List; + +public interface MessageService { + Message save(MessageDto messageDto); + List getMessages(); +} \ No newline at end of file diff --git a/src/main/java/Be9room/festime/service/MessageServiceImpl.java b/src/main/java/Be9room/festime/service/MessageServiceImpl.java new file mode 100644 index 0000000..0bccd88 --- /dev/null +++ b/src/main/java/Be9room/festime/service/MessageServiceImpl.java @@ -0,0 +1,34 @@ +package Be9room.festime.service; + +import Be9room.festime.domain.Message; +import Be9room.festime.dto.MessageDto; +import Be9room.festime.repository.MessageRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class MessageServiceImpl implements MessageService { + private final MessageRepository messageRepository; + + @Override + public Message save(MessageDto messageDto) { + Message message = Message.builder() + .memberId(messageDto.getMemberId()) + .memberName(messageDto.getMemberName()) + .message(messageDto.getMessage()) + .createdAt(LocalDateTime.now()) + .build(); + + return messageRepository.save(message); + } + + @Override + public List getMessages() { + return messageRepository.findAll(); + } +} From 138fa92d9a8fb713f3978bd7c204c2f950741716 Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 15:59:27 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../festime/controller/MemberController.java | 2 -- .../festime/controller/MessageController.java | 10 +++++--- .../java/Be9room/festime/domain/Message.java | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Be9room/festime/domain/Message.java diff --git a/src/main/java/Be9room/festime/controller/MemberController.java b/src/main/java/Be9room/festime/controller/MemberController.java index 3ebb56a..e422d14 100644 --- a/src/main/java/Be9room/festime/controller/MemberController.java +++ b/src/main/java/Be9room/festime/controller/MemberController.java @@ -26,6 +26,4 @@ public ApiResponse enter(){ return ApiResponse.of(SuccessStatus.MEMBER_ENTER, memberDto); } - - } diff --git a/src/main/java/Be9room/festime/controller/MessageController.java b/src/main/java/Be9room/festime/controller/MessageController.java index 91cbf12..5aa5186 100644 --- a/src/main/java/Be9room/festime/controller/MessageController.java +++ b/src/main/java/Be9room/festime/controller/MessageController.java @@ -2,6 +2,7 @@ import Be9room.festime.dto.MessageDto; import Be9room.festime.enums.MessageType; +import Be9room.festime.service.MessageService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.handler.annotation.MessageMapping; @@ -13,17 +14,19 @@ @Slf4j public class MessageController { private final SimpMessagingTemplate template; + private final MessageService messageService; /** * 채팅 입장 * /pub/chat/enter 로 보내면 여기로 온다. * @param message */ @MessageMapping(value = "/guestbook/enter") + @Deprecated public void enter(MessageDto message){ message.setMessageType(MessageType.ENTER.toString()); message.setMessage(message.getMemberName() + "님이 방명록에 참여했어요!"); - /* 메세지 저장 부분 */ + messageService.save(message); template.convertAndSend("/topic/guestbook", message); } @@ -36,7 +39,7 @@ public void enter(MessageDto message){ public void message(MessageDto message){ message.setMessageType(MessageType.MESSAGE.toString()); - /* 메세지 저장 부분 */ + messageService.save(message); template.convertAndSend("/topic/guestbook", message); } @@ -46,11 +49,12 @@ public void message(MessageDto message){ * @param message */ @MessageMapping(value = "/guestbook/leave") + @Deprecated public void leave(MessageDto message){ message.setMessageType(MessageType.LEAVE.toString()); message.setMessage(message.getMemberName() + "님이 방명록에서 나가셨어요!"); - /* 메세지 저장 부분 */ + messageService.save(message); template.convertAndSend("/topic/guestbook", message); } diff --git a/src/main/java/Be9room/festime/domain/Message.java b/src/main/java/Be9room/festime/domain/Message.java new file mode 100644 index 0000000..0742d79 --- /dev/null +++ b/src/main/java/Be9room/festime/domain/Message.java @@ -0,0 +1,24 @@ +package Be9room.festime.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.time.LocalDateTime; + +@Getter +@Document(collection = "messages") +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class Message { + @Id + private String id; + private String memberName; + private String memberId; + private String message; + private LocalDateTime createdAt; +} From 5ae8aabc9720bbde8cbcc5a71d400e11c8b197d6 Mon Sep 17 00:00:00 2001 From: thsghdud13 Date: Thu, 26 Sep 2024 22:18:54 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat=20:=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20API?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayLoad/code/status/ErrorStatus.java | 3 +- .../apiPayLoad/code/status/SuccessStatus.java | 5 ++- .../festime/controller/MessageController.java | 25 +++++++++++--- .../festime/converter/MessageConverter.java | 34 +++++++++++++++++++ .../{MessageDto.java => MessageChatDto.java} | 2 +- .../Be9room/festime/dto/MessageWebDto.java | 34 +++++++++++++++++++ .../festime/repository/MessageRepository.java | 5 +++ .../festime/service/MessageService.java | 9 +++-- .../festime/service/MessageServiceImpl.java | 18 +++++----- .../validation/annotation/CheckPage.java | 17 ++++++++++ .../validator/CheckPageValidator.java | 30 ++++++++++++++++ 11 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 src/main/java/Be9room/festime/converter/MessageConverter.java rename src/main/java/Be9room/festime/dto/{MessageDto.java => MessageChatDto.java} (88%) create mode 100644 src/main/java/Be9room/festime/dto/MessageWebDto.java create mode 100644 src/main/java/Be9room/festime/validation/annotation/CheckPage.java create mode 100644 src/main/java/Be9room/festime/validation/validator/CheckPageValidator.java diff --git a/src/main/java/Be9room/festime/apiPayLoad/code/status/ErrorStatus.java b/src/main/java/Be9room/festime/apiPayLoad/code/status/ErrorStatus.java index ecc9952..a01140e 100644 --- a/src/main/java/Be9room/festime/apiPayLoad/code/status/ErrorStatus.java +++ b/src/main/java/Be9room/festime/apiPayLoad/code/status/ErrorStatus.java @@ -15,7 +15,8 @@ public enum ErrorStatus implements BaseErrorCode { _UNAUTHORIZED(HttpStatus.UNAUTHORIZED,"COMMON401","인증이 필요합니다."), _FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "금지된 요청입니다."), - // 멤버 관려 예외 + //페이지 관련 에러 + _PAGE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "PAGE2001", "page 번호는 0 이상이여야 합니다") ; private final HttpStatus httpStatus; private final String code; diff --git a/src/main/java/Be9room/festime/apiPayLoad/code/status/SuccessStatus.java b/src/main/java/Be9room/festime/apiPayLoad/code/status/SuccessStatus.java index 79db43b..2ef0efd 100644 --- a/src/main/java/Be9room/festime/apiPayLoad/code/status/SuccessStatus.java +++ b/src/main/java/Be9room/festime/apiPayLoad/code/status/SuccessStatus.java @@ -13,7 +13,10 @@ public enum SuccessStatus implements BaseCode { _OK(HttpStatus.OK, "COMMON200","success"), //멤버 관련 응답 - MEMBER_ENTER(HttpStatus.OK, "MEMBER2001", "member entered") + MEMBER_ENTER(HttpStatus.OK, "MEMBER2001", "member entered"), + + //방명록 관련 응답 + MESSAGE_RETURN(HttpStatus.OK,"GUESTBOOK2001", "message list returned") ; private final HttpStatus httpStatus; diff --git a/src/main/java/Be9room/festime/controller/MessageController.java b/src/main/java/Be9room/festime/controller/MessageController.java index 5aa5186..704992b 100644 --- a/src/main/java/Be9room/festime/controller/MessageController.java +++ b/src/main/java/Be9room/festime/controller/MessageController.java @@ -1,12 +1,21 @@ package Be9room.festime.controller; -import Be9room.festime.dto.MessageDto; +import Be9room.festime.apiPayLoad.ApiResponse; +import Be9room.festime.apiPayLoad.code.status.SuccessStatus; +import Be9room.festime.converter.MessageConverter; +import Be9room.festime.domain.Message; +import Be9room.festime.dto.MessageChatDto; +import Be9room.festime.dto.MessageWebDto; import Be9room.festime.enums.MessageType; import Be9room.festime.service.MessageService; +import Be9room.festime.validation.annotation.CheckPage; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -22,7 +31,7 @@ public class MessageController { */ @MessageMapping(value = "/guestbook/enter") @Deprecated - public void enter(MessageDto message){ + public void enter(MessageChatDto message){ message.setMessageType(MessageType.ENTER.toString()); message.setMessage(message.getMemberName() + "님이 방명록에 참여했어요!"); @@ -36,7 +45,7 @@ public void enter(MessageDto message){ * @param message */ @MessageMapping(value = "/guestbook/message") - public void message(MessageDto message){ + public void message(MessageChatDto message){ message.setMessageType(MessageType.MESSAGE.toString()); messageService.save(message); @@ -50,7 +59,7 @@ public void message(MessageDto message){ */ @MessageMapping(value = "/guestbook/leave") @Deprecated - public void leave(MessageDto message){ + public void leave(MessageChatDto message){ message.setMessageType(MessageType.LEAVE.toString()); message.setMessage(message.getMemberName() + "님이 방명록에서 나가셨어요!"); @@ -59,5 +68,11 @@ public void leave(MessageDto message){ template.convertAndSend("/topic/guestbook", message); } + @GetMapping(value = "/message") + public ApiResponse getMessageList(@CheckPage @RequestParam(name = "page") Integer page){ + Page messagePage = messageService.getMessages(page); + MessageWebDto.MessageResponseDtoList messageResponseDtoList = MessageConverter.toMessageResponseDtoList(messagePage); + return ApiResponse.of(SuccessStatus.MESSAGE_RETURN, messageResponseDtoList); + } -} +} \ No newline at end of file diff --git a/src/main/java/Be9room/festime/converter/MessageConverter.java b/src/main/java/Be9room/festime/converter/MessageConverter.java new file mode 100644 index 0000000..6cd797a --- /dev/null +++ b/src/main/java/Be9room/festime/converter/MessageConverter.java @@ -0,0 +1,34 @@ +package Be9room.festime.converter; + +import Be9room.festime.domain.Message; +import Be9room.festime.dto.MessageChatDto; +import Be9room.festime.dto.MessageWebDto; +import org.springframework.data.domain.Page; + +import java.util.List; +import java.util.stream.Collectors; + +public class MessageConverter { + public static MessageWebDto.MessageResponseDto toMessageResponseDto(Message message){ + return MessageWebDto.MessageResponseDto.builder() + .memberName(message.getMemberName()) + .memberId(message.getMemberId()) + .message(message.getMessage()) + .createdAt(message.getCreatedAt()) + .build(); + } + public static MessageWebDto.MessageResponseDtoList toMessageResponseDtoList(Page messagePage){ + List messageList = messagePage.stream() + .map(MessageConverter::toMessageResponseDto) + .toList(); + + return MessageWebDto.MessageResponseDtoList.builder() + .messageDtoList(messageList) + .isLast(messagePage.isLast()) + .isFirst(messagePage.isFirst()) + .totalPage(messagePage.getTotalPages()) + .totalElements(messagePage.getTotalElements()) + .listSize(messageList.size()) + .build(); + } +} diff --git a/src/main/java/Be9room/festime/dto/MessageDto.java b/src/main/java/Be9room/festime/dto/MessageChatDto.java similarity index 88% rename from src/main/java/Be9room/festime/dto/MessageDto.java rename to src/main/java/Be9room/festime/dto/MessageChatDto.java index 3f32230..a753389 100644 --- a/src/main/java/Be9room/festime/dto/MessageDto.java +++ b/src/main/java/Be9room/festime/dto/MessageChatDto.java @@ -7,7 +7,7 @@ @AllArgsConstructor @NoArgsConstructor @ToString -public class MessageDto { +public class MessageChatDto { private String memberName; private String memberId; private String messageType; diff --git a/src/main/java/Be9room/festime/dto/MessageWebDto.java b/src/main/java/Be9room/festime/dto/MessageWebDto.java new file mode 100644 index 0000000..6e83c84 --- /dev/null +++ b/src/main/java/Be9room/festime/dto/MessageWebDto.java @@ -0,0 +1,34 @@ +package Be9room.festime.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.util.List; + +public class MessageWebDto { + @Builder + @NoArgsConstructor + @AllArgsConstructor + @Getter + public static class MessageResponseDtoList{ + List messageDtoList; + Boolean isFirst; + Boolean isLast; + Long totalElements; + Integer listSize; + Integer totalPage; + } + @Builder + @NoArgsConstructor + @AllArgsConstructor + @Getter + public static class MessageResponseDto{ + private String memberName; + private String memberId; + private String message; + private LocalDateTime createdAt; + } +} \ No newline at end of file diff --git a/src/main/java/Be9room/festime/repository/MessageRepository.java b/src/main/java/Be9room/festime/repository/MessageRepository.java index dbb0120..7f5c42d 100644 --- a/src/main/java/Be9room/festime/repository/MessageRepository.java +++ b/src/main/java/Be9room/festime/repository/MessageRepository.java @@ -1,7 +1,12 @@ package Be9room.festime.repository; import Be9room.festime.domain.Message; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.mongodb.repository.MongoRepository; +import java.time.LocalDateTime; + public interface MessageRepository extends MongoRepository { + Page findMessagesByCreatedAtAfterOrderByCreatedAtDesc(LocalDateTime time, PageRequest pageRequest); } diff --git a/src/main/java/Be9room/festime/service/MessageService.java b/src/main/java/Be9room/festime/service/MessageService.java index e28d8bd..623c916 100644 --- a/src/main/java/Be9room/festime/service/MessageService.java +++ b/src/main/java/Be9room/festime/service/MessageService.java @@ -1,11 +1,10 @@ package Be9room.festime.service; import Be9room.festime.domain.Message; -import Be9room.festime.dto.MessageDto; - -import java.util.List; +import Be9room.festime.dto.MessageChatDto; +import org.springframework.data.domain.Page; public interface MessageService { - Message save(MessageDto messageDto); - List getMessages(); + Message save(MessageChatDto messageChatDto); + Page getMessages(Integer page); } \ No newline at end of file diff --git a/src/main/java/Be9room/festime/service/MessageServiceImpl.java b/src/main/java/Be9room/festime/service/MessageServiceImpl.java index 0bccd88..66481bf 100644 --- a/src/main/java/Be9room/festime/service/MessageServiceImpl.java +++ b/src/main/java/Be9room/festime/service/MessageServiceImpl.java @@ -1,14 +1,14 @@ package Be9room.festime.service; import Be9room.festime.domain.Message; -import Be9room.festime.dto.MessageDto; +import Be9room.festime.dto.MessageChatDto; import Be9room.festime.repository.MessageRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; -import java.util.List; @Service @RequiredArgsConstructor @@ -16,11 +16,11 @@ public class MessageServiceImpl implements MessageService { private final MessageRepository messageRepository; @Override - public Message save(MessageDto messageDto) { + public Message save(MessageChatDto messageChatDto) { Message message = Message.builder() - .memberId(messageDto.getMemberId()) - .memberName(messageDto.getMemberName()) - .message(messageDto.getMessage()) + .memberId(messageChatDto.getMemberId()) + .memberName(messageChatDto.getMemberName()) + .message(messageChatDto.getMessage()) .createdAt(LocalDateTime.now()) .build(); @@ -28,7 +28,7 @@ public Message save(MessageDto messageDto) { } @Override - public List getMessages() { - return messageRepository.findAll(); + public Page getMessages(Integer page) { + return messageRepository.findMessagesByCreatedAtAfterOrderByCreatedAtDesc(LocalDateTime.now().minusHours(3),PageRequest.of(page, 30)); } } diff --git a/src/main/java/Be9room/festime/validation/annotation/CheckPage.java b/src/main/java/Be9room/festime/validation/annotation/CheckPage.java new file mode 100644 index 0000000..1673d4e --- /dev/null +++ b/src/main/java/Be9room/festime/validation/annotation/CheckPage.java @@ -0,0 +1,17 @@ +package Be9room.festime.validation.annotation; + +import Be9room.festime.validation.validator.CheckPageValidator; +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +import java.lang.annotation.*; + +@Documented +@Constraint(validatedBy = CheckPageValidator.class) +@Target({ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface CheckPage { + String message() default "페이지는 0 이상이여야 합니다."; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/src/main/java/Be9room/festime/validation/validator/CheckPageValidator.java b/src/main/java/Be9room/festime/validation/validator/CheckPageValidator.java new file mode 100644 index 0000000..70961dd --- /dev/null +++ b/src/main/java/Be9room/festime/validation/validator/CheckPageValidator.java @@ -0,0 +1,30 @@ +package Be9room.festime.validation.validator; + +import Be9room.festime.apiPayLoad.code.status.ErrorStatus; +import Be9room.festime.validation.annotation.CheckPage; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CheckPageValidator implements ConstraintValidator{ + @Override + public void initialize(CheckPage constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + } + + @Override + public boolean isValid(Integer value, ConstraintValidatorContext context) { + boolean isValid = true; + if(value < 0){ + isValid = false; + } + if(!isValid){ + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate(ErrorStatus._PAGE_ERROR.toString()).addConstraintViolation(); + } + return isValid; + } +}