Skip to content

Commit

Permalink
fix: pokeMessage Facade 변경 (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunom authored Dec 22, 2023
2 parents d07c6b0 + 8d0d209 commit f6c5d78
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.sopt.app.application.poke;

import lombok.RequiredArgsConstructor;
import lombok.val;
import org.sopt.app.domain.entity.PokeMessage;
import org.sopt.app.domain.enums.PokeMessageType;
import org.sopt.app.interfaces.postgres.PokeMessageRepository;
import org.sopt.app.application.poke.PokeInfo.PokeMessageDetail;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -17,25 +16,13 @@ public class PokeMessageService {
private static final int MESSAGES_QUANTITY_AT_ONCE = 5;
private final PokeMessageRepository messageRepository;

public List<Long> pickRandomMessageIdsTypeOf(String type) {
public List<PokeMessage> pickRandomMessageByTypeOf(String type) {
PokeMessageType messageType = PokeMessageType.ofParam(type);
List<Long> allMessageIds = messageRepository.findAllByType(messageType).stream()
.map(PokeMessage::getId)
.collect(Collectors.toList());
Collections.shuffle(allMessageIds, new Random());
return allMessageIds.stream()
val messages = messageRepository.findAllByType(messageType);
Collections.shuffle(messages, new Random());
return messages.stream()
.limit(MESSAGES_QUANTITY_AT_ONCE)
.toList();
}

public List<PokeMessageDetail> getMessagesDetail(List<Long> messageIds) {
return messageRepository.findAllByIdIn(messageIds).stream()
.map(pokeMessage -> PokeMessageDetail.builder()
.id(pokeMessage.getId())
.content(pokeMessage.getContent())
.build())
.collect(Collectors.toList());
}

}

5 changes: 2 additions & 3 deletions src/main/java/org/sopt/app/facade/PokeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public class PokeFacade {

@Transactional(readOnly = true)
public List<PokeResponse.PokeMessage> getPokingMessages(String type) {
List<Long> targetMessageIds = pokeMessageService.pickRandomMessageIdsTypeOf(type);
List<PokeInfo.PokeMessageDetail> messagesDetails = pokeMessageService.getMessagesDetail(targetMessageIds);
return messagesDetails.stream()
val messages = pokeMessageService.pickRandomMessageByTypeOf(type);
return messages.stream()
.map(messagesDetail -> PokeResponse.PokeMessage.of(
messagesDetail.getId(), messagesDetail.getContent()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@

public interface PokeMessageRepository extends JpaRepository<PokeMessage, Long> {

@Query("SELECT pm FROM PokeMessage pm WHERE pm.type = :type")
List<PokeMessage> findAllByType(@Param("type") PokeMessageType type);

@Query("SELECT pm FROM PokeMessage pm WHERE pm.id in :messageIds")
List<PokeMessage> findAllByIdIn(@Param("messageIds") List<Long> messageIds);
List<PokeMessage> findAllByType(PokeMessageType type);
}

0 comments on commit f6c5d78

Please sign in to comment.