Skip to content

Commit

Permalink
Merge pull request #53 from Murakano/feature/requests
Browse files Browse the repository at this point in the history
등록요청 단어 중복검사 api 추가
  • Loading branch information
grulla99 authored Jul 11, 2024
2 parents fee34e9 + 2ae18d4 commit 999951c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/common/constants/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const ErrorMessage = Object.freeze({
RANK_WORDS_ERROR: '인기 검색어 조회 중 오류가 발생하였습니다.',
RELATED_WORDS_ERROR: '연관 검색어 조회 중 오류가 발생하였습니다.',
REGISTER_WORDS_ERROR: '등록 요청 중 오류가 발생하였습니다.',
EXIST_WORD: '이미 존재하는 단어입니다.',
CHECK_DUPLICATE_WORD_ERROR: '단어 중복검사중 오류가 발생하였습니다.',

// REQUEST
GET_REQUESTS_ERROR: '요청 조회중 오류가 발생하였습니다.',
Expand Down
3 changes: 3 additions & 0 deletions src/common/constants/success-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const SuccessMessage = Object.freeze({

// Word - 등록요청
REGISTER_WORDS_SUCCESS: '등록 요청 성공',

// Word - 중복검사
CHECK_DUPLICATE_REQUEST_SUCCESS: '중복 검사 성공',
});

module.exports = SuccessMessage;
1 change: 0 additions & 1 deletion src/routes/user/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ exports.updateRequest = async (requestId, formData) => {

exports.updateRequestState = async (userId, requestId, status, formData, requestType) => {
if (userId) {
console.log('업데이트 서비스 진입!!!!!!!!!!!!', userId, requestId, status, formData, requestType);
await userRepository.updateRequestState(userId, requestId, status, formData);
if (requestType === 'add') {
await wordRepository.addWord(requestId, formData);
Expand Down
28 changes: 28 additions & 0 deletions src/routes/word/word.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,31 @@ exports.getAllWords = async (req, res) => {
sendResponse.fail(req, res, ErrorMessage.GET_WORDS_ERROR);
}
};

// 등록요청 중복 단어 검사
exports.checkDuplicateWord = async (req, res) => {
try {
const { word } = req.body; // req.body 에서 word 추출
console.log("전달된 중복 검수 단어", );
const isDataExist = await wordService.checkDuplicateWord(word);
data = { isDataExist };
console.log("중복검수 결과", data);

// 중복 단어가 있는 경우
if (isDataExist) {
return sendResponse.ok(res, {
message: ErrorMessage.EXIST_WORD,
data,
});
}
// 중복 단어가 없는 경우
return sendResponse.ok(res, {
message: SuccessMessage.CHECK_DUPLICATE_REQUEST_SUCCESS,
data,
});

} catch (error) {
sendResponse.fail(req, res, ErrorMessage.CHECK_DUPLICATE_REQUEST_ERROR);
}
}

12 changes: 12 additions & 0 deletions src/routes/word/word.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ exports.updateWord = async (requestId, formData) => {
return null;
}
};

exports.checkDuplicateWord = async (word) => {
try {
const wordExists = await Word.findOne({ word: { $regex: new RegExp(`^${word}$`, 'i') } });
console.log('wordExists:', wordExists);
return wordExists;
}
catch (error) {
console.log('Error while checking duplicate word:', error);
return null;
}
}
5 changes: 4 additions & 1 deletion src/routes/word/word.route.js
Original file line number Diff line number Diff line change
@@ -1,7 +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 { getRankWords, getSearchWords, getRelatedWords, getAllWords, checkDuplicateWord } = require('./word.controller');

// 전체 단어 목록 (쿼리 스트링에 정렬 & 페이지 정보)
wordRouter.get('/', getAllWords);
Expand All @@ -13,5 +13,8 @@ wordRouter.get('/search/related', getRelatedWords);
// 검색어 조회
wordRouter.post('/search/:searchTerm', isUser, getSearchWords);

//등록요청 단어 중복검사
wordRouter.post('/checkDuplicateWord', checkDuplicateWord);


module.exports = wordRouter;
7 changes: 7 additions & 0 deletions src/routes/word/word.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ exports.getAllWords = async (sort, page, limit) => {
const words = await wordRepository.getAllWords(sort, page, limit);
return words;
};

// 등록 단어 중복 검사
exports.checkDuplicateWord = async (word) => {
const isDuplicate = await wordRepository.checkDuplicateWord(word);
console.log("isDuplicate: ", isDuplicate)
return isDuplicate;
};

0 comments on commit 999951c

Please sign in to comment.