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

Dev #62

Merged
merged 8 commits into from
Jul 15, 2024
4 changes: 2 additions & 2 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ exports.postWords = async (req, res) => {
try {
const validData = validateRequest(requestBodySchema, req.body);
const { _id } = req.user;
const { nickname } = req.params;
const { formData, type } = req.body;
const { formData, type, nickname } = validData;
console.log(formData);
const result = await userService.postWords(_id, formData, nickname, type);
sendResponse.ok(res, {
message: SuccessMessage.REGISTER_WORDS_SUCCESS,
Expand Down
34 changes: 18 additions & 16 deletions src/routes/user/user.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ exports.postWords = async (userId, formData, nickname, type) => {

console.log('User before modification:', JSON.stringify(user.requests, null, 2));

// 이미 존재하는 단어 요청 확인 (status가 'pend'인 경우에만 중복 확인)
const existingRequest = user.requests.find((req) => req.word === formData.devTerm && req.status === 'pend');
if (existingRequest) {
console.log('이미 같은 단어 요청이 존재합니다.');
throw new Error('Word request already exists');
}

if (type === 'add') {
user.requests.push({
word: formData.devTerm,
Expand All @@ -108,22 +115,16 @@ exports.postWords = async (userId, formData, nickname, type) => {
suggestedBy: nickname, // nickname 추가
});
} else if (type === 'mod') {
const request = user.requests.find((req) => req.word === formData.devTerm);
if (!request) {
user.requests.push({
word: formData.devTerm,
info: formData.addInfo,
awkPron: formData.awkPron,
comPron: formData.commonPron,
deletedAt: null,
status: 'pend',
type: 'mod',
suggestedBy: nickname, // nickname 추가
});
} else {
console.log('이미 같은 단어 수정 요청이 존재합니다.');
throw new Error('Word not found');
}
user.requests.push({
word: formData.devTerm,
info: formData.addInfo,
awkPron: formData.awkPron,
comPron: formData.commonPron,
deletedAt: null,
status: 'pend',
type: 'mod',
suggestedBy: nickname, // nickname 추가
});
} else {
throw new Error('Invalid type');
}
Expand All @@ -137,6 +138,7 @@ exports.postWords = async (userId, formData, nickname, type) => {
}
};


exports.getUserRequests = async (userId) => {
try {
const user = await User.findById(userId).select('requests').exec();
Expand Down
2 changes: 1 addition & 1 deletion src/routes/user/user.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const commonSchemas = {
},
word: {
type: 'string',
pattern: '^[a-zA-Z!@#$%^&*()_+\\-=\\[\\]{};:\'",.<>/?]+$',
pattern: '^[a-zA-Z0-9 !@#$%^&*()_+\\-=\\[\\]{};:\'",.<>/?~`/]+$',
},
};

Expand Down
1 change: 1 addition & 0 deletions src/routes/word/word.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ exports.getAllWords = async (isSorted, page, limit) => {
sortOrder.word = isSorted === 'asc' ? 1 : -1;
} else if (isSorted === 'popularity') {
sortOrder.freq = -1;
sortOrder.word = 1; // freq가 동일한 경우 word 오름차��으로 정��
} else if (isSorted === 'recent') {
sortOrder.createdAt = -1;
sortOrder.word = 1; // createdAt이 동일한 경우 단어 오름차순으로 정렬
Expand Down