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
10 changes: 9 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ DB_USERNAME=root
DB_PASSWORD=example
DB_NAME=fake_store
DB_PORT=27017
DATABASE_URL=mongodb://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?authSource=admin
# DATABASE_URL=mongodb://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?authSource=admin


# DATABASE_URL=mongodb://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?authSource=admin

# db - is the service name which is also the db connection name for local
# DATABASE_URL=mongodb://db:$DB_PORT/DB_NAME
# DATABASE_URL=mongodb://$DB_USERNAME:$DB_PASSWORD@db:$DB_PORT/DB_NAME?authSource=admin
DATABASE_URL=mongodb://db:$DB_PORT/DB_NAME
172 changes: 86 additions & 86 deletions controller/cart.js
Original file line number Diff line number Diff line change
@@ -1,110 +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();
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();
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));
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
// })
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,
});
}
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));
}
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));
}
};
Loading