From d057b1aefc37f272f0c065bfb5c34bf903002a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Ti=E1=BA=BFn=20T=C3=A0i?= <63393170+fdhhhdjd@users.noreply.github.com> Date: Mon, 17 Apr 2023 20:40:50 +0700 Subject: [PATCH] #259 [Backend] change spec borrow_book and book --- backend-manager-student/.eslintrc.js | 1 + .../book.controllers/book.controller.js | 114 +++++++++++++++--- .../src/share/configs/constants.js | 8 ++ .../src/share/configs/message.js | 5 +- .../src/share/models/book.model.js | 9 +- .../admin_service/book_categories.service.js | 51 ++++++++ 6 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 backend-manager-student/src/share/services/admin_service/book_categories.service.js diff --git a/backend-manager-student/.eslintrc.js b/backend-manager-student/.eslintrc.js index d4a750b..98ba9a9 100644 --- a/backend-manager-student/.eslintrc.js +++ b/backend-manager-student/.eslintrc.js @@ -68,5 +68,6 @@ module.exports = { 'no-unused-expressions': 'off', 'implicit-arrow-linebreak': 'off', 'object-curly-newline': 'off', + 'no-nested-ternary': 'off', }, }; diff --git a/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js b/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js index 8741c87..222c544 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js @@ -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 = { /** @@ -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, @@ -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, @@ -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, + }, }, }); } @@ -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}} @@ -118,6 +155,7 @@ const bookController = { status, page_number, public_id_image, + book_categories_array, } = req.body.input.book_input; // Check input @@ -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, @@ -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, @@ -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, @@ -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, + }, }, }); } @@ -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), @@ -256,6 +333,7 @@ const bookController = { }, }); } + // Delete book, borrow, favorite database let err; let result; diff --git a/backend-manager-student/src/share/configs/constants.js b/backend-manager-student/src/share/configs/constants.js index 24a05ad..53bd2dd 100644 --- a/backend-manager-student/src/share/configs/constants.js +++ b/backend-manager-student/src/share/configs/constants.js @@ -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, + }, }; diff --git a/backend-manager-student/src/share/configs/message.js b/backend-manager-student/src/share/configs/message.js index ee1f52b..aba595d 100644 --- a/backend-manager-student/src/share/configs/message.js +++ b/backend-manager-student/src/share/configs/message.js @@ -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!', diff --git a/backend-manager-student/src/share/models/book.model.js b/backend-manager-student/src/share/models/book.model.js index 8f81869..f8c7264 100644 --- a/backend-manager-student/src/share/models/book.model.js +++ b/backend-manager-student/src/share/models/book.model.js @@ -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) => @@ -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(); diff --git a/backend-manager-student/src/share/services/admin_service/book_categories.service.js b/backend-manager-student/src/share/services/admin_service/book_categories.service.js new file mode 100644 index 0000000..9ad2891 --- /dev/null +++ b/backend-manager-student/src/share/services/admin_service/book_categories.service.js @@ -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; + } + }, +};