Skip to content

Commit

Permalink
Merge pull request #93 from jjikky/refactor/word
Browse files Browse the repository at this point in the history
Refactor/word
  • Loading branch information
jjikky authored Oct 23, 2024
2 parents 7a5ede9 + 33a9817 commit 4815077
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 220 deletions.
15 changes: 15 additions & 0 deletions src/common/utils/catch-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const sendResponse = require('../utils/response-handler');

exports.catchAsync = (fn, errorMessage) => {
return async (req, res) => {
try {
await fn(req, res);
} catch (error) {
console.error(error);
if (error?.type) {
return sendResponse.badRequest(res, error.message);
}
return sendResponse.fail(req, res, errorMessage);
}
};
};
17 changes: 10 additions & 7 deletions src/common/utils/request.validator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const ErrorMessage = require('../../common/constants/error-message');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const ErrorMessage = require('../../common/constants/error-message');

const ajv = new Ajv();
addFormats(ajv);

exports.validateRequest = (schema, req) => {
const validate = ajv.compile(schema);
const isValid = validate(req);
if (isValid) {
return validateAndHandleErrors(validate, req);
};

const validateAndHandleErrors = (validate, req) => {
if (validate(req)) {
return req;
} else {
const { errors } = validate;
const message = errors && errors[0] ? errors[0].message : ErrorMessage.BAD_REQUEST;
throw { message, type: 'ajv' };
}
const { errors } = validate;
const message = errors && errors[0] ? errors[0].message : ErrorMessage.BAD_REQUEST;
throw { message, type: 'ajv' };
};
138 changes: 55 additions & 83 deletions src/routes/word/word.controller.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,73 @@
const wordService = require('./word.service');
const userService = require('../user/user.service');
const { catchAsync } = require('../../common/utils/catch-async');
const sendResponse = require('../../common/utils/response-handler');
const ErrorMessage = require('../../common/constants/error-message');
const SuccessMessage = require('../../common/constants/success-message');
const { validateRequest } = require('../../common/utils/request.validator');
const { searchTermSchema, relatedTermSchema, wordListSchema } = require('./word.schema');

exports.getSearchWords = async (req, res) => {
try {
const _id = req?.user ? req.user._id : null;
const validData = validateRequest(searchTermSchema, req.params);
let searchTerm = validData.searchTerm;
const data = await wordService.getSearchWords(searchTerm);
if (_id) {
await userService.updateRecentSearch(_id, searchTerm);
}
const message = data ? SuccessMessage.SEARCH_WORDS_SUCCESS : SuccessMessage.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);
}
};
exports.getSearchWords = catchAsync(async (req, res) => {
const validData = validateRequest(searchTermSchema, req.params);
const { searchTerm } = validData;
const data = await wordService.getSearchWords(searchTerm);
await updateRecentWordIfLogined(req, searchTerm);

exports.getRankWords = async (req, res) => {
try {
const data = await wordService.getRankWords();
sendResponse.ok(res, {
message: SuccessMessage.RANK_WORDS_SUCCESS,
data,
});
} catch (error) {
sendResponse.fail(req, res, ErrorMessage.RANK_WORDS_ERROR);
}
};
const message = data ? SuccessMessage.SEARCH_WORDS_SUCCESS : SuccessMessage.SEARCH_WORDS_NONE;
sendResponse.ok(res, { message, data });
}, ErrorMessage.SEARCH_WORDS_ERROR);

exports.getRelatedWords = async (req, res) => {
try {
const { searchTerm, limit } = validateRequest(relatedTermSchema, req.query);
const data = await wordService.getRelatedWords(searchTerm, limit);
sendResponse.ok(res, {
message: SuccessMessage.RELATED_WORDS_SUCCESS,
data,
});
} catch (error) {
console.log(error);
sendResponse.fail(req, res, ErrorMessage.RELATED_WORDS_ERROR);
}
};
exports.getRankWords = catchAsync(async (req, res) => {
const data = await wordService.getRankWords();
sendResponse.ok(res, {
message: SuccessMessage.RANK_WORDS_SUCCESS,
data,
});
}, ErrorMessage.RANK_WORDS_ERROR);

exports.getAllWords = async (req, res) => {
try {
const { limit, page, sort } = validateRequest(wordListSchema, {
limit: req.query.limit * 1,
page: req.query.page * 1,
sort: req.query.sort,
});
exports.getRelatedWords = catchAsync(async (req, res) => {
const { searchTerm, limit } = validateRequest(relatedTermSchema, req.query);
const data = await wordService.getRelatedWords(searchTerm, limit);
sendResponse.ok(res, {
message: SuccessMessage.RELATED_WORDS_SUCCESS,
data,
});
}, ErrorMessage.RELATED_WORDS_ERROR);

const data = await wordService.getAllWords(sort, page, limit);
exports.getAllWords = catchAsync(async (req, res) => {
const { limit, page, sort } = validateRequest(wordListSchema, {
limit: req.query.limit * 1,
page: req.query.page * 1,
sort: req.query.sort,
});

sendResponse.ok(res, {
message: SuccessMessage.GET_WORDS_SUCCESS,
data,
});
} catch (error) {
console.log(error);
if (error?.type) {
return sendResponse.badRequest(res, error.message);
}
sendResponse.fail(req, res, ErrorMessage.GET_WORDS_ERROR);
}
};

exports.checkDuplicateWord = async (req, res) => {
try {
const { word } = req.body;
const isDataExist = await wordService.checkDuplicateWord(word);
data = { isDataExist };
const data = await wordService.getAllWords(sort, page, limit);

if (isDataExist) {
return sendResponse.ok(res, {
message: ErrorMessage.EXIST_WORD,
data,
});
}
sendResponse.ok(res, {
message: SuccessMessage.GET_WORDS_SUCCESS,
data,
});
}, ErrorMessage.GET_WORDS_ERROR);

exports.checkDuplicateWord = catchAsync(async (req, res) => {
const { word } = req.body;
const isDataExist = await wordService.checkDuplicateWord(word);
if (isDataExist) {
return sendResponse.ok(res, {
message: SuccessMessage.CHECK_DUPLICATE_REQUEST_SUCCESS,
data,
message: ErrorMessage.EXIST_WORD,
data: { isDataExist },
});
} catch (error) {
sendResponse.fail(req, res, ErrorMessage.CHECK_DUPLICATE_REQUEST_ERROR);
}
};

return sendResponse.ok(res, {
message: SuccessMessage.CHECK_DUPLICATE_REQUEST_SUCCESS,
data,
});
});

async function updateRecentWordIfLogined(req, searchTerm) {
const userId = req.user?._id;
if (userId) {
await userService.updateRecentSearch(userId, searchTerm);
}
}
Loading

0 comments on commit 4815077

Please sign in to comment.