diff --git a/src/common/constants/error-message.js b/src/common/constants/error-message.js index 596426f..a6594f9 100644 --- a/src/common/constants/error-message.js +++ b/src/common/constants/error-message.js @@ -30,6 +30,7 @@ const ErrorMessage = Object.freeze({ RECENT_WORDS_ERROR: '최근 검색어 조회중 오류가 발생하였습니다.', DELETE_RECENT_WORD_ERROR: '최근 검색어 삭제중 오류가 발생하였습니다.', SEARCH_WORDS_ERROR: '검색 결과 조회 중 오류가 발생하였습니다.', + RANK_WORDS_ERROR: '인기 검색어 조회 중 오류가 발생하였습니다.', }); module.exports = ErrorMessage; diff --git a/src/common/constants/success-message.js b/src/common/constants/success-message.js index 7b92abd..12160a0 100644 --- a/src/common/constants/success-message.js +++ b/src/common/constants/success-message.js @@ -19,6 +19,7 @@ const SucesssMessage = Object.freeze({ // WORD - 검색어 SEARCH_WORDS_SUCCESS: '검색어 조회 성공', SEARCH_WORDS_NONE: '검색 결과가 없습니다.', + RANK_WORDS_SUCCESS: '인기 검색어 조회 성공', }); module.exports = SucesssMessage; diff --git a/src/routes/user/user.repository.js b/src/routes/user/user.repository.js index be1bb8f..f42879d 100644 --- a/src/routes/user/user.repository.js +++ b/src/routes/user/user.repository.js @@ -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) { diff --git a/src/routes/word/word.controller.js b/src/routes/word/word.controller.js index b2483aa..e12d5dd 100644 --- a/src/routes/word/word.controller.js +++ b/src/routes/word/word.controller.js @@ -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 { diff --git a/src/routes/word/word.repository.js b/src/routes/word/word.repository.js index 6389a31..66bff83 100644 --- a/src/routes/word/word.repository.js +++ b/src/routes/word/word.repository.js @@ -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; + } +}; diff --git a/src/routes/word/word.service.js b/src/routes/word/word.service.js index ac57558..c0c1717 100644 --- a/src/routes/word/word.service.js +++ b/src/routes/word/word.service.js @@ -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; +};