From 584946c19ee626445cfdccd9c7d773f99fa8abce 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: Sun, 16 Apr 2023 17:17:33 +0700 Subject: [PATCH] #259 [Backend] change spec borrow_book and book --- .../admin_api/book_categories.swagger.yaml | 6 +- .../book_categories.controller.js | 76 +++++++++++++++---- .../src/share/configs/message.js | 1 + 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/backend-manager-student/src/admin_api/v1/Swagger/admin_api/book_categories.swagger.yaml b/backend-manager-student/src/admin_api/v1/Swagger/admin_api/book_categories.swagger.yaml index d1079ea..62adc56 100644 --- a/backend-manager-student/src/admin_api/v1/Swagger/admin_api/book_categories.swagger.yaml +++ b/backend-manager-student/src/admin_api/v1/Swagger/admin_api/book_categories.swagger.yaml @@ -132,9 +132,9 @@ paths: book_id: type: number example: 13456789 - category_id: - type: number - example: 13456789 + book_categories_array: + type: string + example: '[{"category_id":"1475794147142008836"},{"category_id":"1475794147142008835"}]' required: - book_id - category_id diff --git a/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js b/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js index ea21146..e5a228c 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js @@ -19,10 +19,13 @@ const bookCategoriesController = { * @return {Object:{Number,String}} */ InsertBookCategory: async (req, res) => { - const { book_id, category_id } = req.body.input.book_categories_input; + const { book_id, book_categories_array } = req.body.input.book_categories_input; + + // Parse data json + let book_categories_array_parse = JSON.parse(book_categories_array); // Check input - if (!HELPER.validateBigInt(book_id) || !HELPER.validateBigInt(category_id)) { + if (!book_categories_array_parse || !HELPER.validateBigInt(book_id)) { 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), @@ -31,33 +34,74 @@ const bookCategoriesController = { }, }); } + try { - // create Category database - let err; - let result; - [err, result] = await HELPER.handleRequest( - book_category_model.createBookCategories({ - book_categories_id: RANDOMS.createID(), + // Get data book_categories + const book_categories = await book_category_model.getAllBookCategories( + { book_id, - category_id, - }), + isdeleted: CONSTANTS.DELETED_DISABLE, + }, + '*', ); - if (result) { - return res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({ - status: CONSTANTS.HTTP.STATUS_2XX_OK, - message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK), + // Take data filter + let data_filter; + + if (book_categories) { + data_filter = book_categories_array_parse.filter( + (item) => !book_categories.some((existingItem) => existingItem.category_id === item.category_id), + ); + } else { + // eslint-disable-next-line no-unused-vars + data_filter = book_categories_array_parse; + } + if (Array.isArray(data_filter) && data_filter.length === 0) { + 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), element: { - result: result[0].book_categories_id, + result: MESSAGES.GENERAL.ALREADY_CATEGORIES_BOOK, }, }); } - if (err) { + let successResults = []; + let errorResults = []; + for (const data of data_filter) { + // create Category database + let err; + let result; + [err, result] = await HELPER.handleRequest( + book_category_model.createBookCategories({ + book_categories_id: RANDOMS.createID(), + book_id, + category_id: data.category_id, + }), + ); + if (result) { + successResults.push(result[0].book_categories_id); + } + if (err) { + errorResults.push(err); + } + } + + if (errorResults.length > 0) { return res.status(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR).json({ status: CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR, message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR), + error: errorResults, }); } + + return res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({ + status: CONSTANTS.HTTP.STATUS_2XX_OK, + message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK), + element: { + result: successResults, + }, + }); } catch (error) { + console.error(error, '--------error------'); return res.status(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE).json({ status: CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE, message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE), diff --git a/backend-manager-student/src/share/configs/message.js b/backend-manager-student/src/share/configs/message.js index d3064f0..27fbc37 100644 --- a/backend-manager-student/src/share/configs/message.js +++ b/backend-manager-student/src/share/configs/message.js @@ -59,6 +59,7 @@ module.exports = { ALREADY_FAVORITE: 'Favorite already exits!', ALREADY_ACCOUNT_STUDENT: 'Account student already exits !', ALREADY_AUTHOR_BOOK: 'Please delete book before delete author !', + ALREADY_CATEGORIES_BOOK: 'Categories already add database !', // ? PLEASE PLEASE_REFUND_BOOK: 'Please refund the book !!',