Skip to content

Commit

Permalink
Merge pull request #24 from hossainchisty/improve/api
Browse files Browse the repository at this point in the history
perf: Add validation message for better user experiences
  • Loading branch information
hossainchisty authored Oct 15, 2023
2 parents 87a23dd + 89cf79a commit c9afd58
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
32 changes: 32 additions & 0 deletions src/app/module/book/book.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 29 additions & 10 deletions src/app/module/book/book.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -148,6 +149,24 @@ exports.deleteBook = async (bookId) => {
}
};

/**
* @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);
}
};

/**
* Updates the reading status of a book.
*
Expand All @@ -163,4 +182,4 @@ exports.updateReadingStatus = async (bookId, readingStatus) => {
} catch (error) {
throw new Error('Error updating reading status');
}
};
};

0 comments on commit c9afd58

Please sign in to comment.