From d304704015777ea76d9ecdd4441db2d96c716f97 Mon Sep 17 00:00:00 2001 From: hossainchisty Date: Sun, 15 Oct 2023 16:42:55 +0600 Subject: [PATCH] perf: Add vakudatuib nessage for better exps --- src/app/module/book/book.controller.js | 32 ++++++++++++++++++++++ src/app/module/book/book.services.js | 37 +++++++++++++++++++------- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/app/module/book/book.controller.js b/src/app/module/book/book.controller.js index 520162d..2890f05 100644 --- a/src/app/module/book/book.controller.js +++ b/src/app/module/book/book.controller.js @@ -97,6 +97,38 @@ exports.addBook = asyncHandler(async (req, res, next) => { inStock = false; } + // Validate Fields + if (!title) { + return sendResponse(res, 400, false, 'Title is required'); + } + + if (!description) { + return sendResponse(res, 400, false, 'Description is required'); + } + + if (!price || isNaN(price) || price <= 0) { + return sendResponse(res, 400, false, 'Price must be a positive number'); + } + + if (!shippingFees || isNaN(shippingFees) || shippingFees < 0) { + return sendResponse( + res, + 400, + false, + 'Shipping fees must be a non-negative number' + ); + } + + // Check if ISBN already exists in the database + const existingBook = await bookService.getBookByISBN(ISBN); + if (existingBook) { + return sendResponse( + res, + 400, + false, + 'Book with this ISBN already exists' + ); + } // Prepare book data object const bookData = { title, diff --git a/src/app/module/book/book.services.js b/src/app/module/book/book.services.js index d4c4fb0..c76f76a 100644 --- a/src/app/module/book/book.services.js +++ b/src/app/module/book/book.services.js @@ -12,7 +12,7 @@ const Book = require('../book/book.model'); * const books = await getBooksByUserId(userId); * // books: [{...}, {...}, ...] */ -exports.getBooksByUserId = async (userId) => { +exports.getBooksByUserId = async userId => { try { // Fetch books from the database for the given user ID, sorting them by creation date. const books = await Book.find({ user: userId }).sort({ createdAt: -1 }); @@ -51,17 +51,18 @@ exports.getBooksList = async (page = 1) => { * @returns {Object} Retrieved book details. * @throws {Error} Throws an error if there's an issue fetching the book data. */ -exports.getBookByID = async (bookId) => { +exports.getBookByID = async bookId => { try { const book = await Book.findByIdAndUpdate( bookId, { $inc: { read: 1 } }, { new: true } - - ).populate({ - path: 'reviews.user', - select: 'full_name avatar', - }).lean(); + ) + .populate({ + path: 'reviews.user', + select: 'full_name avatar', + }) + .lean(); if (!book) { throw new Error('Book not found'); } @@ -98,7 +99,7 @@ exports.searchBooks = async (title, author) => { * @returns {Object} Created book details. * @throws {Error} Throws an error if there's an issue creating the book. */ -exports.createBook = async (bookData) => { +exports.createBook = async bookData => { try { const book = await Book.create(bookData); return book; @@ -136,7 +137,7 @@ exports.updateBook = async (bookId, updatedData) => { * @returns {Object} Deleted book details. * @throws {Error} Throws an error if there's an issue deleting the book. */ -exports.deleteBook = async (bookId) => { +exports.deleteBook = async bookId => { try { const book = await Book.findByIdAndRemove(bookId, { new: true }); if (!book) { @@ -147,3 +148,21 @@ exports.deleteBook = async (bookId) => { throw new Error('Error deleting book'); } }; + +/** + * @function getBookByISBN + * @description Retrieves a book from the database based on its ISBN. + * + * @param {string} ISBN - The ISBN of the book to retrieve. + * @returns {Promise} A promise that resolves to the found book or null if not found. + * @throws Will throw an error if there's an issue with the database query. + */ +exports.getBookByISBN = async ISBN => { + try { + // Use the Book model to find a book with the specified ISBN in the database + const book = await Book.findOne({ ISBN: ISBN }).exec(); + return book; + } catch (error) { + throw new Error('Error retrieving book by ISBN: ' + error.message); + } +};