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
20 changes: 14 additions & 6 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,18 @@ $(document).ready(function () {

$(".buy_now").on("click", function (e) {
var id = $(this).attr("data-id");
alert("Product has beed added to Cart");

$.ajax({
url: "/addtocart/" + id, // url where to submit the request
type: "patch", // type of action POST || GET

success: function (data) {
alert("Product has beed added to Cart");
window.location.href = "http://localhost:3000/products/product/" + id;
},
error: function () {},
error: function () {
window.location.href = "/users/form";
},
});
});

Expand All @@ -168,7 +171,6 @@ $(document).ready(function () {
},
error: function () {},
});

});

$(".add_review").on("click", function (e) {
Expand All @@ -184,7 +186,9 @@ $(document).ready(function () {
success: function (data) {
window.location.href = "http://localhost:3000/products/product/" + id;
},
error: function () {},
error: function () {
window.location.href = "/users/form";
},
});
});

Expand All @@ -201,7 +205,9 @@ $(document).ready(function () {
$("#unliked").attr("id", "liked");
userLiked = false;
},
error: function () {},
error: function () {
window.location.href = "/users/form";
},
});
} else {
$.ajax({
Expand All @@ -212,7 +218,9 @@ $(document).ready(function () {
$("#liked").attr("id", "unliked");
userLiked = true;
},
error: function () {},
error: function () {
window.location.href = "/users/form";
},
});
}
});
Expand Down
1 change: 0 additions & 1 deletion public/js/signUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
if (!validString(info.zipCode)) zipCodeInput.addClass("is-invalid");

if (!hasErrors) {
alert("in !if");
signupForm.unbind().submit();
} else {
submitInfo.prop("disabled", false);
Expand Down
78 changes: 40 additions & 38 deletions routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,41 +98,40 @@ router.get("/", async (req, res) => {
//to get product by Id provided
router.get("/products/product/:id", async (req, res) => {
try {
// if (req.session.user) {
errorHandler.checkStringObjectId(req.params.id, "Product ID");
let product = await productsData.getProductById(req.params.id);
const productComments = await productsData.getProductComments(
req.params.id
);

const commentList = [];
for (comment of productComments) {
commentList.push(comment.commentText);
}
if (req.session.user) {
errorHandler.checkStringObjectId(req.params.id, "Product ID");
let product = await productsData.getProductById(req.params.id);
const productComments = await productsData.getProductComments(
req.params.id
);

await usersData.userViewsAProduct(
"609a2fca59ef0ecfeb7b57af",
req.params.id
);
const commentList = [];
for (comment of productComments) {
commentList.push(comment.commentText);
}

let hascomments = true;
await usersData.userViewsAProduct(
"609a2fca59ef0ecfeb7b57af",
req.params.id
);

if (commentList.length == 0) {
hascomments = false;
}
let hascomments = true;

res.render("pages/singleProduct", {
authenticated: req.session.user ? true : false,
adminAuth: req.session.admin ? true : false,
title: product.title,
product: product,
comments: commentList,
hascomments: hascomments,
});
if (commentList.length == 0) {
hascomments = false;
}

// } else {
// res.sendStatus(404).json({ message: "User not Authenticated" });
// }
res.render("pages/singleProduct", {
authenticated: req.session.user ? true : false,
adminAuth: req.session.admin ? true : false,
title: product.title,
product: product,
comments: commentList,
hascomments: hascomments,
});
} else {
return res.sendStatus(404).json({ message: "User not Authenticated" });
}
} catch (e) {
return res.status(404).json({ error: "product not found" });
}
Expand Down Expand Up @@ -250,7 +249,7 @@ router.get("/buy", async (req, res) => {
});

router.patch("/product/comment/:id", async (req, res) => {
const comment_text = req.body.review;
const comment_text = xss(req.body.review);
try {
errorHandler.checkStringObjectId(req.params.id, "Product ID");
errorHandler.checkString(comment_text);
Expand All @@ -260,29 +259,32 @@ router.patch("/product/comment/:id", async (req, res) => {
req.params.id,
comment_text
);
res.sendStatus(200);
return res.sendStatus(200);
} else {
res.sendStatus(404);
return res.sendStatus(404);
}
} catch (error) {
console.log(error);
res.sendStatus(404);
return res.sendStatus(404);
}
});

router.patch("/addtocart/:id", async (req, res) => {
console.log("edws");
// if (req.session.user == "undefined") {
// res.redirect("/");
// }
try {
errorHandler.checkStringObjectId(req.params.id, "Product ID");
if (req.session.user) {
console.log(req.session);
req.session.cartItems.push(req.params.id);
res.sendStatus(200);
return res.sendStatus(200);
} else {
res.sendStatus(404);
return res.sendStatus(404);
}
} catch (error) {
console.log(error);
res.sendStatus(404);
return res.sendStatus(404);
}
});
////////////////
Expand Down
94 changes: 46 additions & 48 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ router.post("/login", async (req, res) => {
req.session.cartItems = [];
return res.redirect("/users/details");

return res.redirect("/");
// return res.redirect("/");
} else {
errors.push("User E-mail address or password does not match");
return res.render("pages/loginPage", {
Expand All @@ -192,50 +192,51 @@ router.get("/signup", async (req, res) => {
});

router.post("/signup", async (req, res) => {
dataSignIn = req.body;
const firstName = xss(dataSignIn.firstName);
const lastName = xss(dataSignIn.lastName);
const email = xss(dataSignIn.emailId);
const password = xss(dataSignIn.password);
const phoneNumber = xss(dataSignIn.phoneNumber);
const Line1 = xss(dataSignIn.Line1);
const Line2 = xss(dataSignIn.Line2);
const City = xss(dataSignIn.City);
const State = xss(dataSignIn.State);
const ZipCode = xss(dataSignIn.ZipCode);

errors = [];
if (!errorCheck.stringCheck(firstName))
errors.push("Invalid FirstName (routes/users)");
if (!errorCheck.stringCheck(lastName))
errors.push("Invalid LastName (routes/users)");
if (!errorCheck.emailValidate(email))
errors.push("Invalid Email (routes/users)");
if (!errorCheck.validPassword(password))
errors.push("Invalid Password (routes/users)");
if (!errorCheck.phoneNumberValid(phoneNumber))
errors.push("Invalid PhoneNumber (routes/users)");
if (!errorCheck.stringCheck(Line1))
errors.push("Invalid LineOne (routes/users )");

if (!errorCheck.stringCheck(Line2))
errors.push("Invalid LineTwo (routes/users )");
if (!errorCheck.stringCheck(City))
errors.push("Invalid City (routes/users )");

if (!errorCheck.stringCheck(State))
errors.push("Invalid State routes/users )");
if (!errorCheck.zipcCodeValid(ZipCode))
errors.push("Invalid Zip Code (routes/users )");
address = {
Line1: Line1,
Line2: Line2,
City: City,
State: State,
ZipCode: parseInt(ZipCode),
Country: "USA",
};
try {
dataSignIn = req.body;
const firstName = xss(dataSignIn.firstName);
const lastName = xss(dataSignIn.lastName);
const email = xss(dataSignIn.emailId);
const password = xss(dataSignIn.password);
const phoneNumber = xss(dataSignIn.phoneNumber);
const Line1 = xss(dataSignIn.Line1);
const Line2 = xss(dataSignIn.Line2);
const City = xss(dataSignIn.City);
const State = xss(dataSignIn.State);
const ZipCode = xss(dataSignIn.ZipCode);

errors = [];
if (!errorCheck.stringCheck(firstName))
errors.push("Invalid FirstName (routes/users)");
if (!errorCheck.stringCheck(lastName))
errors.push("Invalid LastName (routes/users)");
if (!errorCheck.emailValidate(email))
errors.push("Invalid Email (routes/users)");
if (!errorCheck.validPassword(password))
errors.push("Invalid Password (routes/users)");
if (!errorCheck.phoneNumberValid(phoneNumber))
errors.push("Invalid PhoneNumber (routes/users)");
if (!errorCheck.stringCheck(Line1))
errors.push("Invalid LineOne (routes/users )");

if (!errorCheck.stringCheck(Line2))
errors.push("Invalid LineTwo (routes/users )");
if (!errorCheck.stringCheck(City))
errors.push("Invalid City (routes/users )");

if (!errorCheck.stringCheck(State))
errors.push("Invalid State routes/users )");
if (!errorCheck.zipcCodeValid(ZipCode))
errors.push("Invalid Zip Code (routes/users )");
address = {
Line1: Line1,
Line2: Line2,
City: City,
State: State,
ZipCode: parseInt(ZipCode),
Country: "USA",
};

dataError.checkAddress(address);
// Just for woking part I'm throwing JSON error
if (errors.length > 0) {
Expand All @@ -248,14 +249,11 @@ router.post("/signup", async (req, res) => {
}

const allUsers = await usersData.getAllUsers();
let emailUsed;
let emailUsed = false;
allUsers.find((user) => {
if (user.emailId === email.toLowerCase()) {
emailUsed = true;

return emailUsed;
} else {
emailUsed = false;
return emailUsed;
}
});
Expand Down
9 changes: 7 additions & 2 deletions views/pages/singleProduct.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</label>
<input type="text" name="review" />
<button
class="add_review"
class="add_review btn btn-success"
type="submit"
data-id="{{product._id}}"
>Submit</button>
Expand Down Expand Up @@ -81,7 +81,12 @@
<div class="col-md-6 mb-4 mb-md-0">
<!--Image-->
<div class="view overlay z-depth-1-half">
<img src="{{product.productImage}}" class="" alt="single product" height="250px" />
<img
src="{{product.productImage}}"
class=""
alt="single product"
height="250px"
/>
<a href="#">
<div class="mask rgba-white-light"></div>
</a>
Expand Down