diff --git a/src/common/constants/error-message.js b/src/common/constants/error-message.js index 877743f..0261a19 100644 --- a/src/common/constants/error-message.js +++ b/src/common/constants/error-message.js @@ -32,6 +32,8 @@ const ErrorMessage = Object.freeze({ SEARCH_WORDS_ERROR: '검색 결과 조회 중 오류가 발생하였습니다.', GET_WORDS_ERROR: '단어 조회 중 오류가 발생하였습니다.', RANK_WORDS_ERROR: '인기 검색어 조회 중 오류가 발생하였습니다.', + RELATED_WORDS_ERROR: '연관 검색어 조회 중 오류가 발생하였습니다.', + REGISTER_WORDS_ERROR: '등록 요청 중 오류가 발생하였습니다.', // REQUEST GET_REQUESTS_ERROR: '요청 조회중 오류가 발생하였습니다.', diff --git a/src/common/constants/success-message.js b/src/common/constants/success-message.js index 54712d7..e9575e3 100644 --- a/src/common/constants/success-message.js +++ b/src/common/constants/success-message.js @@ -31,6 +31,9 @@ const SuccessMessage = Object.freeze({ // WORD - 조회 GET_WORDS_SUCCESS: '단어 조회 성공', RELATED_WORDS_SUCCESS: '연관 검색어 조회 성공', + + // Word - 등록요청 + REGISTER_WORDS_SUCCESS: '등록 요청 성공', }); module.exports = SuccessMessage; diff --git a/src/routes/user/user.controller.js b/src/routes/user/user.controller.js index a2f8bec..c71e708 100755 --- a/src/routes/user/user.controller.js +++ b/src/routes/user/user.controller.js @@ -220,6 +220,24 @@ exports.delRecentSearch = async (req, res) => { } }; +// 새로운 단어 등록 및 수정 +exports.postWords = async (req, res) => { + try { + const { _id } = req.user; + const { nickname } = req.params; // URL 파라미터에서 nickname 추출 + const { formData, type } = req.body; // formData와 type을 요청 본문에서 분리 + + const result = await userService.postWords(_id, formData, nickname, type); + + sendResponse.ok(res, { + message: SuccessMessage.REGISTER_WORDS_SUCCESS, + data: result + }); + } catch (error) { + console.log("Error during postWords:", error); + sendResponse.fail(req, res, ErrorMessage.REGISTER_WORDS_ERROR); + } +}; exports.UserRequests = async (req, res) => { try{ const { _id } = req.user; diff --git a/src/routes/user/user.repository.js b/src/routes/user/user.repository.js index 1f4e8ed..30e815b 100644 --- a/src/routes/user/user.repository.js +++ b/src/routes/user/user.repository.js @@ -86,6 +86,57 @@ exports.updateRecentSearch = async (_id, searchTerm) => { } }; +// 단어 추가 및 수정 +exports.postWords = async (userId, formData, nickname, type) => { + try { + const user = await User.findById(userId).exec(); + if (!user) { + throw new Error('User not found'); + } + + console.log("User before modification:", JSON.stringify(user.requests, null, 2)); + + if (type === 'add') { + user.requests.push({ + word: formData.devTerm, + info: formData.addInfo, + awkPron: formData.awkPron, + comPron: formData.commonPron, + deletedAt: null, + status: 'pend', + type: 'add', + 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'); + } + } else { + throw new Error('Invalid type'); + } + + await user.save(); + console.log("User after modification:", JSON.stringify(user.requests, null, 2)); + return user.requests.find(req => req.word === formData.devTerm); + } catch (err) { + console.error(err); + throw err; + } +}; + exports.getUserRequests = async (userId) => { try { const user = await User.findById(userId).select('requests').exec(); @@ -153,7 +204,6 @@ exports.getRole = async (userId) => { exports.updateRequest = async (requestId, formData) => { try { - console.log("updateRequest 레포진입!!!!!!!!!!!!", requestId, formData) const user = await User.findOne({ 'requests._id': requestId }).select('requests').exec(); if (!user) { throw new Error('User not found'); @@ -201,5 +251,4 @@ exports.updateRequestState = async (userId, requestId, status) => { } catch (err) { console.error(err); - } -}; \ No newline at end of file + }} \ No newline at end of file diff --git a/src/routes/user/user.route.js b/src/routes/user/user.route.js index 3b69344..9934f25 100644 --- a/src/routes/user/user.route.js +++ b/src/routes/user/user.route.js @@ -10,6 +10,7 @@ const { logout, recentSearches, delRecentSearch, + postWords, UserRequests, UserRequestsAll, deleteRequest, @@ -17,7 +18,7 @@ const { getRole, updateRequestState, } = require('./user.controller'); -const { isLoggedIn, isNotLoggedIn } = require('../../common/utils/auth'); +const { isLoggedIn, isNotLoggedIn, isUser } = require('../../common/utils/auth'); const userRouter = express.Router(); // 회원가입 @@ -37,6 +38,10 @@ userRouter.get('/profile', isLoggedIn, getProfile); userRouter.get('/recent', isLoggedIn, recentSearches); // 최근 검색어 조회 userRouter.delete('/:searchTerm', isLoggedIn, delRecentSearch); // 최근 검색어 삭제 +//등록 요청 +userRouter.post('/requests/:nickname/new', isLoggedIn, postWords); + +module.exports = userRouter; // 요청 조회 userRouter.get('/requests', isLoggedIn, UserRequests); // 요청 목록 조회 userRouter.get('/requests/all', isLoggedIn, UserRequestsAll); // 모든 요청 목록 조회 diff --git a/src/routes/user/user.service.js b/src/routes/user/user.service.js index c49f202..53fb77e 100644 --- a/src/routes/user/user.service.js +++ b/src/routes/user/user.service.js @@ -45,6 +45,16 @@ exports.updateRecentSearch = async (userID, searchTerm) => { } }; +// 단어 추가 및 수정 +exports.postWords = async (userId, formData, nickname, type) => { + try { + const word = await userRepository.postWords(userId, formData, nickname, type); + return word; + } catch (error) { + console.error('Error in userService.postWords:', error.message); + throw new Error('Error processing word: ' + error.message); + } +}; exports.getUserRequests = async (userId) => { const requests = await userRepository.getUserRequests(userId); return requests; diff --git a/src/routes/word/word.controller.js b/src/routes/word/word.controller.js index 734eac4..ec5e4bd 100644 --- a/src/routes/word/word.controller.js +++ b/src/routes/word/word.controller.js @@ -2,7 +2,7 @@ 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 SuccessMessage = require('../../common/constants/success-message'); const { validateRequest } = require('../../common/utils/request.validator'); const { searchTermSchema, relatedTermSchema, wordListSchema } = require('./word.schema'); const { request } = require('express'); @@ -20,7 +20,7 @@ exports.getSearchWords = async (req, res) => { if (_id) { await userService.updateRecentSearch(_id, searchTerm); } - const message = data ? SucesssMessage.SEARCH_WORDS_SUCCESS : SucesssMessage.SEARCH_WORDS_NONE; + const message = data ? SuccessMessage.SEARCH_WORDS_SUCCESS : SuccessMessage.SEARCH_WORDS_NONE; sendResponse.ok(res, { message, data, @@ -39,7 +39,7 @@ exports.getRankWords = async (req, res) => { try { const data = await wordService.getRankWords(); sendResponse.ok(res, { - message: SucesssMessage.RANK_WORDS_SUCCESS, + message: SuccessMessage.RANK_WORDS_SUCCESS, data, }); } catch (error) { @@ -54,7 +54,7 @@ exports.getRelatedWords = async (req, res) => { searchTerm = validateRequest(relatedTermSchema, searchTerm); const data = await wordService.getRelatedWords(searchTerm, limit); sendResponse.ok(res, { - message: SucesssMessage.RELATED_WORDS_SUCCESS, + message: SuccessMessage.RELATED_WORDS_SUCCESS, data, }); } catch (error) { diff --git a/src/routes/word/word.route.js b/src/routes/word/word.route.js index 55ebb3c..979c4f7 100644 --- a/src/routes/word/word.route.js +++ b/src/routes/word/word.route.js @@ -1,8 +1,7 @@ const express = require('express'); const wordRouter = express.Router(); - +const { isUser } = require('../../common/utils/auth'); const { getRankWords, getSearchWords, getRelatedWords, getAllWords } = require('./word.controller'); -const { isLoggedIn, isUser } = require('../../common/utils/auth'); // 전체 단어 목록 (쿼리 스트링에 정렬 & 페이지 정보) wordRouter.get('/', getAllWords); @@ -14,4 +13,5 @@ wordRouter.get('/search/related', getRelatedWords); // 검색어 조회 wordRouter.post('/search/:searchTerm', isUser, getSearchWords); + module.exports = wordRouter;