diff --git a/final_project/Images_of_task/Task 1.png b/final_project/Images_of_task/Task 1.png new file mode 100644 index 0000000000..ca5d493569 Binary files /dev/null and b/final_project/Images_of_task/Task 1.png differ diff --git a/final_project/Images_of_task/Task 10.png b/final_project/Images_of_task/Task 10.png new file mode 100644 index 0000000000..9af3f83f53 Binary files /dev/null and b/final_project/Images_of_task/Task 10.png differ diff --git a/final_project/Images_of_task/Task 11.png b/final_project/Images_of_task/Task 11.png new file mode 100644 index 0000000000..c19a260a64 Binary files /dev/null and b/final_project/Images_of_task/Task 11.png differ diff --git a/final_project/Images_of_task/Task 12.png b/final_project/Images_of_task/Task 12.png new file mode 100644 index 0000000000..0b0280678e Binary files /dev/null and b/final_project/Images_of_task/Task 12.png differ diff --git a/final_project/Images_of_task/Task 13.png b/final_project/Images_of_task/Task 13.png new file mode 100644 index 0000000000..a0bafc1081 Binary files /dev/null and b/final_project/Images_of_task/Task 13.png differ diff --git a/final_project/Images_of_task/Task 2.png b/final_project/Images_of_task/Task 2.png new file mode 100644 index 0000000000..450166da42 Binary files /dev/null and b/final_project/Images_of_task/Task 2.png differ diff --git a/final_project/Images_of_task/Task 3.png b/final_project/Images_of_task/Task 3.png new file mode 100644 index 0000000000..744dd0cf04 Binary files /dev/null and b/final_project/Images_of_task/Task 3.png differ diff --git a/final_project/Images_of_task/Task 4.png b/final_project/Images_of_task/Task 4.png new file mode 100644 index 0000000000..4286427c55 Binary files /dev/null and b/final_project/Images_of_task/Task 4.png differ diff --git a/final_project/Images_of_task/Task 6.png b/final_project/Images_of_task/Task 6.png new file mode 100644 index 0000000000..8507ecd4f7 Binary files /dev/null and b/final_project/Images_of_task/Task 6.png differ diff --git a/final_project/Images_of_task/Task 7.png b/final_project/Images_of_task/Task 7.png new file mode 100644 index 0000000000..edfe139bc3 Binary files /dev/null and b/final_project/Images_of_task/Task 7.png differ diff --git a/final_project/Images_of_task/Task 8.png b/final_project/Images_of_task/Task 8.png new file mode 100644 index 0000000000..b93540be05 Binary files /dev/null and b/final_project/Images_of_task/Task 8.png differ diff --git a/final_project/Images_of_task/Task 9.png b/final_project/Images_of_task/Task 9.png new file mode 100644 index 0000000000..a1f1187e78 Binary files /dev/null and b/final_project/Images_of_task/Task 9.png differ diff --git a/final_project/Test/Teste_cmd b/final_project/Test/Teste_cmd new file mode 100644 index 0000000000..39f7ded639 --- /dev/null +++ b/final_project/Test/Teste_cmd @@ -0,0 +1,26 @@ +$body = @{ + username = "john1" + password = "pass123" +} | ConvertTo-Json + +Invoke-RestMethod -Uri "http://localhost:5000/register" -Method POST -Body $body -ContentType "application/json" + + +##Login success +Write-Host "`n=== login ===" -ForegroundColor Green +$loginBody = @{ + username = "john1" + password = "pass123" +} | ConvertTo-Json + +try { + $loginResult = Invoke-RestMethod -Uri "http://localhost:5000/customer/login" -Method POST -Body $loginBody -ContentType "application/json" -SessionVariable session + Write-Host "Yes: $($loginResult.message)" -ForegroundColor Green +} catch { + Write-Host "No: $($_.Exception.Response.StatusCode) - $($_.ErrorDetails.Message)" -ForegroundColor Red +} +Invoke-RestMethod -Uri "http://localhost:5000/customer/auth/review/1" ` + -Method Put ` + -Headers @{ "Content-Type" = "application/json" } ` + -Body '{"review":"This is John’s review"}' + diff --git a/final_project/index.js b/final_project/index.js index b890c1d380..4d6e5cebc3 100644 --- a/final_project/index.js +++ b/final_project/index.js @@ -12,6 +12,10 @@ 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 || !req.session.authorization || !req.session.authorization.username) { + return res.status(401).json({ message: "User not logged in" }); + } + next(); // very important to call next() }); const PORT =5000; diff --git a/final_project/router/auth_users.js b/final_project/router/auth_users.js index 8cb6ef6e40..cf788e65a3 100644 --- a/final_project/router/auth_users.js +++ b/final_project/router/auth_users.js @@ -7,24 +7,106 @@ let users = []; const isValid = (username)=>{ //returns boolean //write code to check is the username is valid + return (username in users); } const authenticatedUser = (username,password)=>{ //returns boolean //write code to check if username and password match the one we have in records. + if(isValid(username)){ + if(users[username] === password){ + return true; + }else{ + return false; + } + }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"}); + let username = req.body.username; + let password = req.body.password; + if(!username || !password){ + res.status(400).json({message : `Please input the username and the password`}); + } + + if(!authenticatedUser(username,password)){ + res.status(401).json({message : `Username and password not match`}) + }else{ + + let accesstoken = jwt.sign({data : username} , 'access', {expiresIn : 60 * 60}) + req.session.authorization = { + accesstoken, + username + } + return res.status(200).json( + { + message : `User ${username} has login `, + token: accesstoken + } + ) + + } + + + //return res.status(300).json({message: "Yet to be implemented"}); }); // 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"}); + + let isbn = req.params.isbn; + let username = req.session.authorization.username; + let review = req.body.review; + + if (!review || review.trim().length === 0) { + return res.status(400).json({ message: "Review text is required" }); + } + + let book = books[isbn]; + if (!book) { + return res.status(404).json({ message: "Book not found" }); + } + + book.reviews[username] = review; + + return res.status(200).json({ + message: "Review successfully added/updated", + reviews: book.reviews + }); }); +//delete review +regd_users.delete("/auth/review/:isbn", (req, res) => { + + let isbn = req.params.isbn; + let username = req.session.authorization.username; + console.log("Je suis ici") + + let book = books[isbn]; + if (!book) { + return res.status(404).json({ message: "Book not found" }); + } + + let review = book.reviews[username] + + if(review){ + delete book.reviews[username]; + + return res.status(200).json({ + message: `Review ${review} successfully deleted` + }) + }else{ + return res.status(400).json({ + message: `Review for this user not exist` + }) + + } +}) + + module.exports.authenticated = regd_users; module.exports.isValid = isValid; module.exports.users = users; diff --git a/final_project/router/general.js b/final_project/router/general.js index 9eb0ac1a91..86b91dc493 100644 --- a/final_project/router/general.js +++ b/final_project/router/general.js @@ -7,37 +7,114 @@ 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"}); + let username = req.body.username; + let password = req.body.password; + + if(!username || !password){ + return res.status(400).json({message: "Please enter the username and the password!"}); + } + + + if(username in users){ + return res.status(409).json({message: `User ${username} has already been registered`}); + } + + + users[username] = password; + return res.status(201).json({message: `New User ${username} has been registered`}); + }); // 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"}); + + +public_users.get('/', function (req, res) { + new Promise((resolve, reject) => { + if (books) { + resolve(books); + } else { + reject("Books not found"); + } + }) + .then(data => res.status(200).json(data)) + .catch(err => res.status(500).json({ message: err })); }); + // 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"}); + + new Promise((resolve,reject) => { + let isbn = req.params.isbn; + let book_find = books[isbn]; + if(book_find){ + resolve(book_find); + }else{ + reject("Book not found"); + } + + }) + .then(data => res.status(200).json(data)) + .catch(err => res.status(500).json({message : 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"}); + new Promise((resolve,reject) =>{ + let author = req.params.author; + let book_find_with_author = {}; + for(let isbn in books){ + if(books[isbn].author === author){ + book_find_with_author[isbn] = books[isbn] + } + } + + if(book_find_with_author){ + resolve(book_find_with_author); + }else{ + reject("No Book Found"); + } + + + }).then(data => res.status(200).json(data)) + .catch(err => res.status(500).json({message : 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"}); + + new Promise((resolve,reject)=>{ + let title = req.params.title; + let book_find_with_title = {}; + for(let isbn in books){ + if(books[isbn].title === title){ + book_find_with_title[isbn] = books[isbn] + } + } + if(book_find_with_title){ + resolve(book_find_with_title) + }else{ + reject("No book found with your title") + } + + }).then(data=>res.status(200).json(data)) + .catch(err=> res.status(500).json({message : err})) }); // Get book review public_users.get('/review/:isbn',function (req, res) { //Write your code here + let isbn = req.params.isbn; + let book_find = books[isbn]; + if(book_find){ + return res.status(200).json(books[isbn].reviews) + } return res.status(300).json({message: "Yet to be implemented"}); }); + + module.exports.general = public_users;