-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Be9room/feat/11
Feat/11 : 무한스크롤 기능 구현
- Loading branch information
Showing
15 changed files
with
233 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/Be9room/festime/converter/MessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Message> messagePage){ | ||
List<MessageWebDto.MessageResponseDto> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MessageResponseDto> 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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/Be9room/festime/repository/MessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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<Message, String> { | ||
Page<Message> findMessagesByCreatedAtAfterOrderByCreatedAtDesc(LocalDateTime time, PageRequest pageRequest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package Be9room.festime.service; | ||
|
||
import Be9room.festime.domain.Message; | ||
import Be9room.festime.dto.MessageChatDto; | ||
import org.springframework.data.domain.Page; | ||
|
||
public interface MessageService { | ||
Message save(MessageChatDto messageChatDto); | ||
Page<Message> getMessages(Integer page); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/Be9room/festime/service/MessageServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package Be9room.festime.service; | ||
|
||
import Be9room.festime.domain.Message; | ||
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 java.time.LocalDateTime; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MessageServiceImpl implements MessageService { | ||
private final MessageRepository messageRepository; | ||
|
||
@Override | ||
public Message save(MessageChatDto messageChatDto) { | ||
Message message = Message.builder() | ||
.memberId(messageChatDto.getMemberId()) | ||
.memberName(messageChatDto.getMemberName()) | ||
.message(messageChatDto.getMessage()) | ||
.createdAt(LocalDateTime.now()) | ||
.build(); | ||
|
||
return messageRepository.save(message); | ||
} | ||
|
||
@Override | ||
public Page<Message> getMessages(Integer page) { | ||
return messageRepository.findMessagesByCreatedAtAfterOrderByCreatedAtDesc(LocalDateTime.now().minusHours(3),PageRequest.of(page, 30)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/Be9room/festime/validation/annotation/CheckPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<? extends Payload>[] payload() default {}; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/Be9room/festime/validation/validator/CheckPageValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CheckPage, Integer>{ | ||
@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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
spring: | ||
application: | ||
name: festime_BE | ||
|
||
data: | ||
mongodb: | ||
uri: ${MONGO_HOST} | ||
|
||
rabbit: | ||
host: ${RABBIT_HOST} |