Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing the rendering view template in backend with the JSON response in files Resolving #130 #191

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion backend/src/routes/feed.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ const sequelize = new Sequelize(require('../config/keys').PostgresURI);
initModels(sequelize);

router.get('/feed', ensureAuthenticated, function (req, res) {
if (!req.isAuthenticated()) {
// add ! back in the if statement
if (req.isAuthenticated()) {
const errors = [];
res.post('Unauthenticated');
} else {
11 changes: 4 additions & 7 deletions backend/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ initModels(sequelize);
router.get('/', function(req, res, next) {
if (!req.isAuthenticated()) {
let errors = [];
res.render('index', { errors });
res.json(errors);
} else {
res.redirect('/home', { title: 'Home' });

@@ -43,13 +43,10 @@ router.get('/home', ensureAuthenticated, function(req, res) {
// }
// console.log(ids);
if (allItems.length > 0) {
res.render('dashboard', {
data: { name: req.user, allItems }
})
res.json({ name: req.user, allItems });
} else {
res.render('dashboard', {
data: { name: req.user }
})
res.json({ name: req.user });

}
}
}
73 changes: 37 additions & 36 deletions backend/src/routes/sale.js
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ router.get("/sale", ensureAuthenticated, function (req, res) {
let errors = [];
res.redirect("index", { errors });
} else {
res.render("sale", {
name: req.user.name,
res.json({
name: req.user.name
});
}
});
@@ -73,9 +73,10 @@ router.get("/donor", ensureAuthenticated, function (req, res) {
},
})
.then((allDonors) => {
res.render("donor", {
data: { name: req.user.name, donors: allDonors },
});
res.json({
name: req.user.name,
donors: allDonors
});
})
.catch((err) => console.log(err));
}
@@ -95,13 +96,11 @@ router.get("/categories", ensureAuthenticated, function (req, res) {
}
console.log(donor);
const allCategories = item.getAttributes().category?.defaultValue;
res.render("stock", {
data: {
name: req.user.name,
categories: allCategories,
donorId: donor,
donorName: donor_name,
},
res.json({
name: req.user.name,
categories: allCategories,
donorId: donor,
donorName: donor_name,
});
}
});
@@ -230,9 +229,11 @@ router.post("/checkout", ensureAuthenticated, function (req, res) {
itemName +
" has expired. Select other item.",
});
res.render("checkoutdetails", {
data: { name: req.user.name, items: allItems, errors },
});
res.json({
name: req.user.name,
items: allItems,
errors: errors
});
}
});
} else if (chicoId.length != 9) {
@@ -244,9 +245,11 @@ router.post("/checkout", ensureAuthenticated, function (req, res) {
if (err) {
console.log("THIS IS ERRROR " + err);
} else {
res.render("checkoutdetails", {
data: { name: req.user.name, items: allItems, errors },
});
res.json({
name: req.user.name,
items: allItems,
errors: errors
});
}
});
} else {
@@ -266,8 +269,9 @@ router.post("/checkout", ensureAuthenticated, function (req, res) {
if (err) {
console.log("THIS IS ERRROR " + err);
} else {
res.render("checkoutdetails", {
data: { name: req.user.name, items: allItems, errors },
res.json({
name: req.user.name,
items: allItems
});
}
});
@@ -307,9 +311,7 @@ router.post("/checkout", ensureAuthenticated, function (req, res) {
console.log("THIS IS ERRROR " + err);
} else {
console.log(" ALL ITEMS => " + allItems);
res.render("checkout_success", {
data: { name: req.user.name, items: allItems, errors },
});
res.status(200).json({ items: allItems });
}
});
//res.render('checkout_success', {data: {}});
@@ -336,9 +338,10 @@ router.get("/checkout", function (req, res) {
// console.log(" forward to checkout");
// console.log(allItems);

res.render("checkoutdetails", {
data: { name: "Subhed", items: allItems },
});
res.json({
name: "Subhed",
items: allItems
});
}

console.log("OUTSIDE");
@@ -378,16 +381,14 @@ router.get("/charts", ensureAuthenticated, function (req, res) {
} else {
// console.log(allItems1);

res.render("charts", {
data: {
name: req.user.name,
allItems,
cat: result,
stock: allItems0,
items: allItems1,
expired: expItems,
},
});
res.json({
name: req.user.name,
allItems: allItems,
cat: result,
stock: allItems0,
items: allItems1,
expired: expItems
});

return;
}
25 changes: 12 additions & 13 deletions backend/src/routes/users.js
Original file line number Diff line number Diff line change
@@ -15,12 +15,11 @@ initModels(sequelize);

router.get('/register', function (req, res, next) {
if (!req.isAuthenticated()) {
let errors = [];
res.render('signup', { errors });
}
else {
res.redirect('/home');
}
let errors = ['User not authenticated'];
res.status(400).json({ errors });
} else {
res.status(200).json({ message: 'Redirect to /home' });
}
});

router.post('/sign_up', function (req, res) {
@@ -37,9 +36,9 @@ router.post('/sign_up', function (req, res) {
}
}).then(([user, created]) => {
if (!created) {
let errors = [];
errors.push('User Exist!' );
res.render('signup', { errors });
// let errors = [];
// errors.push('User Exist!' );
res.status(409).json({ error: 'User Exist!' });
}
else {
bcrypt.genSalt(10, (err, salt) =>
@@ -52,15 +51,16 @@ router.post('/sign_up', function (req, res) {


console.log(user);
return res.render('./signup_success', { title: 'Express' });
return res.status(201).json({ user });

}

});
});


router.get('/login', function (req, res, next) {
res.render('signup_success', { title: 'Express' });
res.status(200).json({ message: 'Signup successful'});
});

router.post('/login', function (req, res, next) {
@@ -80,8 +80,7 @@ router.post('/login', function (req, res, next) {


router.get('/logout', function (req, res) {
req.logout();
res.render('index', { title: 'Express' });
res.status(200).send({ title: 'Express' });
});