Skip to content

Commit

Permalink
#259 [Backend] change spec borrow_book and book
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 17, 2023
1 parent 7334d54 commit d057b1a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 20 deletions.
1 change: 1 addition & 0 deletions backend-manager-student/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ module.exports = {
'no-unused-expressions': 'off',
'implicit-arrow-linebreak': 'off',
'object-curly-newline': 'off',
'no-nested-ternary': 'off',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const { returnReasons } = require('../../../../share/middleware/handle_error');

//! MODEL
const book_model = require('../../../../share/models/book.model');
const book_category_model = require('../../../../share/models/book_categories.model');

//! SERVICE
const book_admin_service = require('../../../../share/services/admin_service/book_service');
const book_categories_admin_service = require('../../../../share/services/admin_service/book_categories.service');

const bookController = {
/**
Expand All @@ -25,20 +27,30 @@ const bookController = {
* @return {Object:{Number,String}}
*/
createBook: async (req, res) => {
const { name, author_id, image_uri, description, page_number, bookshelf, language, quantity, public_id_image } =
req.body.input.book_input;
const {
name,
author_id,
image_uri,
description,
page_number,
bookshelf,
language,
quantity,
public_id_image,
book_categories_array,
} = req.body.input.book_input;

// Check input
if (
!name
|| !author_id
|| !image_uri
|| !description
|| !bookshelf
|| !language
|| !quantity
|| !public_id_image
|| !page_number
!name ||
!author_id ||
!image_uri ||
!description ||
!bookshelf ||
!language ||
!quantity ||
!public_id_image ||
!page_number
) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand All @@ -48,8 +60,15 @@ const bookController = {
},
});
}
// Parse data json
let book_categories_array_parse = null;
if (book_categories_array) {
book_categories_array_parse = JSON.parse(book_categories_array);
}

const book_id = RANDOMS.createID();
const data_insert = {
book_id: RANDOMS.createID(),
book_id,
name,
author_id,
image_uri,
Expand All @@ -69,16 +88,33 @@ const bookController = {
// create book database
let err;
let result;
let result_insert_book_categories;
[err, result] = await HELPER.handleRequest(book_model.createBook(data_insert));
if (result) {
if (book_categories_array_parse) {
result_insert_book_categories = await book_categories_admin_service.handleSaveMultiBookCategories(
book_id,
book_categories_array_parse,
null,
);
}
// Del key Redis
MEMORY_CACHE.delKeyCache(CONSTANTS.KEY_REDIS.ALL_BOOK);

return res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({
status: CONSTANTS.HTTP.STATUS_2XX_OK,
message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK),
element: {
result: result[0].book_id,
result: {
insert_book: result[0].book_id,
// eslint-disable-next-line no-nested-ternary

insert_book_categories:
result_insert_book_categories === undefined
? null
: result_insert_book_categories
? MESSAGES.GENERAL.SERVER_INSERT_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
},
},
});
}
Expand All @@ -101,6 +137,7 @@ const bookController = {
/**
* @author Nguyễn Tiến Tài
* @created_at 03/02/2023
* @updated_at 17/04/2023
* @description update book
* @function updateBook
* @return {Object:{Number,String}}
Expand All @@ -118,6 +155,7 @@ const bookController = {
status,
page_number,
public_id_image,
book_categories_array,
} = req.body.input.book_input;

// Check input
Expand Down Expand Up @@ -145,6 +183,11 @@ const bookController = {
},
});
}
// Parse data json
let book_categories_array_parse = null;
if (book_categories_array) {
book_categories_array_parse = JSON.parse(book_categories_array);
}
const book = await book_model.getBookById({
book_id,
isdeleted: CONSTANTS.DELETED_DISABLE,
Expand Down Expand Up @@ -176,9 +219,20 @@ const bookController = {
real_quantity: Number(book[0].quantity) + Number(quantity_update),
};
try {
// Check Student tow a borrow book
// Get data book_categories
const book_categories = await book_category_model.getAllBookCategories(
{
book_id,
isdeleted: CONSTANTS.DELETED_DISABLE,
},
'*',
);
// update book database
let err;
let result;
let result_insert_book_categories;

[err, result] = await HELPER.handleRequest(
book_model.updateBook(
data_update,
Expand All @@ -187,6 +241,14 @@ const bookController = {
),
);
if (result) {
if (book_categories_array_parse) {
// insert book categories array
result_insert_book_categories = await book_categories_admin_service.handleSaveMultiBookCategories(
book_id,
book_categories_array_parse,
book_categories,
);
}
// Create key Cache
const key_cache_book_detail = HELPER.getURIFromTemplate(CONSTANTS.KEY_REDIS.DETAIL_BOOK, {
book_id,
Expand All @@ -199,7 +261,17 @@ const bookController = {
status: CONSTANTS.HTTP.STATUS_2XX_OK,
message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK),
element: {
result: result[0].book_id,
result: {
insert_book: result[0].book_id,
// eslint-disable-next-line no-nested-ternary

insert_book_categories:
result_insert_book_categories === undefined
? null
: result_insert_book_categories
? MESSAGES.GENERAL.SERVER_UPDATE_FAIL
: MESSAGES.GENERAL.SERVER_CURD_SUCCESS,
},
},
});
}
Expand Down Expand Up @@ -242,12 +314,17 @@ const bookController = {
try {
// Check account already delete
const result_book_detail = await book_model.getBookById(
{ book_id, isdeleted: CONSTANTS.DELETED_ENABLE },
{ book_id: 'book_id' },
{
book_id,
isdeleted: CONSTANTS.DELETED_ENABLE,
},
{
book_id: 'book_id',
},
);

// Check Book already delete
if (result_book_detail.length > 0) {
if (result_book_detail.length > CONSTANTS.ARRAY.EMPTY) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST),
Expand All @@ -256,6 +333,7 @@ const bookController = {
},
});
}

// Delete book, borrow, favorite database
let err;
let result;
Expand Down
8 changes: 8 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,12 @@ module.exports = {
STRING_DELETE_FLAG: {
VALUE_TIME: '${value}_deleted_${time}',
},
/**
* @author Nguyễn Tiến Tài
* @created_at 17/04/2023
* @description Array empty
*/
ARRAY: {
EMPTY: 0,
},
};
5 changes: 4 additions & 1 deletion backend-manager-student/src/share/configs/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ module.exports = {
EXPIRED_SESSION_TOKEN: 'Login session expired',

// ? SERVER
SERVER_OUT_OF_SERVICE: 'Out Of Service',
SERVER_OUT_OF_SERVICE: 'Out Of Service!',
SERVER_INSERT_FAIL: 'Insert Fail!',
SERVER_UPDATE_FAIL: 'Update Fail!',
SERVER_CURD_SUCCESS: 'Success!',

// ? SUCCESS
SUCCESS_CHANGE_PASSWORD: 'Change Password Success!',
Expand Down
9 changes: 8 additions & 1 deletion backend-manager-student/src/share/models/book.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module.exports = {
/**
* @author Nguyễn Tiến Tài
* @created_at 14/04/2023
* @updated_at 17/04/2023
* @description Transaction Delete Book
*/
transactionDeleteBook: async (data, student_query, return_data) =>
Expand All @@ -115,8 +116,14 @@ module.exports = {
// Query 3: updateFavorite
const updateFavorite = trx('favorite_book').update(data).where(student_query).returning(return_data);

// Query 4: updateBookCategories
const updateBookCategories = trx('book_categories')
.update(data)
.where(student_query)
.returning(return_data);

// Run Sequential async function
Promise.all([updatedBook, updateBorrowBook, updateFavorite])
Promise.all([updatedBook, updateBorrowBook, updateFavorite, updateBookCategories])
.then((final_rs) => {
// Commit transaction
trx.commit();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! SHARE
const RANDOMS = require('../../utils/random');
const CONSTANTS = require('../../configs/constants');

//! MODEL
const book_category_model = require('../../models/book_categories.model');

module.exports = {
/**
* @author Nguyễn Tiến Tài
* @created_at 17/04/2023
* @description handle save book categories
* @function handleSetCacheRedis
*/
handleSaveMultiBookCategories: async (book_id, book_categories_array_parse, book_categories) => {
try {
// Init data filter
let data_filter;

// if book_categories deference empty
if (book_categories) {
data_filter = book_categories_array_parse.filter(
(item) => !book_categories.some((existingItem) => existingItem.category_id === item.category_id),
);
} else {
// if book_categories null
// eslint-disable-next-line no-unused-vars
data_filter = book_categories_array_parse;
}
// data_filter equal array empty
if (Array.isArray(data_filter) && data_filter.length === CONSTANTS.ARRAY.EMPTY) {
return false;
} else {
// Take data in array save database
for (const data of data_filter) {
// create Category database
book_category_model.createBookCategories({
book_categories_id: RANDOMS.createID(),
book_id,
category_id: data.category_id,
});
}
// Save database success
return false;
}
} catch (error) {
// Save database error
return true;
}
},
};

0 comments on commit d057b1a

Please sign in to comment.