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

Module.exports, Request body, Async Await Func #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
49 changes: 25 additions & 24 deletions controller/auth.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const User = require('../model/user');
const jwt = require('jsonwebtoken');
const User = require("../model/user");
const jwt = require("jsonwebtoken");

module.exports.login = (req, res) => {
const username = req.body.username;
const password = req.body.password;
if (username && password) {
User.findOne({
username: username,
password: password,
})
.then((user) => {
if (user) {
res.json({
token: jwt.sign({ user: username }, 'secret_key'),
});
} else {
res.status(401);
res.send('username or password is incorrect');
}
})
.catch((err) => {
console.error(err);
});
}
module.exports = {
login: async (req, res) => {
const { username, password } = req.body;
try {
if (username && password) {
const user = await User.findOne({
username: username,
password: password,
});

if (user) {
res.json({
token: jwt.sign({ user: username }, "secret_key"),
});
} else {
res.status(401);
res.send("username or password is incorrect");
}
}
} catch (err) {
console.error(err);
}
},
};
196 changes: 99 additions & 97 deletions controller/cart.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,112 @@
const Cart = require('../model/cart');
const Cart = require("../model/cart");

module.exports.getAllCarts = (req, res) => {
const limit = Number(req.query.limit) || 0;
const sort = req.query.sort == 'desc' ? -1 : 1;
const startDate = req.query.startdate || new Date('1970-1-1');
const endDate = req.query.enddate || new Date();
module.exports = {
getAllCarts: (req, res) => {
const limit = Number(req.query.limit) || 0;
const sort = req.query.sort == "desc" ? -1 : 1;
const startDate = req.query.startdate || new Date("1970-1-1");
const endDate = req.query.enddate || new Date();

console.log(startDate, endDate);
console.log(startDate, endDate);

Cart.find({
date: { $gte: new Date(startDate), $lt: new Date(endDate) },
})
.select('-_id -products._id')
.limit(limit)
.sort({ id: sort })
.then((carts) => {
res.json(carts);
})
.catch((err) => console.log(err));
};
Cart.find({
date: { $gte: new Date(startDate), $lt: new Date(endDate) },
})
.select("-_id -products._id")
.limit(limit)
.sort({ id: sort })
.then((carts) => {
res.json(carts);
})
.catch((err) => console.log(err));
},

module.exports.getCartsbyUserid = (req, res) => {
const userId = req.params.userid;
const startDate = req.query.startdate || new Date('1970-1-1');
const endDate = req.query.enddate || new Date();
getCartsbyUserid: (req, res) => {
const userId = req.params.userid;
const startDate = req.query.startdate || new Date("1970-1-1");
const endDate = req.query.enddate || new Date();

console.log(startDate, endDate);
Cart.find({
userId,
date: { $gte: new Date(startDate), $lt: new Date(endDate) },
})
.select('-_id -products._id')
.then((carts) => {
res.json(carts);
})
.catch((err) => console.log(err));
};
console.log(startDate, endDate);
Cart.find({
userId,
date: { $gte: new Date(startDate), $lt: new Date(endDate) },
})
.select("-_id -products._id")
.then((carts) => {
res.json(carts);
})
.catch((err) => console.log(err));
},

module.exports.getSingleCart = (req, res) => {
const id = req.params.id;
Cart.findOne({
id,
})
.select('-_id -products._id')
.then((cart) => res.json(cart))
.catch((err) => console.log(err));
};
getSingleCart: (req, res) => {
const id = req.params.id;
Cart.findOne({
id,
})
.select("-_id -products._id")
.then((cart) => res.json(cart))
.catch((err) => console.log(err));
},

module.exports.addCart = (req, res) => {
if (typeof req.body == undefined) {
res.json({
status: 'error',
message: 'data is undefined',
});
} else {
// let cartCount = 0;
// Cart.find().countDocuments(function (err, count) {
// cartCount = count
// })
addCart: (req, res) => {
if (typeof req.body == undefined) {
res.json({
status: "error",
message: "data is undefined",
});
} else {
// let cartCount = 0;
// Cart.find().countDocuments(function (err, count) {
// cartCount = count
// })

// .then(() => {
const cart = {
id: 11,
userId: req.body.userId,
date: req.body.date,
products: req.body.products,
};
// cart.save()
// .then(cart => res.json(cart))
// .catch(err => console.log(err))
// .then(() => {
const cart = {
id: 11,
userId: req.body.userId,
date: req.body.date,
products: req.body.products,
};
// cart.save()
// .then(cart => res.json(cart))
// .catch(err => console.log(err))

res.json(cart);
// })
res.json(cart);
// })

//res.json({...req.body,id:Cart.find().count()+1})
}
};
//res.json({...req.body,id:Cart.find().count()+1})
}
},

module.exports.editCart = (req, res) => {
if (typeof req.body == undefined || req.params.id == null) {
res.json({
status: 'error',
message: 'something went wrong! check your sent data',
});
} else {
res.json({
id: parseInt(req.params.id),
userId: req.body.userId,
date: req.body.date,
products: req.body.products,
});
}
};
editCart: (req, res) => {
if (typeof req.body == undefined || req.params.id == null) {
res.json({
status: "error",
message: "something went wrong! check your sent data",
});
} else {
res.json({
id: parseInt(req.params.id),
userId: req.body.userId,
date: req.body.date,
products: req.body.products,
});
}
},

module.exports.deleteCart = (req, res) => {
if (req.params.id == null) {
res.json({
status: 'error',
message: 'cart id should be provided',
});
} else {
Cart.findOne({ id: req.params.id })
.select('-_id -products._id')
.then((cart) => {
res.json(cart);
})
.catch((err) => console.log(err));
}
deleteCart: (req, res) => {
if (req.params.id == null) {
res.json({
status: "error",
message: "cart id should be provided",
});
} else {
Cart.findOne({ id: req.params.id })
.select("-_id -products._id")
.then((cart) => {
res.json(cart);
})
.catch((err) => console.log(err));
}
},
};
14 changes: 8 additions & 6 deletions controller/home.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module.exports.indexPage = (req,res) => {
res.render('home/index')
}
module.exports = {
indexPage: (req, res) => {
res.render("home/index");
},

module.exports.docsPage = (req,res) => {
res.render('home/docs')
}
docsPage: (req, res) => {
res.render("home/docs");
},
};
Loading