Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 퀘스트 추천 API를 임시 구현한다 #108

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package daybyquest.quest.application;

import daybyquest.quest.dto.response.MultipleQuestsResponse;
import daybyquest.quest.dto.response.QuestResponse;
import daybyquest.quest.query.QuestDao;
import daybyquest.quest.query.QuestData;
import daybyquest.quest.query.QuestRecommendDao;
import daybyquest.user.domain.User;
import daybyquest.user.domain.Users;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RecommendQuestsService {

private static final int MAX_RECOMMENDATION_COUNT = 5;

private final Users users;

private final QuestRecommendDao recommendDao;

private final QuestDao questDao;

public RecommendQuestsService(final Users users, final QuestRecommendDao recommendDao,
final QuestDao questDao) {
this.users = users;
this.recommendDao = recommendDao;
this.questDao = questDao;
}

@Transactional(readOnly = true)
public MultipleQuestsResponse invoke(final Long loginId) {
final User user = users.getById(loginId);
final List<Long> ids = recommendDao.findTopNNormalQuestIdsByInterestIn(MAX_RECOMMENDATION_COUNT,
user.getInterests());
final List<QuestData> questData = questDao.findAllByIdIn(loginId, ids);
final List<QuestResponse> responses = questData.stream().map(QuestResponse::of).toList();
return new MultipleQuestsResponse(responses);
}
}
15 changes: 14 additions & 1 deletion src/main/java/daybyquest/quest/presentation/QuestQueryApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import daybyquest.auth.domain.AccessUser;
import daybyquest.quest.application.GetQuestByIdService;
import daybyquest.quest.application.GetQuestImagesService;
import daybyquest.quest.application.RecommendQuestsService;
import daybyquest.quest.dto.response.MultipleQuestsResponse;
import daybyquest.quest.dto.response.QuestImagesResponse;
import daybyquest.quest.dto.response.QuestResponse;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,10 +20,14 @@ public class QuestQueryApi {

private final GetQuestImagesService getQuestImagesService;

private final RecommendQuestsService recommendQuestsService;

public QuestQueryApi(final GetQuestByIdService getQuestByIdService,
final GetQuestImagesService getQuestImagesService) {
final GetQuestImagesService getQuestImagesService,
final RecommendQuestsService recommendQuestsService) {
this.getQuestByIdService = getQuestByIdService;
this.getQuestImagesService = getQuestImagesService;
this.recommendQuestsService = recommendQuestsService;
}

@GetMapping("/quest/{questId}")
Expand All @@ -37,4 +43,11 @@ public ResponseEntity<QuestImagesResponse> getQuestImages(@PathVariable final Lo
final QuestImagesResponse response = getQuestImagesService.invoke(questId);
return ResponseEntity.ok(response);
}

@GetMapping("/quest/recommendation")
@Authorization
public ResponseEntity<MultipleQuestsResponse> recommendQuests(final AccessUser accessUser) {
final MultipleQuestsResponse response = recommendQuestsService.invoke(accessUser.getId());
return ResponseEntity.ok(response);
}
}
16 changes: 16 additions & 0 deletions src/main/java/daybyquest/quest/query/QuestRecommendDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package daybyquest.quest.query;

import daybyquest.quest.domain.Quest;
import java.util.Collection;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

public interface QuestRecommendDao extends Repository<Quest, Long> {

@Query(value = "select q.id from quest q where q.category='NORMAL' and q.interest_name in :interests "
+ "and q.id >= FLOOR(1 + RAND() * (select MAX(quest.id) from quest)) "
+ "ORDER BY q.id limit :topN",
nativeQuery = true)
List<Long> findTopNNormalQuestIdsByInterestIn(final int topN, final Collection<String> interests);
}
Loading