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 16, 2023
1 parent 069012e commit 584946c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions backend-manager-student/src/share/configs/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !!',
Expand Down

0 comments on commit 584946c

Please sign in to comment.