From 34d53612f2e941b9583ae2ecbbb90bf678f233d9 Mon Sep 17 00:00:00 2001 From: "MUH. AHSAN" Date: Tue, 6 Jun 2023 06:18:39 +0700 Subject: [PATCH 1/2] Update module.exports --- controller/cart.js | 196 ++++++++++++++++++------------------ controller/home.js | 14 +-- controller/product.js | 218 ++++++++++++++++++++-------------------- controller/user.js | 226 +++++++++++++++++++++--------------------- 4 files changed, 331 insertions(+), 323 deletions(-) diff --git a/controller/cart.js b/controller/cart.js index 84700098..5e326190 100755 --- a/controller/cart.js +++ b/controller/cart.js @@ -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)); + } + }, }; diff --git a/controller/home.js b/controller/home.js index e49c9107..16d22f39 100755 --- a/controller/home.js +++ b/controller/home.js @@ -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') -} \ No newline at end of file + docsPage: (req, res) => { + res.render("home/docs"); + }, +}; diff --git a/controller/product.js b/controller/product.js index 699d97d6..11dd9c6b 100755 --- a/controller/product.js +++ b/controller/product.js @@ -1,118 +1,120 @@ -const Product = require('../model/product'); +const Product = require("../model/product"); -module.exports.getAllProducts = (req, res) => { - const limit = Number(req.query.limit) || 0; - const sort = req.query.sort == 'desc' ? -1 : 1; +module.exports = { + getAllProducts: (req, res) => { + const limit = Number(req.query.limit) || 0; + const sort = req.query.sort == "desc" ? -1 : 1; - Product.find() - .select(['-_id']) - .limit(limit) - .sort({ id: sort }) - .then((products) => { - res.json(products); - }) - .catch((err) => console.log(err)); -}; + Product.find() + .select(["-_id"]) + .limit(limit) + .sort({ id: sort }) + .then((products) => { + res.json(products); + }) + .catch((err) => console.log(err)); + }, -module.exports.getProduct = (req, res) => { - const id = req.params.id; + getProduct: (req, res) => { + const id = req.params.id; - Product.findOne({ - id, - }) - .select(['-_id']) - .then((product) => { - res.json(product); - }) - .catch((err) => console.log(err)); -}; + Product.findOne({ + id, + }) + .select(["-_id"]) + .then((product) => { + res.json(product); + }) + .catch((err) => console.log(err)); + }, + + getProductCategories: (req, res) => { + Product.distinct("category") + .then((categories) => { + res.json(categories); + }) + .catch((err) => console.log(err)); + }, -module.exports.getProductCategories = (req, res) => { - Product.distinct('category') - .then((categories) => { - res.json(categories); - }) - .catch((err) => console.log(err)); -}; + getProductsInCategory: (req, res) => { + const category = req.params.category; + const limit = Number(req.query.limit) || 0; + const sort = req.query.sort == "desc" ? -1 : 1; -module.exports.getProductsInCategory = (req, res) => { - const category = req.params.category; - const limit = Number(req.query.limit) || 0; - const sort = req.query.sort == 'desc' ? -1 : 1; + Product.find({ + category, + }) + .select(["-_id"]) + .limit(limit) + .sort({ id: sort }) + .then((products) => { + res.json(products); + }) + .catch((err) => console.log(err)); + }, - Product.find({ - category, - }) - .select(['-_id']) - .limit(limit) - .sort({ id: sort }) - .then((products) => { - res.json(products); - }) - .catch((err) => console.log(err)); -}; + addProduct: (req, res) => { + if (typeof req.body == undefined) { + res.json({ + status: "error", + message: "data is undefined", + }); + } else { + // let productCount = 0; + // Product.find() + // .countDocuments(function (err, count) { + // productCount = count; + // }) + // .then(() => { + const product = { + id: 21, + title: req.body.title, + price: req.body.price, + description: req.body.description, + image: req.body.image, + category: req.body.category, + }; + // product.save() + // .then(product => res.json(product)) + // .catch(err => console.log(err)) + res.json(product); + // }); + } + }, -module.exports.addProduct = (req, res) => { - if (typeof req.body == undefined) { - res.json({ - status: 'error', - message: 'data is undefined', - }); - } else { - // let productCount = 0; - // Product.find() - // .countDocuments(function (err, count) { - // productCount = count; - // }) - // .then(() => { - const product = { - id: 21, - title: req.body.title, - price: req.body.price, - description: req.body.description, - image: req.body.image, - category: req.body.category, - }; - // product.save() - // .then(product => res.json(product)) - // .catch(err => console.log(err)) - res.json(product); - // }); - } -}; - -module.exports.editProduct = (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), - title: req.body.title, - price: req.body.price, - description: req.body.description, - image: req.body.image, - category: req.body.category, - }); - } -}; + editProduct: (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), + title: req.body.title, + price: req.body.price, + description: req.body.description, + image: req.body.image, + category: req.body.category, + }); + } + }, -module.exports.deleteProduct = (req, res) => { - if (req.params.id == null) { - res.json({ - status: 'error', - message: 'cart id should be provided', - }); - } else { - Product.findOne({ - id: req.params.id, - }) - .select(['-_id']) - .then((product) => { - res.json(product); - }) - .catch((err) => console.log(err)); - } + deleteProduct: (req, res) => { + if (req.params.id == null) { + res.json({ + status: "error", + message: "cart id should be provided", + }); + } else { + Product.findOne({ + id: req.params.id, + }) + .select(["-_id"]) + .then((product) => { + res.json(product); + }) + .catch((err) => console.log(err)); + } + }, }; diff --git a/controller/user.js b/controller/user.js index 891a8f94..4d5d115c 100755 --- a/controller/user.js +++ b/controller/user.js @@ -1,122 +1,124 @@ -const User = require('../model/user'); +const User = require("../model/user"); -module.exports.getAllUser = (req, res) => { - const limit = Number(req.query.limit) || 0; - const sort = req.query.sort == 'desc' ? -1 : 1; +module.exports = { + getAllUser: (req, res) => { + const limit = Number(req.query.limit) || 0; + const sort = req.query.sort == "desc" ? -1 : 1; - User.find() - .select(['-_id']) - .limit(limit) - .sort({ - id: sort, - }) - .then((users) => { - res.json(users); - }) - .catch((err) => console.log(err)); -}; + User.find() + .select(["-_id"]) + .limit(limit) + .sort({ + id: sort, + }) + .then((users) => { + res.json(users); + }) + .catch((err) => console.log(err)); + }, -module.exports.getUser = (req, res) => { - const id = req.params.id; + getUser: (req, res) => { + const id = req.params.id; - User.findOne({ - id, - }) - .select(['-_id']) - .then((user) => { - res.json(user); - }) - .catch((err) => console.log(err)); -}; + User.findOne({ + id, + }) + .select(["-_id"]) + .then((user) => { + res.json(user); + }) + .catch((err) => console.log(err)); + }, -module.exports.addUser = (req, res) => { - if (typeof req.body == undefined) { - res.json({ - status: 'error', - message: 'data is undefined', - }); - } else { - let userCount = 0; - User.find() - .countDocuments(function (err, count) { - userCount = count; - }) - .then(() => { - const user = new User({ - id: userCount + 1, - email: req.body.email, - username: req.body.username, - password: req.body.password, - name: { - firstname: req.body.firstname, - lastname: req.body.lastname, - }, - address: { - city: req.body.address.city, - street: req.body.address.street, - number: req.body.number, - zipcode: req.body.zipcode, - geolocation: { - lat: req.body.address.geolocation.lat, - long: req.body.address.geolocation.long, - }, - }, - phone: req.body.phone, - }); - // user.save() - // .then(user => res.json(user)) - // .catch(err => console.log(err)) + addUser: (req, res) => { + if (typeof req.body == undefined) { + res.json({ + status: "error", + message: "data is undefined", + }); + } else { + let userCount = 0; + User.find() + .countDocuments(function (err, count) { + userCount = count; + }) + .then(() => { + const user = new User({ + id: userCount + 1, + email: req.body.email, + username: req.body.username, + password: req.body.password, + name: { + firstname: req.body.firstname, + lastname: req.body.lastname, + }, + address: { + city: req.body.address.city, + street: req.body.address.street, + number: req.body.number, + zipcode: req.body.zipcode, + geolocation: { + lat: req.body.address.geolocation.lat, + long: req.body.address.geolocation.long, + }, + }, + phone: req.body.phone, + }); + // user.save() + // .then(user => res.json(user)) + // .catch(err => console.log(err)) - res.json(user); - }); + res.json(user); + }); - //res.json({id:User.find().count()+1,...req.body}) - } -}; + //res.json({id:User.find().count()+1,...req.body}) + } + }, -module.exports.editUser = (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), - email: req.body.email, - username: req.body.username, - password: req.body.password, - name: { - firstname: req.body.firstname, - lastname: req.body.lastname, - }, - address: { - city: req.body.address.city, - street: req.body.address.street, - number: req.body.number, - zipcode: req.body.zipcode, - geolocation: { - lat: req.body.address.geolocation.lat, - long: req.body.address.geolocation.long, - }, - }, - phone: req.body.phone, - }); - } -}; + editUser: (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), + email: req.body.email, + username: req.body.username, + password: req.body.password, + name: { + firstname: req.body.firstname, + lastname: req.body.lastname, + }, + address: { + city: req.body.address.city, + street: req.body.address.street, + number: req.body.number, + zipcode: req.body.zipcode, + geolocation: { + lat: req.body.address.geolocation.lat, + long: req.body.address.geolocation.long, + }, + }, + phone: req.body.phone, + }); + } + }, -module.exports.deleteUser = (req, res) => { - if (req.params.id == null) { - res.json({ - status: 'error', - message: 'cart id should be provided', - }); - } else { - User.findOne({ id: req.params.id }) - .select(['-_id']) - .then((user) => { - res.json(user); - }) - .catch((err) => console.log(err)); - } + deleteUser: (req, res) => { + if (req.params.id == null) { + res.json({ + status: "error", + message: "cart id should be provided", + }); + } else { + User.findOne({ id: req.params.id }) + .select(["-_id"]) + .then((user) => { + res.json(user); + }) + .catch((err) => console.log(err)); + } + }, }; From 063135b817c60e1d635b2e6a93cb3e934aab661f Mon Sep 17 00:00:00 2001 From: "MUH. AHSAN" Date: Tue, 6 Jun 2023 06:21:03 +0700 Subject: [PATCH 2/2] Destruc request body, implement async await funct, update module.exports --- controller/auth.js | 49 +++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/controller/auth.js b/controller/auth.js index 15ecbfb3..50d726a7 100755 --- a/controller/auth.js +++ b/controller/auth.js @@ -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); + } + }, };