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

Features/header 인기검색어 api 로직 구현 #19

Merged
merged 3 commits into from
Jul 2, 2024
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
1 change: 1 addition & 0 deletions src/common/constants/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ErrorMessage = Object.freeze({
RECENT_WORDS_ERROR: '최근 검색어 조회중 오류가 발생하였습니다.',
DELETE_RECENT_WORD_ERROR: '최근 검색어 삭제중 오류가 발생하였습니다.',
SEARCH_WORDS_ERROR: '검색 결과 조회 중 오류가 발생하였습니다.',
RANK_WORDS_ERROR: '인기 검색어 조회 중 오류가 발생하였습니다.',
});

module.exports = ErrorMessage;
1 change: 1 addition & 0 deletions src/common/constants/success-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SucesssMessage = Object.freeze({
// WORD - 검색어
SEARCH_WORDS_SUCCESS: '검색어 조회 성공',
SEARCH_WORDS_NONE: '검색 결과가 없습니다.',
RANK_WORDS_SUCCESS: '인기 검색어 조회 성공',
});

module.exports = SucesssMessage;
1 change: 0 additions & 1 deletion src/routes/user/user.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ exports.updateRecentSearch = async (_id, searchTerm) => {
console.log('User not found');
}
const recentSearch = user.recentSearches.find((search) => search.searchTerm === searchTerm);

if (recentSearch) {
// 검색어가 이미 존재하는 경우
if (recentSearch.deletedAt) {
Expand Down
12 changes: 11 additions & 1 deletion src/routes/word/word.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ const SucesssMessage = require('../../common/constants/success-message');
const { validateRequest } = require('../../common/utils/request.validator');
const { rankWordsSchema, searchTermSchema } = require('./word.schema');

exports.getRankWords = async (req, res) => {};
exports.getRankWords = async (req, res) => {
try {
const data = await wordService.getRankWords();
sendResponse.ok(res, {
message: SucesssMessage.RANK_WORDS_SUCCESS,
data,
});
} catch (error) {
sendResponse.fail(req, res, ErrorMessage.RANK_WORDS_ERROR);
}
};

exports.getSearchWords = async (req, res) => {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/routes/word/word.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ exports.getSearchWords = async (searchTerm) => {
return null;
}
};

exports.getRankWords = async () => {
try {
const words = await Word.find().sort({ freq: -1 }).limit(10);
const wordNames = words.map((word) => word.word);
return wordNames;
} catch (error) {
console.log('Error while getting rank words:', error);
return null;
}
};
6 changes: 6 additions & 0 deletions src/routes/word/word.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ exports.getSearchWords = async (searchTerm) => {
const searchWords = await wordRepository.getSearchWords(searchTerm);
return searchWords;
};

// 인기검색어 조회
exports.getRankWords = async () => {
const rankWords = await wordRepository.getRankWords();
return rankWords;
};