diff --git a/.DS_Store b/.DS_Store index c5c4cad722..57fffa4da4 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Screenshots/1-getallbooks.png b/Screenshots/1-getallbooks.png new file mode 100644 index 0000000000..63517d5b87 Binary files /dev/null and b/Screenshots/1-getallbooks.png differ diff --git a/Screenshots/2-gedetailsISBN.png b/Screenshots/2-gedetailsISBN.png new file mode 100644 index 0000000000..267000a5fa Binary files /dev/null and b/Screenshots/2-gedetailsISBN.png differ diff --git a/Screenshots/3-getbooksbyauthor.png b/Screenshots/3-getbooksbyauthor.png new file mode 100644 index 0000000000..50fb8635a5 Binary files /dev/null and b/Screenshots/3-getbooksbyauthor.png differ diff --git a/Screenshots/4-getbooksbytitle.png b/Screenshots/4-getbooksbytitle.png new file mode 100644 index 0000000000..b2b42e3fa7 Binary files /dev/null and b/Screenshots/4-getbooksbytitle.png differ diff --git a/Screenshots/5-getbookreview.png b/Screenshots/5-getbookreview.png new file mode 100644 index 0000000000..fa1d215d95 Binary files /dev/null and b/Screenshots/5-getbookreview.png differ diff --git a/Screenshots/6-register.png b/Screenshots/6-register.png new file mode 100644 index 0000000000..bcdde2d4f4 Binary files /dev/null and b/Screenshots/6-register.png differ diff --git a/Screenshots/7-login.png b/Screenshots/7-login.png new file mode 100644 index 0000000000..c7bed1fdab Binary files /dev/null and b/Screenshots/7-login.png differ diff --git a/Screenshots/8-reviewadded.png b/Screenshots/8-reviewadded.png new file mode 100644 index 0000000000..ab3d7d8aa3 Binary files /dev/null and b/Screenshots/8-reviewadded.png differ diff --git a/Screenshots/9-deletereview.png b/Screenshots/9-deletereview.png new file mode 100644 index 0000000000..9741f6d369 Binary files /dev/null and b/Screenshots/9-deletereview.png differ diff --git a/Screenshots/task10.png b/Screenshots/task10.png new file mode 100644 index 0000000000..c4f3cf12d5 Binary files /dev/null and b/Screenshots/task10.png differ diff --git a/Screenshots/task11.png b/Screenshots/task11.png new file mode 100644 index 0000000000..f958f9a0dc Binary files /dev/null and b/Screenshots/task11.png differ diff --git a/Screenshots/task12.png b/Screenshots/task12.png new file mode 100644 index 0000000000..2027523dbf Binary files /dev/null and b/Screenshots/task12.png differ diff --git a/Screenshots/task13.png b/Screenshots/task13.png new file mode 100644 index 0000000000..c92ec60bde Binary files /dev/null and b/Screenshots/task13.png differ diff --git a/final_project/.DS_Store b/final_project/.DS_Store new file mode 100644 index 0000000000..3e90f4a97e Binary files /dev/null and b/final_project/.DS_Store differ diff --git a/final_project/index.js b/final_project/index.js index b890c1d380..418ef46b10 100644 --- a/final_project/index.js +++ b/final_project/index.js @@ -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); diff --git a/final_project/router/auth_users.js b/final_project/router/auth_users.js index 8cb6ef6e40..943e1d4be9 100644 --- a/final_project/router/auth_users.js +++ b/final_project/router/auth_users.js @@ -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; diff --git a/final_project/router/general.js b/final_project/router/general.js index 9eb0ac1a91..4d931cf3d0 100644 --- a/final_project/router/general.js +++ b/final_project/router/general.js @@ -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;