Skip to content

Commit

Permalink
[#142] feat: 추천 태그 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo committed Nov 12, 2023
1 parent ab05233 commit d2668e6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class TodaysFailConst {
public static final String TOKEN_ISSUER = "TodaysFail";
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
public static final String REFRESH_TOKEN = "REFRESH_TOKEN";
public static final String RECOMMEND_TAG_KEY = "RecommendTag";

public static final int MILLI_TO_SECOND = 1000;
public static final int SERVICE_UNAVAILABLE = 503;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.todaysfail.events.handler;

import static com.todaysfail.common.consts.TodaysFailConst.RECOMMEND_TAG_KEY;

import com.todaysfail.common.annotation.EventHandler;
import com.todaysfail.domains.failure.domain.Failure;
import com.todaysfail.domains.tag.domain.Tag;
Expand Down Expand Up @@ -36,7 +38,7 @@ public void handleUserRegisterEvent(FailureRegisterEvent event) {
tag -> {
redisTemplate
.opsForZSet()
.incrementScore("RecommendTag", tag.getTagName(), 1);
.incrementScore(RECOMMEND_TAG_KEY, tag.getTagName(), 1);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.todaysfail.api.web.tag;

import com.todaysfail.api.web.tag.dto.response.TagRecommendResponse;
import com.todaysfail.api.web.tag.dto.response.TagResponse;
import com.todaysfail.api.web.tag.usecase.TagPopularUseCase;
import com.todaysfail.api.web.tag.usecase.TagRecommendUseCase;
import com.todaysfail.api.web.tag.usecase.TagSearchUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
Expand All @@ -21,6 +23,7 @@
public class TagController {
private final TagSearchUseCase tagSearchUseCase;
private final TagPopularUseCase tagPopularUseCase;
private final TagRecommendUseCase tagRecommendUseCase;

@Operation(summary = "태그를 검색합니다. (5개)")
@GetMapping("/search")
Expand All @@ -34,7 +37,9 @@ public List<TagResponse> popular() {
return tagPopularUseCase.execute();
}

// @Operation(summary = "추천 태그를 조회합니다.")
// @GetMapping("/recommend")
// TODO: 추천 태그 조회 API 구현
@Operation(summary = "추천 태그를 조회합니다.")
@GetMapping("/recommend")
public List<TagRecommendResponse> recommendTag() {
return tagRecommendUseCase.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.todaysfail.api.web.tag.dto.response;

import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

public record TagRecommendResponse(String tagName, double score) {
public static TagRecommendResponse from(TypedTuple<String> tuple) {
return new TagRecommendResponse(tuple.getValue(), tuple.getScore());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.todaysfail.api.web.tag.usecase;

import static com.todaysfail.common.consts.TodaysFailConst.RECOMMEND_TAG_KEY;

import com.todaysfail.api.web.tag.dto.response.TagRecommendResponse;
import com.todaysfail.common.annotation.UseCase;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

@UseCase
@RequiredArgsConstructor
public class TagRecommendUseCase {
private final RedisTemplate<String, String> redisTemplate;

public List<TagRecommendResponse> execute() {
String key = RECOMMEND_TAG_KEY;
ZSetOperations<String, String> ZSetOperations = redisTemplate.opsForZSet();
Set<TypedTuple<String>> typedTuples = ZSetOperations.reverseRangeWithScores(key, 0, 9);
return typedTuples.stream().map(TagRecommendResponse::from).collect(Collectors.toList());
}
}

0 comments on commit d2668e6

Please sign in to comment.