Skip to content
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
Binary file modified .DS_Store
Binary file not shown.
Binary file added Screenshots/1-getallbooks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/2-gedetailsISBN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/3-getbooksbyauthor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/4-getbooksbytitle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/5-getbookreview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/6-register.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/7-login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/8-reviewadded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/9-deletereview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/task10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/task11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/task12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/task13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added final_project/.DS_Store
Binary file not shown.
16 changes: 15 additions & 1 deletion final_project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ app.use("/customer",session({secret:"fingerprint_customer",resave: true, saveUni

app.use("/customer/auth/*", function auth(req,res,next){
//Write the authenication mechanism here
if(req.session.authorization) {
let token = req.session.authorization['accessToken'];

jwt.verify(token, "access", (err, customer) => {
if(!err) {
req.customer = customer;
next();
} else {
return res.status(403).json({ message: "User not authenticated" });
}
});
} else {
return res.status(403).json({ message: "User not logged in" });
}
});

const PORT =5000;
const PORT =3333;

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

const isValid = (username)=>{ //returns boolean
//write code to check is the username is valid
// Filter the users array for any user with the same username
let userswithsamename = users.filter((user) => {
return user.username === username;
});
// Return true if any user with the same username is found, otherwise false
if (userswithsamename.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.
// Filter the users array for any user with the same username and password
let validusers = users.filter((user) => {
return (user.username === username && user.password === password);
});
// Return true if any valid user is found, otherwise false
if (validusers.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 = req.body.username;
const password = req.body.password;

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

if(authenticatedUser(username, password)) {
let accessToken = jwt.sign({
data: password
}, 'access', { expiresIn: 60 * 60})

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

});

// Add a book review
regd_users.put("/auth/review/:isbn", (req, res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const isbn = req.params.isbn; // ISBN aus der Route
const review = req.body.review; // Rezension aus dem Request-Body
const username = req.session.authorization.username; // Benutzername aus der Session

if (!username) {
return res.status(401).send("User is not logged in.");
}

if (!books[isbn]) {
return res.status(404).send("Book not found!");
}

if (!review) {
return res.status(400).send("Review content is required.");
}

// Sicherstellen, dass das 'reviews'-Objekt existiert
if (!books[isbn].reviews) {
books[isbn].reviews = {};
}

// Benutzerrezension hinzufügen oder aktualisieren
books[isbn].reviews[username] = review;

res.send({
message: `Review by ${username} for book with ISBN ${isbn} has been added/updated.`,
reviews: books[isbn].reviews // Alle aktuellen Rezensionen zurückgeben
});
});

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

if (!username) {
return res.status(401).send("User is not logged in.");
}

if (!books[isbn]) {
return res.status(404).send("Book not found!");
}

books = Object.values(books).filter(books => books.isbn === isbn);

res.send(`Reviews from User ${username} deleted`);
});

module.exports.authenticated = regd_users;
Expand Down
129 changes: 122 additions & 7 deletions final_project/router/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,155 @@ 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 = req.body.username;
const password = req.body.password;

if (username && password) {
// Check if the user does not already exist
if (!isValid(username)) {
// Add the new user to the users array
users.push({"username": username, "password": password});
return res.status(200).json({message: "User successfully registered. Now you can login"});
} else {
return res.status(404).json({message: "User already exists!"});
}
}
// Return error if username or password is missing
return res.status(404).json({message: "Unable to register user."});
});

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

// Get the book list available in the shop
public_users.get('/task10',function (req, res) {
// Promise zur Rückgabe der Bücherliste
const getBooks = new Promise((resolve, reject) => {
if (books) {
resolve(books); // Bücherliste auflösen
} else {
reject("No books available."); // Fehler auslösen, wenn keine Bücher verfügbar sind
}
});

// Promise verarbeiten
getBooks
.then((books) => {
res.send(JSON.stringify(books, null, 4)); // Erfolgreich die Bücher zurückgeben
})
.catch((err) => {
res.status(500).send(err); // Fehler an den Client senden
});
});

// 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"});
const isbn = req.params.isbn;
res.send(books[isbn]);
});

// Get book details based on ISBN
public_users.get('/task11/isbn/:isbn',function (req, res) {
//Write your code here
const isbn = req.params.isbn;

const getBookByISBN = new Promise((resolve, reject) => {
if(books[isbn]) {
resolve(books[isbn]);
} else {
reject("Book not found");
}
});

getBookByISBN
.then((book) => {
res.send(JSON.stringify(book,null,4));
})
.catch((err) => {
res.status(404).send(err);
})
});

// 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;

let filtered_author = Object.values(books).filter(books => books.author === author);

res.send(filtered_author);
});

// Get book details based on author
public_users.get('/task12/author/:author',function (req, res) {
//Write your code here
const author = req.params.author;

const getBookByAuthor = new Promise((resolve, reject) => {
const filtered_author = Object.values(books).filter(books => books.author === author);

if(filtered_author.length > 0) {
resolve(filtered_author);
} else {
reject("No books found for this author");

}
});
getBookByAuthor
.then((book) => {
res.send(JSON.stringify(book,null,4));
})
.catch((err) => {
res.status(404).send(err);
});
});

// 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;

let filtered_title = Object.values(books).filter(books => books.title === title);

res.send(filtered_title);
});

// Get all books based on title
public_users.get('/task13/title/:title',function (req, res) {
//Write your code here
const title = req.params.title;

const getBookByTitle = new Promise((resolve, reject) => {
let filtered_title = Object.values(books).filter(books => books.title === title);

if(filtered_title.length > 0) {
resolve(filtered_title);
} else {
reject("No books found with this title");
}
});

getBookByTitle
.then((book) => {
res.send(JSON.stringify(book,null,4));
})
.catch((err) => {
res.status(404).send(err);
})
});

// 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;

res.send(books[isbn].reviews);
});

module.exports.general = public_users;