Skip to content

Commit

Permalink
๐Ÿ”ฅ word repository ๋ถˆํ•„์š”ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ ์‚ญ์ œ Murakano#90
Browse files Browse the repository at this point in the history
  • Loading branch information
jjikky committed Oct 22, 2024
1 parent bcdd21c commit b4fe8ce
Showing 1 changed file with 78 additions and 130 deletions.
208 changes: 78 additions & 130 deletions src/routes/word/word.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,161 +2,109 @@ const Word = require('./word.model');
const User = require('../user/user.model');

exports.getSearchWords = async (searchTerm) => {
try {
const escapedSearchTerm = searchTerm.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
const searchWords = await Word.findOne({ word: { $regex: `^${escapedSearchTerm}$`, $options: '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;
}
const escapedSearchTerm = searchTerm.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
return await Word.findOne({ word: { $regex: `^${escapedSearchTerm}$`, $options: 'i' } });
};

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;
}
return await Word.find()
.sort({ freq: -1 })
.limit(10)
.map((word) => word.word);
};

exports.getRelatedWords = async (searchTerm, limit) => {
try {
const escapedTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const relatedWords = await Word.find({ word: new RegExp(escapedTerm, 'i') })
.sort({ freq: -1 })
.limit(parseInt(limit));
const wordNames = relatedWords.map((word) => word.word);
return wordNames;
} catch (error) {
console.log('Error while getting related words:', error);
return null;
}
const escapedTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const relatedWords = await Word.find({ word: new RegExp(escapedTerm, 'i') })
.sort({ freq: -1 })
.limit(parseInt(limit));
return relatedWords.map((word) => word.word);
};

exports.getAllWords = async (isSorted, page, limit) => {
try {
const skip = (page - 1) * limit;
const sortOrder = {};
const collation = { locale: 'en', strength: 2 };

if (isSorted === 'asc' || isSorted === 'desc') {
sortOrder.word = isSorted === 'asc' ? 1 : -1;
} else if (isSorted === 'popularity') {
sortOrder.freq = -1;
sortOrder.word = 1;
} else if (isSorted === 'recent') {
sortOrder.createdAt = -1;
sortOrder.word = 1;
}

const words = await Word.find().collation(collation).sort(sortOrder).skip(skip).limit(parseInt(limit, 10));

return words;
} catch (error) {
console.log('Error while getting all words:', error);
return null;
const skip = (page - 1) * limit;
const sortOrder = {};
const collation = { locale: 'en', strength: 2 };

if (isSorted === 'asc' || isSorted === 'desc') {
sortOrder.word = isSorted === 'asc' ? 1 : -1;
} else if (isSorted === 'popularity') {
sortOrder.freq = -1;
sortOrder.word = 1;
} else if (isSorted === 'recent') {
sortOrder.createdAt = -1;
sortOrder.word = 1;
}

return await Word.find().collation(collation).sort(sortOrder).skip(skip).limit(parseInt(limit, 10));
};

exports.addWord = async (requestId, formData) => {
try {
const user = await User.findOne({ 'requests._id': requestId });
if (!user) {
console.log('User with the given request not found');
return null;
}

const request = user.requests.id(requestId);
if (!request) {
console.log('Request not found');
return null;
}

const newWord = new Word({
word: formData.devTerm,
awkPron: formData.awkPron,
comPron: formData.commonPron,
info: formData.addInfo,
suggestedBy: request.suggestedBy,
});

await newWord.save();

console.log('Word added successfully');
} catch (error) {
console.log('Error while adding word:', error);
const user = await User.findOne({ 'requests._id': requestId });
if (!user) {
console.log('User with the given request not found');
return null;
}

const request = user.requests.id(requestId);
if (!request) {
console.log('Request not found');
return null;
}

const newWord = new Word({
word: formData.devTerm,
awkPron: formData.awkPron,
comPron: formData.commonPron,
info: formData.addInfo,
suggestedBy: request.suggestedBy,
});

await newWord.save();
};

exports.updateWord = async (requestId, formData) => {
try {
const user = await User.findOne({ 'requests._id': requestId });
if (!user) {
console.log('User with the given request not found');
return null;
}

const request = user.requests.id(requestId);
if (!request) {
console.log('Request not found');
return null;
}

const wordToUpdate = await Word.findOne({ word: request.word });
if (!wordToUpdate) {
console.log('Word not found in Word collection');
return null;
}

wordToUpdate.word = formData.devTerm;
wordToUpdate.awkPron = formData.awkPron;
wordToUpdate.comPron = formData.commonPron;
wordToUpdate.info = formData.addInfo;
wordToUpdate.suggestedBy = request.suggestedBy;

await wordToUpdate.save();

console.log('Word updated successfully');
return wordToUpdate;
} catch (error) {
console.log('Error while updating word:', error);
const user = await User.findOne({ 'requests._id': requestId });
if (!user) {
console.log('User with the given request not found');
return null;
}

const request = user.requests.id(requestId);
if (!request) {
console.log('Request not found');
return null;
}

const findWord = await Word.findOne({ word: request.word });
if (!findWord) {
console.log('Word not found in Word collection');
return null;
}

updateWordFields(findWord, formData, request);

await findWord.save();

return findWord;
};

exports.deleteWordContributor = async (_id) => {
try {
const user = await User.findById(_id);

if (!user) {
console.log('User not found');
return null;
}
const nickname = user.nickname;

const words = await Word.updateMany({ suggestedBy: nickname }, { suggestedBy: null });
return words;
} catch (error) {
console.log('Error while deleting word contributor:', error);
const user = await User.findById(_id);
if (!user) {
return null;
}
return await Word.updateMany({ suggestedBy: user.nickname }, { suggestedBy: null });
};
exports.checkDuplicateWord = async (word) => {
try {
const escapedTerm = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const wordExists = await Word.findOne({ word: { $regex: new RegExp(`^${escapedTerm}$`, 'i') } });
return wordExists;
} catch (error) {
console.log('Error while checking duplicate word:', error);
return null;
}
const escapedTerm = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return await Word.findOne({ word: { $regex: new RegExp(`^${escapedTerm}$`, 'i') } });
};

const updateWordFields = (wordToUpdate, formData, request) => {
wordToUpdate.word = formData.devTerm;
wordToUpdate.awkPron = formData.awkPron;
wordToUpdate.comPron = formData.commonPron;
wordToUpdate.info = formData.addInfo;
wordToUpdate.suggestedBy = request.suggestedBy;
};

0 comments on commit b4fe8ce

Please sign in to comment.