Skip to content

Commit

Permalink
chore: 추천 템플릿이 중복인 경우, 그 중에서 랜덤으로 하나 선택하도록 수정 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekks authored Sep 11, 2024
1 parent 92cb472 commit 1419e3b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.layer.domain.form.service;

import lombok.RequiredArgsConstructor;

import org.layer.domain.common.random.CustomRandom;
import org.layer.domain.form.controller.dto.request.FormNameUpdateRequest;
import org.layer.domain.form.controller.dto.request.RecommendFormQueryDto;
import org.layer.domain.form.controller.dto.request.RecommendFormSetRequest;
Expand Down Expand Up @@ -46,6 +48,8 @@ public class FormService {
private final SpaceRepository spaceRepository;
private final TemplateMetadataRepository metadataRepository;

private final CustomRandom customRandom;

/**
* 회고 폼 질문을 조회한다.
*
Expand All @@ -72,7 +76,7 @@ public FormGetResponse getForm(Long formId, Long memberId) {
}

public RecommendFormResponseDto getRecommendTemplate(RecommendFormQueryDto queryDto) {
FormTag recommandFormTag = FormTag.getRecommandFormTag(queryDto.purpose());
FormTag recommandFormTag = FormTag.getRecommandFormTag(queryDto.purpose(), customRandom);

Form form = formRepository.findByFormTagAndFormTypeOrThrow(recommandFormTag, FormType.TEMPLATE);
TemplateMetadata metadata = metadataRepository.findByFormIdOrThrow(form.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.layer.domain.common.random;

public interface CustomRandom {
String generateRandomValue();

int nextInt(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.layer.domain.common.random;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("test")
public class MockRandom implements CustomRandom {

@Override
public String generateRandomValue() {
return "test-random-value";
}

@Override
public int nextInt(int index) {
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.layer.domain.common.random;

import java.util.Random;
import java.util.UUID;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile({"local", "dev", "prod"})
public class RealRandom implements CustomRandom {
@Override
public String generateRandomValue() {
return UUID.randomUUID().toString().substring(0, 13);
}

@Override
public int nextInt(int index) {
Random random = new Random();
return random.nextInt(index);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.layer.domain.form.enums;

import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.layer.domain.common.random.CustomRandom;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand All @@ -21,7 +24,7 @@ public enum FormTag {

private final String tag;

public static FormTag getRecommandFormTag(List<RetrospectPurpose> retrospectPurposes) {
public static FormTag getRecommandFormTag(List<RetrospectPurpose> retrospectPurposes, CustomRandom customRandom) {
Map<FormTag, Integer> formTagIntegerMap = EnumSet.allOf(FormTag.class).stream()
.collect(Collectors.toMap(tag -> tag, tag -> 0));

Expand All @@ -33,10 +36,14 @@ public static FormTag getRecommandFormTag(List<RetrospectPurpose> retrospectPurp
formTagIntegerMap.put(PMI, formTagIntegerMap.get(PMI) +purpose.getPmiPont());
});

return formTagIntegerMap.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
int maxValue = Collections.max(formTagIntegerMap.values());

List<FormTag> maxTags = formTagIntegerMap.entrySet().stream()
.filter(entry -> entry.getValue() == maxValue)
.map(Map.Entry::getKey)
.orElse(FormTag.UNTITLED);
.toList();

// 최댓값을 가진 것 중 랜덤으로 하나 선택
return maxTags.get(customRandom.nextInt(maxTags.size()));
}
}

0 comments on commit 1419e3b

Please sign in to comment.