Skip to content

Commit

Permalink
add 401 status to non auth uers
Browse files Browse the repository at this point in the history
  • Loading branch information
keikaavousi committed Dec 11, 2021
1 parent 7f0a706 commit 17325f6
Show file tree
Hide file tree
Showing 106 changed files with 361 additions and 363 deletions.
Binary file modified .DS_Store
100644 → 100755
Binary file not shown.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .vscode/settings.json
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified __test__/cart.spec.js
100644 → 100755
Empty file.
Empty file modified __test__/product.spec.js
100644 → 100755
Empty file.
Empty file modified __test__/user.spec.js
100644 → 100755
Empty file.
46 changes: 23 additions & 23 deletions controller/auth.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const User = require("../model/user");
const User = require('../model/user');

module.exports.login = (req, res) => {
const username = req.body.username;
const password = req.body.password;
const username = req.body.username;
const password = req.body.password;

if (username && password) {
User.findOne({
username,
})
.then((user) => {
if (!user) {
res.json({
status: "Error",
msg: "username or password is incorrect",
});
} else {
res.json({
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
});
}
})
.catch((err) => {
console.log(err);
});
}
if (username && password) {
User.findOne({
username,
})
.then((user) => {
if (!user) {
res.status(401).json({
status: 'Error',
msg: 'username or password is incorrect',
});
} else {
res.json({
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
});
}
})
.catch((err) => {
console.log(err);
});
}
};
197 changes: 97 additions & 100 deletions controller/cart.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,113 +1,110 @@
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)

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))
}
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));
};

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()
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();

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))
}
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));
};

module.exports.addCart = (req,res) => {
if (typeof req.body == undefined) {
res.json({
status: "error",
message: "data is undefined"
})
} else {
console.log(req.body.date)
let cartCount = 0;
Cart.find().countDocuments(function (err, count) {
cartCount = count
})
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
// })

.then(() => {
const cart = new Cart({
id: cartCount + 1,
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:req.params.id,
userId: req.body.userId,
date:req.body.date,
products:req.body.products
})
}
}
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: 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))
}
}
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));
}
};
Empty file modified controller/home.js
100644 → 100755
Empty file.
Loading

0 comments on commit 17325f6

Please sign in to comment.