Skip to content

Commit

Permalink
#275 [Backend] New Spec 23042023
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 23, 2023
1 parent c474923 commit f9eb9e7
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 70 deletions.
21 changes: 21 additions & 0 deletions backend-manager-student/src/admin_api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const session = require('express-session');
const CONFIGS = require('../share/configs/config');
const CONSTANTS = require('../share/configs/constants');
const OPTIONS = require('../share/configs/option');
const MESSAGES = require('../share/configs/message');
const { returnReasons } = require('../share/middleware/handle_error');

//! CACHE MEMORY
const { REDIS_MASTER } = require('../share/db/init_multiple_redis');
Expand Down Expand Up @@ -84,4 +86,23 @@ app.use(
//! ROUTE
app.use(ADMIN_API);

//! INIT HANDLE ERROR
app.use((req, res, next) => {
const error = new Error(MESSAGES.GENERAL.NOTFOUND);
error.status = CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND;
next(error);
});

/* eslint-disable no-unused-vars */
app.use((error, req, res, next) => {
const statusCode = error.status || CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR;
const message = error.message || MESSAGES.GENERAL.INTERNAL_SERVER;
return res.status(statusCode).json({
status: statusCode,
stack: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_DEV ? error.stack : MESSAGES.MEDIA.STRING_EMPTY,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND),
element: message,
});
});

module.exports = app;
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ const bookController = {
result_insert_book_categories === undefined
? null
: result_insert_book_categories
? MESSAGES.GENERAL.SERVER_INSERT_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
? MESSAGES.GENERAL.SERVER_INSERT_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
},
},
});
Expand Down Expand Up @@ -269,8 +269,8 @@ const bookController = {
result_insert_book_categories === undefined
? null
: result_insert_book_categories
? MESSAGES.GENERAL.SERVER_UPDATE_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
? MESSAGES.GENERAL.SERVER_UPDATE_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
},
},
});
Expand Down
5 changes: 5 additions & 0 deletions backend-manager-student/src/share/configs/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = {
NOT_EXIT_ACCOUNT: 'Account admin not exit!',
},
GENERAL: {
// ? EMPTY
STRING_EMPTY: '',

// ? INVALID INPUT HEADER,..
INVALID_INPUT: 'Invalid input!',
INVALID_HEADER: 'Invalid Header!',
Expand All @@ -42,6 +45,7 @@ module.exports = {
// ? EXITS
EXITS_EMAIL_PHONE: 'Email or Phone or Email or Mssv exits !',
EXITS_NOT_BOOK: 'Book Not Found!',
EXITS_NOT_COMMENT: 'Comment Not Found!',
EXITS_NOT_BORROW_BOOK: 'Borrow Book Not Found!',
EXITS_DELETE_BOOK: 'Book already delete!',
EXITS_DELETE_AUTHOR: 'Author already delete!',
Expand Down Expand Up @@ -101,6 +105,7 @@ module.exports = {
SERVER_INSERT_FAIL: 'Insert Fail!',
SERVER_UPDATE_FAIL: 'Update Fail!',
SERVER_CURD_SUCCESS: 'Success!',
INTERNAL_SERVER: 'Internal Server Error',

// ? SUCCESS
SUCCESS_CHANGE_PASSWORD: 'Change Password Success!',
Expand Down
24 changes: 12 additions & 12 deletions backend-manager-student/src/share/middleware/handle_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ module.exports = {
const constraint = error.constraint;
let message;
switch (constraint) {
case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL:
message = MESSAGES.GENERAL.EXITS_EMAIL;
break;
case KEY_DUPLICATE.DUPLICATE_KEY_PHONE:
message = MESSAGES.GENERAL.EXITS_PHONE;
break;
case KEY_DUPLICATE.DUPLICATE_KEY_MSSV:
message = MESSAGES.GENERAL.EXITS_MSSV;
break;
default:
message = MESSAGES.GENERAL.ERROR_UNKNOWN;
break;
case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL:
message = MESSAGES.GENERAL.EXITS_EMAIL;
break;
case KEY_DUPLICATE.DUPLICATE_KEY_PHONE:
message = MESSAGES.GENERAL.EXITS_PHONE;
break;
case KEY_DUPLICATE.DUPLICATE_KEY_MSSV:
message = MESSAGES.GENERAL.EXITS_MSSV;
break;
default:
message = MESSAGES.GENERAL.ERROR_UNKNOWN;
break;
}
return message;
},
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/author.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
createAuthor: (data) =>
new Promise((resolve, reject) => {
try {
const result = knex('authors').insert(data).onConflict('author_id').merge().returning(['author_id']);
const result = knex('authors').insert(data).onConflict('author_id').merge()
.returning(['author_id']);
resolve(result);
} catch (error) {
reject(error);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/book.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
createBook: (data) =>
new Promise((resolve, reject) => {
try {
const result = knex('books').insert(data).onConflict('book_id').merge().returning(['book_id']);
const result = knex('books').insert(data).onConflict('book_id').merge()
.returning(['book_id']);
resolve(result);
} catch (error) {
reject(error);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/phone.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
createPhone: (data) =>
new Promise((resolve, reject) => {
try {
const result = knex('phone').insert(data).onConflict('phone_id').merge().returning(['phone_id']);
const result = knex('phone').insert(data).onConflict('phone_id').merge()
.returning(['phone_id']);
resolve(result);
} catch (error) {
reject(error);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/rating.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
createRatings: (data) =>
new Promise((resolve, reject) => {
try {
const result = knex('book_rates').insert(data).onConflict('rate_id').merge().returning(['rate_id']);
const result = knex('book_rates').insert(data).onConflict('rate_id').merge()
.returning(['rate_id']);
resolve(result);
} catch (error) {
reject(error);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ module.exports = {
createStudent: (data) =>
new Promise((resolve, reject) => {
try {
const result_student = knex('user').insert(data).onConflict('user_id').merge().returning(['user_id']);
const result_student = knex('user').insert(data).onConflict('user_id').merge()
.returning(['user_id']);
resolve(result_student);
} catch (error) {
reject(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const list_comment = async (match, search_data) => {
return list_comment;
};

const list_comment_slug = async (match, search_data) =>
await COMMENT.find(match, search_data).sort({ full_slug: 1 }).lean();

/**
* @author Nguyễn Tiến Tài
* @created_at 13/04/2023
Expand All @@ -54,8 +57,27 @@ const list_comment = async (match, search_data) => {
*/
const get_paren_slug = async (data) => await COMMENT.findOne(data).lean();

/**
* @author Nguyễn Tiến Tài
* @created_at 23/04/2023
* @description Delete comment
* @function delete_comment
*/
const delete_comment = async (commentId) => await COMMENT.deleteOne(commentId);

/**
* @author Nguyễn Tiến Tài
* @created_at 23/04/2023
* @description get paren slug
* @function get_comment_id
*/
const get_comment_id = async (data) => await COMMENT.findOne(data).lean();

module.exports = {
insert_comment,
list_comment,
get_paren_slug,
delete_comment,
get_comment_id,
list_comment_slug,
};
30 changes: 15 additions & 15 deletions backend-manager-student/src/share/utils/redis_pub_sub_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ const handleException = (err, name, port) => {
});
let message_queue;
switch (name) {
case CONSTANTS.NAME_SERVER.STUDENT:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT;
break;
case CONSTANTS.NAME_SERVER.ADMIN:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN;
break;
case CONSTANTS.NAME_SERVER.CRON:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON;
break;
case CONSTANTS.NAME_SERVER.DB:
message_queue = CONSTANTS.QUEUE.REDIS_DB;
break;
default:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON;
break;
case CONSTANTS.NAME_SERVER.STUDENT:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT;
break;
case CONSTANTS.NAME_SERVER.ADMIN:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN;
break;
case CONSTANTS.NAME_SERVER.CRON:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON;
break;
case CONSTANTS.NAME_SERVER.DB:
message_queue = CONSTANTS.QUEUE.REDIS_DB;
break;
default:
message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON;
break;
}
// Publish data queue Redis
return queueMessageTelegram(message_queue, {
Expand Down
21 changes: 21 additions & 0 deletions backend-manager-student/src/user_api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const session = require('express-session');
const CONFIGS = require('../share/configs/config');
const CONSTANTS = require('../share/configs/constants');
const OPTIONS = require('../share/configs/option');
const MESSAGES = require('../share/configs/message');
const { returnReasons } = require('../share/middleware/handle_error');

//! CACHE MEMORY
const { REDIS_MASTER } = require('../share/db/init_multiple_redis');
Expand Down Expand Up @@ -90,6 +92,25 @@ app.use(DEVICE_MIDDLEWARE);
//! ROUTE
app.use(USER_API);

//! INIT HANDLE ERROR
app.use((req, res, next) => {
const error = new Error(MESSAGES.GENERAL.NOTFOUND);
error.status = CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND;
next(error);
});

/* eslint-disable no-unused-vars */
app.use((error, req, res, next) => {
const statusCode = error.status || CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR;
const message = error.message || MESSAGES.GENERAL.INTERNAL_SERVER;
return res.status(statusCode).json({
status: statusCode,
stack: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_DEV ? error.stack : MESSAGES.MEDIA.STRING_EMPTY,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND),
element: message,
});
});

//! REDIS PUBSUB
require('../share/db/redis_queue');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ const commentController = {
}

const search_data = {
_id: 0,
_id: 1,
user_id: 1,
book_id: 1,
content: 1,
slug: 1,
full_slug: 1,
parent_slug: 1,
comment_replies_num: 1,
createdAt: 1,
updatedAt: 1,
};
const comments = await list_comment(match, search_data);

Expand Down
Loading

0 comments on commit f9eb9e7

Please sign in to comment.