Skip to content

Commit

Permalink
Controllers Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
shaghayeghng committed Aug 2, 2021
1 parent a02c8aa commit fba8dbd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 35 deletions.
75 changes: 51 additions & 24 deletions controllers/book-bookShelvesController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const book_bookShelvesModel = require("../models/book-bookShelves");
const bookShelvesModel = require("../models/bookShelvesModel");

const catchAsync = require("../util/catchAsync");
const AppError = require("../util/appError");

//get all books of a book shelf
exports.getBooksOfBookShelf = catchAsync(async (req, res, next) => {
const { bookshelfID } = req.params;
if (!bookshelfID) {
Expand All @@ -23,43 +25,68 @@ exports.getBooksOfBookShelf = catchAsync(async (req, res, next) => {
});
});

//add list of books to book shelf
exports.addBooksToBookShelf = catchAsync(async (req, res, next) => {

const { bookshelfID } = req.params;
if (!bookshelfID) {
return next(
new AppError("Request is not providing the book shelf ID!", 400)
);
}
const newBooks = await book_bookShelvesModel.create({
books: req.body.books,
bookShelves: req.body.bookShelves,
bookShelves: bookshelfID,
});

const bookShelf = await bookShelvesModel.findById(bookshelfID);
bookShelf.updateNumOfBooks(bookShelf, req.body.books.length);

res.status(201).json({
status: "Success",
newBooks,
});
});

//delete a book from a book shelf
exports.deleteBookOfBookShelf = catchAsync(async (req, res, next) => {
const { bookshelfID } = req.params;
if (!bookshelfID) {
return next(
new AppError("Request is not providing the book shelf ID!", 400)
);
}

//todo: update the number of books in bookshelf
const bookShelf = await book_bookShelvesModel.find({
bookShelves: bookshelfID,
});
if (!bookShelf) {
return next(new AppError("No books found in this book shelf!", 404));
}

const bookshelfI = bookShelf.findIndex((bs) =>
bs.books.find((b) => b.bookISBN === req.body.book.bookISBN)
);

if (bookshelfI < 0) {
return next(new AppError("Book Not found in this book shelf!", 404));
}

const bookI = bookShelf[bookshelfI].books.findIndex(
(b) => b.bookISBN === req.body.book.bookISBN
);

//! delete books of bookshelf
// exports.deleteBooksOfBookShelf = catchAsync(async (req, res, next) => {
// const { bookShelfID } = req.params;
// if (!bookShelfID) {
// return next(
// new AppError("Request is not providing the book shelf ID!", 400)
// );
// }
bookShelf[bookshelfI].books = bookShelf[bookshelfI].books.slice(
bookI + 1,
bookI + 2
);

// const bookShelf = await book_bookShelvesModel.find({
// bookShelves: bookShelfID,
// });
// if (!bookShelf) {
// return next(new AppError("No books found in this book shelf!", 404));
// }
bookShelf[bookshelfI].save();

// if (bookShelf.books.includes(req.body.book)) {
// }
const updateBookshelf = await bookShelvesModel.findById(bookshelfID);
updateBookshelf.updateNumOfBooks(updateBookshelf, -1);

// res.status(200).json({
// status: "Success",
// bookShelves,
// });
// });
res.status(200).json({
status: "Success",
bookShelf,
});
});
1 change: 0 additions & 1 deletion controllers/book-libraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ exports.getBooksOfLibrary = catchAsync(async (req, res, next) => {
});

exports.addBooksToLibrary = catchAsync(async (req, res, next) => {

const newBooks = await book_libraryModel.create({
bookID: req.body.bookID,
library: req.body.library,
Expand Down
1 change: 1 addition & 0 deletions controllers/bookController.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ exports.addBook = catchAsync(async (req, res, next) => {
});
});

//update the status of a book (available / not available)
exports.updateStatus = catchAsync(async (req, res, next) => {
const { bookID } = req.params;
if (!bookID) {
Expand Down
18 changes: 14 additions & 4 deletions controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ exports.createOrder = catchAsync(async (req, res, next) => {
exports.deleteOrder = catchAsync(async (req, res, next) => {
const { bookID } = req.params;
if (!bookID) {
return next(
new AppError("Request is not providing the book ID!", 400)
);
return next(new AppError("Request is not providing the book ID!", 400));
}
const order = await OrderModel.deleteOne({ bookID: bookID });

Expand All @@ -59,5 +57,17 @@ exports.deleteOrder = catchAsync(async (req, res, next) => {
});
});

exports.removeAllOrders = catchAsync(async (req, res, next) => {
const order = await OrderModel.deleteMany();

if (!order) {
return next(new AppError("No order found!", 404));
}

req.user.clearCart(req.user);

//todo: delete all orders and empty cart
res.status(200).json({
status: "Success",
order,
});
});
2 changes: 1 addition & 1 deletion controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ exports.updateRole = catchAsync(async (req, res, body) => {
} else if (user.role === userRoles.STAFF) {
return res.status(200).json({
status: "Success",
message: "You are already a staff!",
message: "Already a staff!",
});
} else {
return res.status(200).json({
Expand Down
7 changes: 6 additions & 1 deletion models/bookShelvesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const bookShelvesSchema = new Schema(
numOfBooks: {
type: Number,
required: true,
default: 0
default: 0,
},
user: {
type: Schema.Types.ObjectId,
Expand All @@ -23,5 +23,10 @@ const bookShelvesSchema = new Schema(
}
);

bookShelvesSchema.methods.updateNumOfBooks = (bookShelf, num) => {
bookShelf.numOfBooks += num;
return bookShelf.save();
};

const bookShelves = mongoose.model("bookShelves", bookShelvesSchema);
module.exports = bookShelves;
8 changes: 6 additions & 2 deletions routes/bookShelvesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ router
.get(isLoggedIn, book_bookShelvesController.getBooksOfBookShelf);

router
.route("/add-to-bookshelf")
.route("/:bookshelfID/add-to-bookshelf")
.post(isLoggedIn, book_bookShelvesController.addBooksToBookShelf);
module.exports = router;

router
.route("/:bookshelfID/remove-from-bookshelf")
.patch(isLoggedIn, book_bookShelvesController.deleteBookOfBookShelf);

module.exports = router;
6 changes: 4 additions & 2 deletions routes/orderRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const orderController = require("./../controllers/orderController");

const router = express.Router();

router.route("/:bookID")
router.route("/remove-all").delete(isLoggedIn, orderController.removeAllOrders);

router
.route("/:bookID")
.get(isLoggedIn, orderController.getOrder)
.post(isLoggedIn, orderController.createOrder)
.delete(isLoggedIn, orderController.deleteOrder);

module.exports = router;

0 comments on commit fba8dbd

Please sign in to comment.