Skip to content

Commit

Permalink
added some features and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
alieutech committed Jan 10, 2025
1 parent 16537d3 commit 43436cd
Show file tree
Hide file tree
Showing 1,919 changed files with 3,102 additions and 356,959 deletions.
95 changes: 63 additions & 32 deletions backend/controllers/books.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,70 @@
const Books = require('../models/Books');


// Fetch all Books
const getBooks = async (req, res) => {
const books = await Books.find({});
const books = await Books.find();
if(!books) {
return res.status(401).json({'message': 'No books are found'})
return res.status(401).json({'message': 'No books are found' })
}
res.status(200).json({'success': 'All books are found'})
res.status(200).json({'success': 'All books are found', data: books})


};

// Create new Book
const createNewBook = async (req, res) => {
const { title, author, publishYear } = req.body;
if(!title || !author || !publishYear) {
const { title, author, publishYear, price, image } = req.body;
if(!title || !author || !publishYear || !price || !image) {
return res.status(401).json({'message': "title, author and year publish are required."})
}
try {
const book = await Books.create({
title: req.body.title,
author: req.body.author,
publishYear: req.body.publishYear,
price: req.body.price
price: req.body.price,
image: req.body.price
})
res.status(201).json(book);
} catch (err){
console.log(err.message);
}
}

// UPdate Book by ID
const updateBooks = async (req, res) => {
if(!req?.body?.id) {
return res.status(400).json({'message': 'Books ID is required'})
try {
const { id, title, author, publishYear, price, image } = req.body;

if (!req.body.id) {
return res.status(400).json({ message: 'Book ID is required.' });
}

const book = await Books.findById({ _id: req.body.id })
if (!book) {
return res.status(404).json({ message: `No book matches ID ${req.body.id}.` });
}

if (id) book.id = id;
if (title) book.title = title;
if (author) book.author = author;
if (publishYear) book.publishYear = publishYear;
if (price) book.price = price;
if (image) book.image = image;

const result = await book.save();

res.status(200).json({success: true, message: "Book updated successfully.", data: result,});
} catch (error) {
console.error("Error updating book:", error);
if (error.name === "CastError") {
return res.status(400).json({ message: "Invalid book ID format." });
}
res.status(500).json({ message: "An error occurred while updating the book." });
}
const book = await Books.findOne({ _id: req.body.id }).exec()
if(!book) {
return res.status(204).json({"message": `No book matches ID ${req.body.id} `})
}
if(req.body?.title) book.title = req.body.title;
if(req.body?.author) book.author = req.body.author;
if(req.body?.publishYear) book.publishYear = req.body.publishYear;
if(req.body?.price) book.price = req.body.price;

const result = await book.save()
res.json(result);
};

};

// Get Book by ID
const getBook = async (req, res) => {
if(!req?.params?.id) {
return res.status(400).json({'message': 'Books ID is required'})
Expand All @@ -58,21 +77,33 @@ const getBook = async (req, res) => {

}


// Delete Book by ID
const deleteBook = async (req, res) => {
if(!req?.body?.id) {
return res.status(400).json({'message': 'Book ID is required'})
}
const book = await Books.findOne({ _id: req.body.id }).exec()
if(!book) {
return res.status(204).json({"message": `No book matches ID ${req.body.id}`})
}
const result = await book.deleteOne({ _id: req.body.id })
res.json(result);
try {
const { id } = req.params;
if (!id) {
return res.status(400).json({ message: 'Book ID is required' });
}
const book = await Books.findById({_id: req.params.id});
if (!book) {
return res.status(404).json({ message: `No book matches ID ${req.params.id}` });
}

const result = await book.deleteOne();
return res.status(200).json({
success: true,
message: `Book with ID ${req.params.id} deleted successfully`,
data: result,
});
} catch (err) {
console.error('Error deleting book:', err);
return res.status(500).json({ success: false, message: 'Server error while deleting book' });
}
};






module.exports = { getBooks, createNewBook, updateBooks, getBook, deleteBook }
4 changes: 4 additions & 0 deletions backend/models/Books.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const books = new mongoose.Schema({
price: {
type: Number,
require: true
},
image: {
type: String,
require: true
}
})

Expand Down
1 change: 0 additions & 1 deletion backend/node_modules/.bin/mime

This file was deleted.

Loading

0 comments on commit 43436cd

Please sign in to comment.