-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
8,060 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const User = require('../models/user') | ||
const passport = require("passport"); | ||
|
||
const registerUser = async (req, res) => { | ||
User.findOne({email: req.body.email}, (err, user) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
if (user) { | ||
return res.status(403).json({success: false, message: "User already exists"}); | ||
} else { | ||
const newUser = new User(req.body); | ||
newUser.setPassword(req.body.password); | ||
newUser.save((err, user) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
return res.status(201).json({ | ||
success: true, | ||
user | ||
}); | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
const loginUser = async (req, res, next) => { | ||
User.findOne({email: req.body.email}, (err, user) => { | ||
if (err) { | ||
return res.status(500).json({success: false, err}); | ||
} | ||
if (!user) { | ||
return res.status(404).json({success: false, message: "User not found"}); | ||
} | ||
if (!user.isValidPassword(req.body.password)) { | ||
return res.status(401).json({success: false, message: "Password incorrect"}); | ||
} | ||
passport.authenticate("local", (err, user, info) => { | ||
req.logIn(user, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
return res.status(200).json({ | ||
success: true, | ||
user | ||
}); | ||
}); | ||
},)(req, res, next); | ||
}) | ||
} | ||
|
||
const logoutUser = async (req, res, next) => { | ||
req.logout((err) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
// res.redirect('/login'); | ||
}); | ||
return res.status(200).json({success: true, message: "User logged out"}); | ||
} | ||
|
||
module.exports = { | ||
registerUser, | ||
loginUser, | ||
logoutUser | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const Author = require('../models/author') | ||
|
||
|
||
//read | ||
const getAuthor = async (req, res) => { | ||
const authorId = req.params.id; | ||
|
||
Author.findById(authorId, (err, author) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
author | ||
}); | ||
}); | ||
} | ||
|
||
const getAllAuthors = async (req, res) => { | ||
Author.find({}, (err, authors) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
authorsList: authors | ||
}); | ||
}) | ||
} | ||
|
||
//create | ||
const addAuthor = async (req, res) => { | ||
const newAuthor = req.body | ||
|
||
Author.create(newAuthor, (err, author) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
newAuthor: author | ||
}); | ||
}) | ||
} | ||
|
||
//update | ||
const updateAuthor = async (req, res) => { | ||
const authorId = req.params.id | ||
const updatedAuthor = req.body | ||
|
||
Author.findByIdAndUpdate(authorId, updatedAuthor, (err, author) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
updatedAuthor: author | ||
}); | ||
}) | ||
} | ||
|
||
|
||
//delete | ||
const deleteAuthor = async (req, res) => { | ||
const authorId = req.params.id | ||
|
||
Author.findByIdAndDelete(authorId, (err, author) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
deletedAuthor: author | ||
}); | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getAuthor, | ||
getAllAuthors, | ||
addAuthor, | ||
updateAuthor, | ||
deleteAuthor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const Book = require('../models/book') | ||
const mongoose = require("mongoose"); | ||
|
||
const getBook = async (req, res) => { | ||
const bookId = req.params.id; | ||
|
||
Book.findById(bookId, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
book | ||
}); | ||
}); | ||
} | ||
|
||
const getAllBooks = async (req, res) => { | ||
Book.aggregate([{ | ||
$lookup: { | ||
from: "authors", | ||
localField: "authorId", | ||
foreignField: "_id", | ||
as: "author" | ||
}, | ||
}, | ||
{ | ||
$unwind: "$author" | ||
}, | ||
{ | ||
$lookup: { | ||
from: "genres", | ||
localField: "genreId", | ||
foreignField: "_id", | ||
as: "genre" | ||
}, | ||
|
||
}, | ||
{ | ||
$unwind: "$genre" | ||
},]).exec((err, books) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
booksList: books | ||
}); | ||
}); | ||
} | ||
|
||
const addBook = async (req, res) => { | ||
const newBook = { | ||
...req.body, | ||
genreId: mongoose.Types.ObjectId(req.body.genreId), | ||
authorId: mongoose.Types.ObjectId(req.body.authorId) | ||
} | ||
console.log(newBook) | ||
Book.create(newBook, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
newBook: book | ||
}); | ||
}) | ||
} | ||
|
||
const updateBook = async (req, res) => { | ||
const bookId = req.params.id | ||
const updatedBook = req.body | ||
|
||
Book.findByIdAndUpdate(bookId, updatedBook, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
updatedBook: book | ||
}); | ||
}) | ||
} | ||
|
||
const deleteBook = async (req, res) => { | ||
const bookId = req.params.id | ||
|
||
Book.findByIdAndDelete(bookId, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
deletedBook: book | ||
}); | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getBook, | ||
getAllBooks, | ||
addBook, | ||
updateBook, | ||
deleteBook | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const bookReturn = require('../models/bookReturn') | ||
const mongoose = require("mongoose"); | ||
const Book = require("../models/book"); | ||
|
||
const getReturn = async (req, res) => { | ||
const returnId = req.params.id; | ||
|
||
bookReturn.findById(returnId, (err, returnBook) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
returnBook | ||
}); | ||
}); | ||
} | ||
|
||
const getAllReturns = async (req, res) => { | ||
bookReturn.aggregate([{ | ||
$lookup: { | ||
from: "users", | ||
localField: "memberId", | ||
foreignField: "_id", | ||
as: "member" | ||
}, | ||
}, | ||
{ | ||
$unwind: "$member" | ||
},]).exec((err, returns) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
returnsList: returns | ||
}); | ||
}) | ||
} | ||
|
||
const addReturn = async (req, res) => { | ||
const newReturn = { | ||
...req.body, | ||
memberId: mongoose.Types.ObjectId(req.body.memberId), | ||
bookId: mongoose.Types.ObjectId(req.body.bookId) | ||
} | ||
|
||
bookReturn.create(newReturn, (err, returnBook) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
Book.findByIdAndUpdate(newReturn.bookId, {isAvailable: false}, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
newReturn: returnBook | ||
}); | ||
}); | ||
}) | ||
} | ||
|
||
const updateReturn = async (req, res) => { | ||
const returnId = req.params.id | ||
const updatedReturn = req.body | ||
|
||
bookReturn.findByIdAndUpdate(returnId,updatedReturn, (err, returnBook) => { | ||
if (err) { | ||
return res.status(400).json({ success: false, err }); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
updatedReturn: returnBook | ||
}); | ||
}) | ||
} | ||
|
||
const deleteReturn = async (req, res) => { | ||
const returnId = req.params.id | ||
|
||
bookReturn.findByIdAndDelete(returnId, (err, returnBook) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
Book.findByIdAndUpdate(returnBook.bookId, {isAvailable: true}, (err, book) => { | ||
if (err) { | ||
return res.status(400).json({success: false, err}); | ||
} | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
deletedReturn: returnBook | ||
}); | ||
}); | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getReturn, | ||
getAllReturns, | ||
addReturn, | ||
updateReturn, | ||
deleteReturn | ||
} |
Oops, something went wrong.