Skip to content

expressBookReviews Final Project #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions final_project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@ const app = express();

app.use(express.json());

app.use("/customer",session({secret:"fingerprint_customer",resave: true, saveUninitialized: true}))
app.use("/customer",session(
{
secret:"fingerprint_customer",
resave: true,
saveUninitialized: true}
));

app.use("/customer/auth/*", function auth(req,res,next){

//Write the authenication mechanism here
if(req.session.authorization){
let token = req.session.authorization['accessToken'];

// verify the token
jwt.verify(token, 'access' , (err,user)=>{
if(!err){
req.user = user;
next();
}
return res.status(403).json({message:'User is not authenticated'});
});

} else {
return res.status(403).json({message: 'User not logged in'});
}
});

const PORT =5000;
const PORT = 5000;

app.use("/customer", customer_routes);
app.use("/", genl_routes);
Expand Down
83 changes: 76 additions & 7 deletions final_project/router/auth_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,93 @@ let users = [];

const isValid = (username)=>{ //returns boolean
//write code to check is the username is valid
}
let userwithsamename = users.filter(user=>{
return user.username === username;
});
if(userwithsamename.length > 0){
return true;
}
else{
return false;
}
};

const authenticatedUser = (username,password)=>{ //returns boolean
//write code to check if username and password match the one we have in records.
}
let validateUser = users.filter(user => {
return (user.username == username && user.password === password);
});
if(validateUser.length > 0){
return true;
} else{
return false;
}
};

//only registered users can login
regd_users.post("/login", (req,res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const {username,password} = req.body;

if(!username || !password){
return res.status(404).json({message:'Error loggin in'});
}

if(authenticatedUser(username,password)){
// Generate the JWT Token
const accessToken = jwt.sign({data:password}, 'access', {expiresIn: 60 * 60});

req.session.authorization = {
accessToken,
username
}
return res.status(200).json({message:'User logged in successfully'});
} else {
return res.status(208).json({message:'Invalid Login. Check username and password or Register'})
}

});

// Add a book review
// Add a book review or modify
regd_users.put("/auth/review/:isbn", (req, res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const {review} = req.query;
const isbn = req.params.isbn;
const username = req.session.authorization['username'];

if(!review){
return res.status(400).json({message:'Review is required'});
}

if(!books[isbn]){
return res.status(404).json({message:'Book not found'});
}

books[isbn].reviews[username] = review;

return res.status(200).json({message:'Book Review added or updated successfully', reviews: books[isbn].reviews});

});



// Delete a book review
regd_users.delete("/auth/review/:isbn", (req,res) => {
const isbn = req.params.isbn;
const username = req.session.authorization['username'];

if(!books[isbn]){
return res.status(404).json({message:'Book not found'});
}

if(!books[isbn].reviews[username]){
return res.status(404).json({message:'No review found to delete'});
}

delete books[isbn].reviews[username];
return res.status(200).json({message:'Book Review deleted successfully', reviews: books[isbn].reviews});
});



module.exports.authenticated = regd_users;
module.exports.isValid = isValid;
module.exports.users = users;
155 changes: 141 additions & 14 deletions final_project/router/general.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,170 @@
const express = require('express');
const axios = require("axios");
let books = require("./booksdb.js");
let isValid = require("./auth_users.js").isValid;
let users = require("./auth_users.js").users;
const public_users = express.Router();


public_users.post("/register", (req,res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const {username, password} = req.body;

if(username && password){
if(!isValid(username)){
users.push({'username':username, 'password': password});
return res.status(201).json({message:'User Registered successfully. Now you can login.'})
}
else{
return res.status(404).json({message:'User already exists'})
}
}
return res.status(404).json({message:'Unable register the user'});
});

// Get the book list available in the shop
// // Get the book list available in the shop
public_users.get('/',function (req, res) {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
res.send(JSON.stringify(books,null,4));
});



// Task-10
// Get the book list available in the shop using async/await
public_users.get('/' , async (req,res) => {
try {
const allBooks = await new Promise((resolve,reject)=>{
if(books){
resolve(books);
} else{
reject(new Error('Books data not available'));
}
});
return res.status(200).json(allBooks);
} catch (error) {
res.status(500).json({message:'Error retrieving books', error: error.message});
}
});


// Get book details based on ISBN
public_users.get('/isbn/:isbn',function (req, res) {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
});
public_users.get("/isbn/:isbn", function (req, res) {
const isbn = req.params.isbn;

if(books[isbn]){
res.send(books[isbn]);
} else{
return res.status(404).json({message:'Book not found'});
}

});


// Task-11
// Get book details based on ISBN
public_users.get('/isbn/:isbn', async (req,res) => {
const isbn = req.params.isbn;

new Promise((resolve,reject) => {
const book = books[isbn];
if(book){
resolve(book);
} else {
reject('Book not found');
}
})
.then((book) => {
res.status(200).json(book)
})
.catch((error)=>{
res.status(404).json({message:error})
})

});


// Get book details based on author
public_users.get('/author/:author',function (req, res) {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const author = req.params.author;
const isMatch = Object.values(books).filter(book => book.author === author);
if(isMatch.length > 0){
return res.json(isMatch);
}
else{
return res.status(404).json({message:'Book not found'})
}
});



// Task 12
// Get book details based on author
public_users.get('/author/:author', function(req,res) {
const author = req.params.author;

new Promise((resolve,reject) => {
const isMatch = Object.values(books).filter(book => book.author === author);
if(isMatch.length > 0){
resolve(isMatch)
} else {
reject('Book not found for the author')
}
})
.then((isMatch)=>{
res.status(200).json(isMatch);
})
.catch((error)=>{
res.status(404).json({message:error});
})
});


// Get all books based on title
public_users.get('/title/:title',function (req, res) {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const title = req.params.title;
const istitle = Object.values(books).filter(book => book.title === title);
if(istitle.length > 0){
return res.status(200).json(istitle);
}
else{
return res.status(404).json({message:'Book Not found'});
}
});


// Task 13
// Get all books based on title
public_users.get('/title/:title', function(req,res){
const title = req.params.title;

new Promise((resolve,reject) => {
const istitle = Object.values(books).filter(book => book.title === title);
if(istitle.length > 0){
resolve(istitle);
} else{
reject('Book not found for the title');
}
})
.then((istitle)=>{
return res.status(200).json(istitle);
})
.catch((error)=>{
return res.status(404).json({message:error});
})
});


// Get book review
public_users.get('/review/:isbn',function (req, res) {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const isbn = req.params.isbn;
if(books[isbn]){
const bookDetails = books[isbn];
const review = bookDetails.reviews;
return res.status(200).json(review);
}
else{
return res.status(404).json({message:'Reviews not found for this book'});
}
});

module.exports.general = public_users;