Skip to content

Commit

Permalink
Merge pull request #19 from sen2y/features/header
Browse files Browse the repository at this point in the history
Features/header 인기검색어 api 로직 구현
  • Loading branch information
sen2y authored Jul 2, 2024
2 parents cbb48f5 + 3ecb1e8 commit b1814d2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
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;
};

0 comments on commit b1814d2

Please sign in to comment.