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 added final_project/Images_of_task/Task 1.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/Images_of_task/Task 10.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/Images_of_task/Task 11.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/Images_of_task/Task 12.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/Images_of_task/Task 13.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/Images_of_task/Task 2.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/Images_of_task/Task 3.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/Images_of_task/Task 4.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/Images_of_task/Task 6.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/Images_of_task/Task 7.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/Images_of_task/Task 8.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/Images_of_task/Task 9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions final_project/Test/Teste_cmd
Original file line number Diff line number Diff line change
@@ -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"}'

4 changes: 4 additions & 0 deletions final_project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 85 additions & 3 deletions final_project/router/auth_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
95 changes: 86 additions & 9 deletions final_project/router/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;