-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Murakano/dev
Dev
- Loading branch information
Showing
16 changed files
with
300 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const wordService = require('./word.service'); | ||
const userService = require('../user/user.service'); | ||
const sendResponse = require('../../common/utils/response-handler'); | ||
const ErrorMessage = require('../../common/constants/error-message'); | ||
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) => { | ||
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 { | ||
const _id = req?.user ? req.user._id : null; | ||
|
||
// 검색어 검증 | ||
const validData = validateRequest(searchTermSchema, req.params); | ||
// 요청 파라미터에서 검색어 추출 | ||
const searchTerm = validData.searchTerm; | ||
// 검색어 조회 | ||
const data = await wordService.getSearchWords(searchTerm); | ||
|
||
if (_id) { | ||
await userService.updateRecentSearch(_id, searchTerm); | ||
} | ||
const message = data ? SucesssMessage.SEARCH_WORDS_SUCCESS : SucesssMessage.SEARCH_WORDS_NONE; | ||
sendResponse.ok(res, { | ||
message, | ||
data, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
if (error?.type) { | ||
return sendResponse.badRequest(res, error.message); | ||
} | ||
sendResponse.fail(req, res, ErrorMessage.SEARCH_WORDS_ERROR); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const wordSchema = new mongoose.Schema( | ||
{ | ||
word: { type: String, required: true, unique: true }, //콜렉션 이름과 겹치는데 상관없나.. | ||
awkPron: { type: String }, // 어색한 발음 | ||
comPron: { type: String, required: true }, // 일반적인 발음 | ||
info: { type: String }, //추가정보 | ||
suggestedBy: { type: String }, // 제안한 사용자의 닉네임 | ||
freq: { type: Number, default: 0 }, // 인기검색어 | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
// 검색 시마다 검색어의 freq를 1씩 증가시키는 미들웨어 | ||
wordSchema.pre(/^findOne/, async function (next) { | ||
await this.model.updateOne(this.getQuery(), { $inc: { freq: 1 } }); | ||
next(); | ||
}); | ||
|
||
module.exports = mongoose.model('Word', wordSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const Word = require('./word.model'); | ||
|
||
exports.getSearchWords = async (searchTerm) => { | ||
try { | ||
// 대소문자 구분 없이 검색어를 찾기 위한 정규 표현식 사용 | ||
const searchWords = await Word.findOne({ word: new RegExp(`^${searchTerm}$`, 'i') }); | ||
|
||
if (!searchWords) { | ||
console.log('Search term not found in Word collection'); | ||
} | ||
|
||
return searchWords; | ||
} catch (error) { | ||
console.log('Error while getting search words:', error); | ||
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; | ||
} | ||
}; |
Oops, something went wrong.