├── .gitignore ├── .DS_Store ├── uploads ├── burger.png └── Margherita-Pizza-from-Motorino-Pizza-Napoletana.jpg ├── config └── database.js ├── api ├── shops │ ├── shop.router.js │ ├── shop.service.js │ └── shop.controller.js ├── users │ ├── user.router.js │ ├── user.service.js │ └── user.controller.js ├── admin │ ├── admin.router.js │ ├── admin.service.js │ └── admin.controller.js ├── orders │ ├── order.router.js │ ├── order.controller.js │ └── order.service.js ├── products │ ├── product.router.js │ ├── product.controller.js │ └── product.service.js └── categories │ ├── category.router.js │ ├── category.service.js │ └── category.controller.js ├── package.json ├── auth └── token_validation.js └── App.js /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Maurice-Muthaka/Node-JS-Multi-Vendor-Ecommerce-API/HEAD/.DS_Store -------------------------------------------------------------------------------- /uploads/burger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Maurice-Muthaka/Node-JS-Multi-Vendor-Ecommerce-API/HEAD/uploads/burger.png -------------------------------------------------------------------------------- /uploads/Margherita-Pizza-from-Motorino-Pizza-Napoletana.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Maurice-Muthaka/Node-JS-Multi-Vendor-Ecommerce-API/HEAD/uploads/Margherita-Pizza-from-Motorino-Pizza-Napoletana.jpg -------------------------------------------------------------------------------- /config/database.js: -------------------------------------------------------------------------------- 1 | const { createPool } = require('mysql'); 2 | 3 | const pool = createPool({ 4 | port: '3306', 5 | host: 'localhost', 6 | user: 'root', 7 | password: '', 8 | database: 'vello_food', 9 | connectionLimit: 10 10 | }); 11 | 12 | module.exports = pool; -------------------------------------------------------------------------------- /api/shops/shop.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createShop, 3 | getShops, 4 | getShopById, 5 | updateShop, 6 | deleteShop, 7 | login 8 | } = require('./shop.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | router.post('/', createShop); 13 | router.get('/', getShops); 14 | router.get('/:id', checkToken, getShopById); 15 | router.put('/:id', checkToken, updateShop); 16 | router.delete('/:id', checkToken, deleteShop); 17 | router.post('/login', login); 18 | 19 | module.exports = router; -------------------------------------------------------------------------------- /api/users/user.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createUser, 3 | getUsers, 4 | getUserById, 5 | updateUser, 6 | deleteUser, 7 | login 8 | } = require('./user.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | router.post('/', createUser); 13 | router.get('/', checkToken, getUsers); 14 | router.get('/:id', checkToken, getUserById); 15 | router.put('/:id', checkToken, updateUser); 16 | router.delete('/:id', checkToken, deleteUser); 17 | router.post('/login', login); 18 | 19 | module.exports = router; -------------------------------------------------------------------------------- /api/admin/admin.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createAdmin, 3 | getAdmins, 4 | getAdminById, 5 | updateAdmin, 6 | deleteAdmin, 7 | login 8 | } = require('./admin.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | router.post('/', createAdmin); 13 | router.get('/', checkToken, getAdmins); 14 | router.get('/:id', checkToken, getAdminById); 15 | router.put('/:id', checkToken, updateAdmin); 16 | router.delete('/:id', checkToken, deleteAdmin); 17 | router.post('/login', login); 18 | 19 | module.exports = router; -------------------------------------------------------------------------------- /api/orders/order.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createOrder, 3 | getOrders, 4 | getShopOrders, 5 | getOrderById, 6 | updateOrder, 7 | deleteOrder, 8 | } = require('./order.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | router.post('/', createOrder); 13 | router.get('/', checkToken, getOrders); 14 | router.get('/shop/:id',checkToken, getShopOrders); 15 | router.get('/:id', checkToken, getOrderById); 16 | router.put('/:id', checkToken, updateOrder); 17 | router.delete('/:id', checkToken, deleteOrder); 18 | 19 | module.exports = router; -------------------------------------------------------------------------------- /api/products/product.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createProduct, 3 | getProducts, 4 | getShopProducts, 5 | getProductById, 6 | updateProduct, 7 | deleteProduct, 8 | } = require('./product.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | 13 | router.post('/', createProduct); 14 | router.get('/', getProducts); 15 | router.get('/shop/:id', checkToken, getShopProducts); 16 | router.get('/:id', getProductById); 17 | router.put('/:id', checkToken, updateProduct); 18 | router.delete('/:id', checkToken, deleteProduct); 19 | 20 | module.exports = router; -------------------------------------------------------------------------------- /api/categories/category.router.js: -------------------------------------------------------------------------------- 1 | const { 2 | createCategory, 3 | getCategories, 4 | getShopCategories, 5 | getCategoryById, 6 | updateCategory, 7 | deleteCategory, 8 | } = require('./category.controller'); 9 | const router = require('express').Router(); 10 | const { checkToken } = require('../../auth/token_validation'); 11 | 12 | router.post('/', checkToken, createCategory); 13 | router.get('/', getCategories); 14 | router.get('/shop/:id', checkToken, getShopCategories); 15 | router.get('/:id', checkToken, getCategoryById); 16 | router.put('/:id', checkToken, updateCategory); 17 | router.delete('/:id', checkToken, deleteCategory); 18 | 19 | module.exports = router; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vello_food_api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "App.js", 6 | "scripts": { 7 | "start": "nodemon app.js" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bcrypt": "^4.0.1", 14 | "body-parser": "^1.19.0", 15 | "busboy": "^0.3.1", 16 | "cors": "^2.8.5", 17 | "dotenv": "^8.2.0", 18 | "express": "^4.17.1", 19 | "fs": "0.0.1-security", 20 | "jsonwebtoken": "^8.5.1", 21 | "multer": "^1.4.2", 22 | "mysql": "^2.18.1", 23 | "os": "^0.1.1", 24 | "path": "^0.12.7" 25 | }, 26 | "devDependencies": { 27 | "node-dev": "^3.0.0", 28 | "nodemon": "^2.0.4" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /auth/token_validation.js: -------------------------------------------------------------------------------- 1 | const jwt = require('jsonwebtoken'); 2 | 3 | module.exports = { 4 | checkToken: (req, res, next) => { 5 | let token = req.get('authorization'); 6 | if (token) { 7 | token = token.slice(7); 8 | jwt.verify(token, 'qwe1234', (err, decoded) => { 9 | if (err) { 10 | return res.json({ 11 | success: 0, 12 | message: 'Invalid token...' 13 | }); 14 | }else { 15 | req.decoded = decoded; 16 | next(); 17 | } 18 | }); 19 | }else { 20 | return res.json({ 21 | success: 0, 22 | message: 'Access denied, Not autorised' 23 | }); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /App.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const express = require('express'); 3 | const app = express(); 4 | const cors = require("cors"); 5 | const bodyParser = require("body-parser"); 6 | const multer = require('multer'); 7 | const path = require("path"); 8 | 9 | const userRouter = require('./api/users/user.router'); 10 | const shopRouter = require('./api/shops/shop.router'); 11 | const adminRouter = require('./api/admin/admin.router'); 12 | const categoryRouter = require('./api/categories/category.router'); 13 | const productRouter = require('./api/products/product.router'); 14 | const orderRouter = require('./api/orders/order.router'); 15 | 16 | app.use(bodyParser.json()); 17 | app.use(bodyParser.urlencoded({ extended: true })); 18 | app.use(express.json()); 19 | app.use(cors({ origin: "*" })); 20 | app.use('/uploads', express.static('uploads')); 21 | 22 | app.use('/api/users', userRouter); 23 | app.use('/api/shops', shopRouter); 24 | app.use('/api/admin', adminRouter); 25 | app.use('/api/categories', categoryRouter); 26 | app.use('/api/products', productRouter); 27 | app.use('/api/orders', orderRouter); 28 | 29 | 30 | 31 | app.listen(3005, () => { 32 | console.log('server up and running...'); 33 | }); -------------------------------------------------------------------------------- /api/users/user.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into users(username, email, mobile, password) values(?, ?, ?, ?)', 7 | [ data.username, data.email, data.mobile, data.password], 8 | (error, results, fields) => { 9 | if (error) { 10 | return callBack(error); 11 | } 12 | return callBack(null, results); 13 | } 14 | ); 15 | }, 16 | getUsers: callBack => { 17 | pool.query( 18 | `select id, username, email, mobile from users`, 19 | [], 20 | (error, results, fields) => { 21 | if (error) { 22 | return callBack(error); 23 | } 24 | return callBack(null, results); 25 | } 26 | ); 27 | }, 28 | getUserById: (id, callBack) => { 29 | pool.query( 30 | `select id, username, email, mobile from users where id = ?`, 31 | [id], 32 | (error, results, fields) => { 33 | if (error) { 34 | return callBack(error); 35 | } 36 | return callBack(null, results[0]); 37 | } 38 | ); 39 | }, 40 | updateUser: (id, data, callBack) => { 41 | pool.query( 42 | `update users set username = ?, email = ?, mobile = ?, password = ? where id = ?`, 43 | [ 44 | data.username, 45 | data.email, 46 | data.mobile, 47 | data.password, 48 | id 49 | ], 50 | (error, results, fields) => { 51 | if (error) { 52 | return callBack(error); 53 | } 54 | return callBack(null, results[0]); 55 | } 56 | ); 57 | }, 58 | deleteUser: (id, callBack) => { 59 | pool.query( 60 | `delete from users where id = ?`, 61 | [id], 62 | (error, results, fields) => { 63 | if (error) { 64 | return callBack(error); 65 | } 66 | return callBack(null, results[0]); 67 | } 68 | ); 69 | }, 70 | getUserByEmail: (email, callBack) => { 71 | pool.query( 72 | `select * from users where email = ?`, 73 | [email], 74 | (error, results, fields) => { 75 | if (error) { 76 | return callBack(error); 77 | } 78 | return callBack(null, results[0]); 79 | } 80 | ); 81 | }, 82 | }; -------------------------------------------------------------------------------- /api/admin/admin.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into admins(username, email, mobile, password) values(?, ?, ?, ?)', 7 | [ data.username, data.email, data.mobile, data.password], 8 | (error, results, fields) => { 9 | if (error) { 10 | return callBack(error); 11 | } 12 | return callBack(null, results); 13 | } 14 | ); 15 | }, 16 | getAdmins: callBack => { 17 | pool.query( 18 | `select id, username, email, mobile from admins`, 19 | [], 20 | (error, results, fields) => { 21 | if (error) { 22 | return callBack(error); 23 | } 24 | return callBack(null, results); 25 | } 26 | ); 27 | }, 28 | getAdminById: (id, callBack) => { 29 | pool.query( 30 | `select id, username, email, mobile from admins where id = ?`, 31 | [id], 32 | (error, results, fields) => { 33 | if (error) { 34 | return callBack(error); 35 | } 36 | return callBack(null, results[0]); 37 | } 38 | ); 39 | }, 40 | updateAdmin: (id, data, callBack) => { 41 | pool.query( 42 | `update admins set username = ?, email = ?, mobile = ?, password = ? where id = ?`, 43 | [ 44 | data.username, 45 | data.email, 46 | data.mobile, 47 | data.password, 48 | id 49 | ], 50 | (error, results, fields) => { 51 | if (error) { 52 | return callBack(error); 53 | } 54 | return callBack(null, results[0]); 55 | } 56 | ); 57 | }, 58 | deleteAdmin: (id, callBack) => { 59 | pool.query( 60 | `delete from admins where id = ?`, 61 | [id], 62 | (error, results, fields) => { 63 | if (error) { 64 | return callBack(error); 65 | } 66 | return callBack(null, results[0]); 67 | } 68 | ); 69 | }, 70 | getAdminByEmail: (email, callBack) => { 71 | pool.query( 72 | `select * from admins where email = ?`, 73 | [email], 74 | (error, results, fields) => { 75 | if (error) { 76 | return callBack(error); 77 | } 78 | return callBack(null, results[0]); 79 | } 80 | ); 81 | }, 82 | }; -------------------------------------------------------------------------------- /api/shops/shop.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into shops(shop_name, email, mobile, address, open, close, password) values(?, ?, ?, ?, ?, ?, ?)', 7 | [ 8 | data.shop_name, 9 | data.email, 10 | data.mobile, 11 | data.address, 12 | data.open, 13 | data.close, 14 | data.password 15 | ], 16 | (error, results, fields) => { 17 | if (error) { 18 | return callBack(error); 19 | } 20 | return callBack(null, results); 21 | } 22 | ); 23 | }, 24 | getShops: callBack => { 25 | pool.query( 26 | `select id, shop_name, email, mobile, address, open, close from shops`, 27 | [], 28 | (error, results, fields) => { 29 | if (error) { 30 | return callBack(error); 31 | } 32 | return callBack(null, results); 33 | } 34 | ); 35 | }, 36 | getShopById: (id, callBack) => { 37 | pool.query( 38 | `select id, shop_name, email, mobile, address, open, close from shops where id = ?`, 39 | [id], 40 | (error, results, fields) => { 41 | if (error) { 42 | return callBack(error); 43 | } 44 | return callBack(null, results[0]); 45 | } 46 | ); 47 | }, 48 | updateShop: (id, data, callBack) => { 49 | pool.query( 50 | `update shops set shop_name = ?, email = ?, mobile = ?, address = ?, open = ?, close = ?, password = ? where id = ?`, 51 | [ 52 | data.shop_name, 53 | data.email, 54 | data.mobile, 55 | data.address, 56 | data.open, 57 | data.close, 58 | data.password, 59 | id 60 | ], 61 | (error, results, fields) => { 62 | if (error) { 63 | return callBack(error); 64 | } 65 | return callBack(null, results[0]); 66 | } 67 | ); 68 | }, 69 | deleteShop: (id, callBack) => { 70 | pool.query( 71 | `delete from shops where id = ?`, 72 | [id], 73 | (error, results, fields) => { 74 | if (error) { 75 | return callBack(error); 76 | } 77 | return callBack(null, results[0]); 78 | } 79 | ); 80 | }, 81 | getShopByEmail: (email, callBack) => { 82 | pool.query( 83 | `select * from shops where email = ?`, 84 | [email], 85 | (error, results, fields) => { 86 | if (error) { 87 | return callBack(error); 88 | } 89 | return callBack(null, results[0]); 90 | } 91 | ); 92 | }, 93 | }; -------------------------------------------------------------------------------- /api/categories/category.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into categories(shop_id, category_name) values(?, ?)', 7 | [ 8 | data.shop_id, 9 | data.category_name 10 | ], 11 | (error, results, fields) => { 12 | if (error) { 13 | return callBack(error); 14 | } 15 | return callBack(null, results); 16 | } 17 | ); 18 | }, 19 | getCategories: callBack => { 20 | pool.query( 21 | `select categories.id, categories.shop_id, shops.shop_name, categories.category_name, categories.created_at from categories inner join shops on categories.shop_id = shops.id`, 22 | [], 23 | (error, results, fields) => { 24 | if (error) { 25 | return callBack(error); 26 | } 27 | return callBack(null, results); 28 | } 29 | ); 30 | }, 31 | getShopCategories: (id, callBack) => { 32 | pool.query( 33 | // `select id, shop_id, name, created_at from categories where id = ?`, 34 | `select categories.id, categories.shop_id, shops.shop_name, categories.category_name, categories.created_at from categories inner join shops on categories.shop_id = shops.id where categories.shop_id= ?`, 35 | [id], 36 | (error, results, fields) => { 37 | if (error) { 38 | return callBack(error); 39 | } 40 | return callBack(null, results); 41 | } 42 | ); 43 | }, 44 | getCategoryById: (id, callBack) => { 45 | pool.query( 46 | // `select id, shop_id, name, created_at from categories where id = ?`, 47 | `select categories.id, shops.shop_name, categories.category_name, categories.created_at from categories inner join shops on categories.shop_id = shops.id where categories.id= ?`, 48 | [id], 49 | (error, results, fields) => { 50 | if (error) { 51 | return callBack(error); 52 | } 53 | return callBack(null, results[0]); 54 | } 55 | ); 56 | }, 57 | updateCategory: (id, data, callBack) => { 58 | pool.query( 59 | `update categories set category_name = ? where id = ?`, 60 | [ 61 | data.category_name, 62 | id 63 | ], 64 | (error, results, fields) => { 65 | if (error) { 66 | return callBack(error); 67 | } 68 | return callBack(null, results[0]); 69 | } 70 | ); 71 | }, 72 | deleteCategory: (id, callBack) => { 73 | pool.query( 74 | `delete from categories where id = ?`, 75 | [id], 76 | (error, results, fields) => { 77 | if (error) { 78 | return callBack(error); 79 | } 80 | return callBack(null, results[0]); 81 | } 82 | ); 83 | }, 84 | }; -------------------------------------------------------------------------------- /api/products/product.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getProducts, 3 | getShopProducts, 4 | getProductById, 5 | updateProduct, 6 | deleteProduct, 7 | } = require('./product.service'); 8 | 9 | module.exports = { 10 | createProduct: (req, res) => { 11 | const body = req.body; 12 | create(body, (err, results) => { 13 | if (err) { 14 | console.log(err); 15 | return res.status(500).json({ 16 | success: 0, 17 | message: 'Database connection error' 18 | }); 19 | } 20 | return res.status(200).json({ 21 | success: 1, 22 | message: 'Product saved successfully !' 23 | }); 24 | }); 25 | }, 26 | getProducts: (req, res) => { 27 | getProducts((err, results) => { 28 | if (err) { 29 | console.log(err); 30 | return; 31 | } 32 | if (!results) { 33 | return res.json({ 34 | success: 0, 35 | message: 'No Product found' 36 | }); 37 | } 38 | return res.json( 39 | results 40 | ); 41 | }); 42 | }, 43 | getShopProducts: (req, res) => { 44 | const id = req.params.id; 45 | getShopProducts(id, (err, results) => { 46 | if (err) { 47 | console.log(err); 48 | return; 49 | } 50 | if (!results) { 51 | return res.json({ 52 | success: 0, 53 | message: 'No product found !' 54 | }); 55 | } 56 | return res.json( 57 | results 58 | ); 59 | }); 60 | }, 61 | getProductById: (req, res) => { 62 | const id = req.params.id; 63 | getProductById(id, (err, results) => { 64 | if (err) { 65 | console.log(err); 66 | return; 67 | } 68 | if (!results) { 69 | return res.json({ 70 | success: 0, 71 | message: 'Product not found' 72 | }); 73 | } 74 | return res.json( 75 | results 76 | ); 77 | }); 78 | }, 79 | updateProduct: (req, res) => { 80 | const id = req.params.id; 81 | const body = req.body; 82 | updateProduct(id, body, (err, results) => { 83 | if (err) { 84 | console.log(err); 85 | return res.status(500).json({ 86 | success: 0, 87 | message: 'Oops something went wrong' 88 | }); 89 | } 90 | // if (!results) { 91 | // return res.json({ 92 | // success: 0, 93 | // message: 'User not found' 94 | // }); 95 | // } 96 | return res.status(200).json({ 97 | success: 1, 98 | message: 'Product updated successfully' 99 | }); 100 | }); 101 | }, 102 | deleteProduct: (req, res) => { 103 | const id = req.params.id; 104 | deleteProduct(id, (err, results) => { 105 | if (err) { 106 | console.log(err); 107 | return; 108 | } 109 | // if (!results) { 110 | // return res.json({ 111 | // success: 0, 112 | // message: 'User not found' 113 | // }); 114 | // } 115 | return res.json({ 116 | success: 1, 117 | data: 'Product deleted successfully' 118 | }); 119 | }); 120 | } 121 | } -------------------------------------------------------------------------------- /api/orders/order.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getOrders, 3 | getShopOrders, 4 | getOrderById, 5 | updateOrder, 6 | deleteOrder, 7 | } = require('./order.service'); 8 | 9 | module.exports = { 10 | createOrder: (req, res) => { 11 | const body = req.body; 12 | create(body, (err, results) => { 13 | if (err) { 14 | console.log(err); 15 | return res.status(500).json({ 16 | success: 0, 17 | message: 'Database connection failed' 18 | }); 19 | } 20 | return res.status(200).json({ 21 | success: 1, 22 | message: 'Order placed successfully !' 23 | }); 24 | }); 25 | }, 26 | getOrders: (req, res) => { 27 | getOrders((err, results) => { 28 | if (err) { 29 | console.log(err); 30 | return; 31 | } 32 | if (!results) { 33 | return res.json({ 34 | success: 0, 35 | message: 'No Category found' 36 | }); 37 | } 38 | return res.json({ 39 | success: 1, 40 | data: results 41 | }); 42 | }); 43 | }, 44 | getShopOrders: (req, res) => { 45 | const id = req.params.id; 46 | getShopOrders(id, (err, results) => { 47 | if (err) { 48 | console.log(err); 49 | return; 50 | } 51 | if (!results) { 52 | return res.json({ 53 | success: 0, 54 | message: 'No order found' 55 | }); 56 | } 57 | return res.json( 58 | results 59 | ); 60 | }); 61 | }, 62 | getOrderById: (req, res) => { 63 | const id = req.params.id; 64 | getOrderById(id, (err, results) => { 65 | if (err) { 66 | console.log(err); 67 | return; 68 | } 69 | if (!results) { 70 | return res.json({ 71 | success: 0, 72 | message: 'Category not found' 73 | }); 74 | } 75 | return res.json({ 76 | success: 1, 77 | data: results 78 | }); 79 | }); 80 | }, 81 | updateOrder: (req, res) => { 82 | const id = req.params.id; 83 | const body = req.body; 84 | updateOrder(id, body, (err, results) => { 85 | if (err) { 86 | console.log(err); 87 | return res.status(500).json({ 88 | success: 0, 89 | message: 'Oops something went wrong' 90 | }); 91 | } 92 | // if (!results) { 93 | // return res.json({ 94 | // success: 0, 95 | // message: 'User not found' 96 | // }); 97 | // } 98 | return res.status(200).json({ 99 | success: 1, 100 | message: 'Category updated successfully' 101 | }); 102 | }); 103 | }, 104 | deleteOrder: (req, res) => { 105 | const id = req.params.id; 106 | deleteOrder(id, (err, results) => { 107 | if (err) { 108 | console.log(err); 109 | return; 110 | } 111 | // if (!results) { 112 | // return res.json({ 113 | // success: 0, 114 | // message: 'User not found' 115 | // }); 116 | // } 117 | return res.json({ 118 | success: 1, 119 | data: 'Category deleted successfully' 120 | }); 121 | }); 122 | } 123 | } -------------------------------------------------------------------------------- /api/categories/category.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getCategories, 3 | getShopCategories, 4 | getCategoryById, 5 | updateCategory, 6 | deleteCategory, 7 | } = require('./category.service'); 8 | 9 | module.exports = { 10 | createCategory: (req, res) => { 11 | const body = req.body; 12 | create(body, (err, results) => { 13 | if (err) { 14 | console.log(err); 15 | return res.status(500).json({ 16 | success: 0, 17 | message: 'Database connection error' 18 | }); 19 | } 20 | return res.status(200).json({ 21 | success: 1, 22 | message: 'Category saved successfully !' 23 | }); 24 | }); 25 | }, 26 | getCategories: (req, res) => { 27 | getCategories((err, results) => { 28 | if (err) { 29 | console.log(err); 30 | return; 31 | } 32 | if (!results) { 33 | return res.json({ 34 | success: 0, 35 | message: 'No Category found' 36 | }); 37 | } 38 | return res.json({ 39 | success: 1, 40 | data: results 41 | }); 42 | }); 43 | }, 44 | getShopCategories: (req, res) => { 45 | const id = req.params.id; 46 | getShopCategories(id, (err, results) => { 47 | if (err) { 48 | console.log(err); 49 | return; 50 | } 51 | if (!results) { 52 | return res.json({ 53 | success: 0, 54 | message: 'No category found' 55 | }); 56 | } 57 | return res.json( 58 | results 59 | ); 60 | }); 61 | }, 62 | getCategoryById: (req, res) => { 63 | const id = req.params.id; 64 | getCategoryById(id, (err, results) => { 65 | if (err) { 66 | console.log(err); 67 | return; 68 | } 69 | if (!results) { 70 | return res.json({ 71 | success: 0, 72 | message: 'Category not found' 73 | }); 74 | } 75 | return res.json({ 76 | success: 1, 77 | data: results 78 | }); 79 | }); 80 | }, 81 | updateCategory: (req, res) => { 82 | const id = req.params.id; 83 | const body = req.body; 84 | updateCategory(id, body, (err, results) => { 85 | if (err) { 86 | console.log(err); 87 | return res.status(500).json({ 88 | success: 0, 89 | message: 'Oops something went wrong' 90 | }); 91 | } 92 | // if (!results) { 93 | // return res.json({ 94 | // success: 0, 95 | // message: 'User not found' 96 | // }); 97 | // } 98 | return res.status(200).json({ 99 | success: 1, 100 | message: 'Category updated successfully' 101 | }); 102 | }); 103 | }, 104 | deleteCategory: (req, res) => { 105 | const id = req.params.id; 106 | deleteCategory(id, (err, results) => { 107 | if (err) { 108 | console.log(err); 109 | return; 110 | } 111 | // if (!results) { 112 | // return res.json({ 113 | // success: 0, 114 | // message: 'User not found' 115 | // }); 116 | // } 117 | return res.json({ 118 | success: 1, 119 | data: 'Category deleted successfully' 120 | }); 121 | }); 122 | } 123 | } -------------------------------------------------------------------------------- /api/orders/order.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into orders(user_id, product_id, quantity, address, transaction_no) values(?, ?, ?, ?, ?)', 7 | [ 8 | data.user_id, 9 | data.product_id, 10 | data.quantity, 11 | data.address, 12 | data.transaction_no 13 | ], 14 | (error, results, fields) => { 15 | if (error) { 16 | return callBack(error); 17 | } 18 | return callBack(null, results); 19 | } 20 | ); 21 | }, 22 | getOrders: callBack => { 23 | pool.query( 24 | `select orders.id, users.username, products.item_name, categories.category_name, shops.shop_name, products.image, orders.quantity, products.price, products.duration, orders.created_at from orders inner join users on orders.user_id = users.id inner join products on orders.product_id = products.id inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id`, 25 | [], 26 | (error, results, fields) => { 27 | if (error) { 28 | return callBack(error); 29 | } 30 | return callBack(null, results); 31 | } 32 | ); 33 | }, 34 | getShopOrders: (id, callBack) => { 35 | pool.query( 36 | // `select id, shop_id, name, created_at from categories where id = ?`, 37 | `select orders.id, users.username, products.item_name, categories.category_name, shops.shop_name, products.image, orders.quantity, products.price, products.duration, orders.created_at from orders inner join users on orders.user_id = users.id inner join products on orders.product_id = products.id inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id where products.shop_id= ?`, 38 | [id], 39 | (error, results, fields) => { 40 | if (error) { 41 | return callBack(error); 42 | } 43 | return callBack(null, results); 44 | } 45 | ); 46 | }, 47 | getOrderById: (id, callBack) => { 48 | pool.query( 49 | // `select id, shop_id, name, created_at from categories where id = ?`, 50 | `select orders.id, users.username, products.item_name, categories.category_name, shops.shop_name, products.image, orders.quantity, products.price, products.duration, orders.created_at from orders inner join users on orders.user_id = users.id inner join products on orders.product_id = products.id inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id where orders.id= ?`, 51 | [id], 52 | (error, results, fields) => { 53 | if (error) { 54 | return callBack(error); 55 | } 56 | return callBack(null, results[0]); 57 | } 58 | ); 59 | }, 60 | updateOrder: (id, data, callBack) => { 61 | pool.query( 62 | `update orders set product_id = ?, quantity = ? where id = ?`, 63 | [ 64 | data.product_id, 65 | data.quantity, 66 | id 67 | ], 68 | (error, results, fields) => { 69 | if (error) { 70 | return callBack(error); 71 | } 72 | return callBack(null, results[0]); 73 | } 74 | ); 75 | }, 76 | deleteOrder: (id, callBack) => { 77 | pool.query( 78 | `delete from orders where id = ?`, 79 | [id], 80 | (error, results, fields) => { 81 | if (error) { 82 | return callBack(error); 83 | } 84 | return callBack(null, results[0]); 85 | } 86 | ); 87 | }, 88 | }; -------------------------------------------------------------------------------- /api/products/product.service.js: -------------------------------------------------------------------------------- 1 | const pool = require('../../config/database'); 2 | 3 | module.exports = { 4 | create: (data, callBack) => { 5 | pool.query( 6 | 'insert into products(shop_id, category_id, item_name, image, price, duration, description) values(?, ?, ?, ?, ?, ?, ?)', 7 | [ 8 | data.shop_id, 9 | data.category_id, 10 | data.item_name, 11 | data.image, 12 | data.price, 13 | data.duration, 14 | data.description 15 | ], 16 | (error, results, fields) => { 17 | if (error) { 18 | return callBack(error); 19 | } 20 | return callBack(null, results); 21 | } 22 | ); 23 | }, 24 | getProducts: callBack => { 25 | pool.query( 26 | `select products.id, products.shop_id, shops.shop_name, categories.category_name, products.item_name, products.image, products.price, products.duration, products.description, products.created_at from products inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id`, 27 | [], 28 | (error, results, fields) => { 29 | if (error) { 30 | return callBack(error); 31 | } 32 | return callBack(null, results); 33 | } 34 | ); 35 | }, 36 | getShopProducts: (id, callBack) => { 37 | pool.query( 38 | // `select id, shop_id, name, created_at from categories where id = ?`, 39 | `select products.id, products.shop_id, shops.shop_name, categories.category_name, products.item_name, products.image, products.price, products.duration, products.description, products.created_at from products inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id where products.shop_id = ?`, 40 | [id], 41 | (error, results, fields) => { 42 | if (error) { 43 | return callBack(error); 44 | } 45 | return callBack(null, results); 46 | } 47 | ); 48 | }, 49 | getProductById: (id, callBack) => { 50 | pool.query( 51 | // `select id, shop_id, name, created_at from categories where id = ?`, 52 | `select products.id, shops.shop_name, categories.category_name, products.item_name, products.image, products.price, products.duration, products.description, products.created_at from products inner join shops on products.shop_id = shops.id inner join categories on products.category_id = categories.id where products.id = ?`, 53 | [id], 54 | (error, results, fields) => { 55 | if (error) { 56 | return callBack(error); 57 | } 58 | return callBack(null, results[0]); 59 | } 60 | ); 61 | }, 62 | updateProduct: (id, data, callBack) => { 63 | pool.query( 64 | `update products set category_id = ?, item_name = ?, image = ?, price = ?, duration = ?, description = ? where id = ?`, 65 | [ 66 | data.category_id, 67 | data.item_name, 68 | data.image, 69 | data.price, 70 | data.duration, 71 | data.description, 72 | id 73 | ], 74 | (error, results, fields) => { 75 | if (error) { 76 | return callBack(error); 77 | } 78 | return callBack(null, results[0]); 79 | } 80 | ); 81 | }, 82 | deleteProduct: (id, callBack) => { 83 | pool.query( 84 | `delete from products where id = ?`, 85 | [id], 86 | (error, results, fields) => { 87 | if (error) { 88 | return callBack(error); 89 | } 90 | return callBack(null, results[0]); 91 | } 92 | ); 93 | }, 94 | }; -------------------------------------------------------------------------------- /api/admin/admin.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getAdmins, 3 | getAdminById, 4 | updateAdmin, 5 | deleteAdmin, 6 | getAdminByEmail 7 | } = require('./admin.service'); 8 | 9 | const { genSaltSync, hashSync, compareSync } = require('bcrypt'); 10 | const { sign } = require('jsonwebtoken'); 11 | 12 | module.exports = { 13 | createAdmin: (req, res) => { 14 | const body = req.body; 15 | const salt = genSaltSync(10); 16 | body.password = hashSync(body.password, salt); 17 | create(body, (err, results) => { 18 | if (err) { 19 | console.log(err); 20 | return res.status(500).json({ 21 | success: 0, 22 | message: 'Database connection error' 23 | }); 24 | } 25 | return res.status(200).json({ 26 | success: 1, 27 | data: results 28 | }); 29 | }); 30 | }, 31 | getAdmins: (req, res) => { 32 | getAdmins((err, results) => { 33 | if (err) { 34 | console.log(err); 35 | return; 36 | } 37 | if (!results) { 38 | return res.json({ 39 | success: 0, 40 | message: 'No User not found' 41 | }); 42 | } 43 | return res.json({ 44 | success: 1, 45 | data: results 46 | }); 47 | }); 48 | }, 49 | getAdminById: (req, res) => { 50 | const id = req.params.id; 51 | getAdminById(id, (err, results) => { 52 | if (err) { 53 | console.log(err); 54 | return; 55 | } 56 | if (!results) { 57 | return res.json({ 58 | success: 0, 59 | message: 'User not found' 60 | }); 61 | } 62 | return res.json({ 63 | success: 1, 64 | data: results 65 | }); 66 | }); 67 | }, 68 | updateAdmin: (req, res) => { 69 | const id = req.params.id; 70 | const body = req.body; 71 | const salt = genSaltSync(10); 72 | body.password = hashSync(body.password, salt); 73 | updateAdmin(id, body, (err, results) => { 74 | if (err) { 75 | console.log(err); 76 | return res.status(500).json({ 77 | success: 0, 78 | message: 'Oops something went wrong' 79 | }); 80 | } 81 | // if (!results) { 82 | // return res.json({ 83 | // success: 0, 84 | // message: 'User not found' 85 | // }); 86 | // } 87 | return res.status(200).json({ 88 | success: 1, 89 | message: 'User updated successfully' 90 | }); 91 | }); 92 | }, 93 | deleteAdmin: (req, res) => { 94 | const id = req.params.id; 95 | deleteAdmin(id, (err, results) => { 96 | if (err) { 97 | console.log(err); 98 | return; 99 | } 100 | // if (!results) { 101 | // return res.json({ 102 | // success: 0, 103 | // message: 'User not found' 104 | // }); 105 | // } 106 | return res.json({ 107 | success: 1, 108 | data: 'User deleted successfully' 109 | }); 110 | }); 111 | }, 112 | login: (req, res) => { 113 | const body = req.body; 114 | getAdminByEmail(body.email, (err, results) => { 115 | if (err) { 116 | console.log(err); 117 | } 118 | if (!results) { 119 | return res.json({ 120 | success: 0, 121 | message: 'User not found' 122 | }); 123 | } 124 | const result = compareSync(body.password, results.password); 125 | if (result) { 126 | results.password = undefined; 127 | const jsontoken = sign({ result: results}, 'qwe1234', { 128 | expiresIn: '1h' 129 | }); 130 | return res.json({ 131 | success: 1, 132 | message: 'Login successfully', 133 | token: jsontoken 134 | }); 135 | } else { 136 | return res.json({ 137 | success: 0, 138 | message: 'Email or password incorrect' 139 | }); 140 | } 141 | }); 142 | } 143 | } -------------------------------------------------------------------------------- /api/shops/shop.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getShops, 3 | getShopById, 4 | updateShop, 5 | deleteShop, 6 | getShopByEmail 7 | } = require('./shop.service'); 8 | 9 | const { genSaltSync, hashSync, compareSync } = require('bcrypt'); 10 | const { sign } = require('jsonwebtoken'); 11 | 12 | module.exports = { 13 | createShop: (req, res) => { 14 | const body = req.body; 15 | const salt = genSaltSync(10); 16 | body.password = hashSync(body.password, salt); 17 | create(body, (err, results) => { 18 | if (err) { 19 | console.log(err); 20 | return res.status(500).json({ 21 | success: 0, 22 | message: 'Database connection error' 23 | }); 24 | } 25 | return res.status(200).json({ 26 | success: 1, 27 | message: "Account created successfully", 28 | data: results 29 | }); 30 | }); 31 | }, 32 | getShops: (req, res) => { 33 | getShops((err, results) => { 34 | if (err) { 35 | console.log(err); 36 | return; 37 | } 38 | if (!results) { 39 | return res.json({ 40 | success: 0, 41 | message: 'No User found' 42 | }); 43 | } 44 | return res.json({ 45 | success: 1, 46 | data: results 47 | }); 48 | }); 49 | }, 50 | getShopById: (req, res) => { 51 | const id = req.params.id; 52 | getShopById(id, (err, results) => { 53 | if (err) { 54 | console.log(err); 55 | return; 56 | } 57 | if (!results) { 58 | return res.json({ 59 | success: 0, 60 | message: 'User not found' 61 | }); 62 | } 63 | return res.json({ 64 | success: 1, 65 | data: results 66 | }); 67 | }); 68 | }, 69 | updateShop: (req, res) => { 70 | const id = req.params.id; 71 | const body = req.body; 72 | const salt = genSaltSync(10); 73 | body.password = hashSync(body.password, salt); 74 | updateShop(id, body, (err, results) => { 75 | if (err) { 76 | console.log(err); 77 | return res.status(500).json({ 78 | success: 0, 79 | message: 'Oops something went wrong' 80 | }); 81 | } 82 | // if (!results) { 83 | // return res.json({ 84 | // success: 0, 85 | // message: 'No User found' 86 | // }); 87 | // } 88 | return res.status(200).json({ 89 | success: 1, 90 | message: 'User updated successfully' 91 | }); 92 | }); 93 | }, 94 | deleteShop: (req, res) => { 95 | const id = req.params.id; 96 | deleteShop(id, (err, results) => { 97 | if (err) { 98 | console.log(err); 99 | return; 100 | } 101 | // if (!results) { 102 | // return res.json({ 103 | // success: 0, 104 | // message: 'User not found' 105 | // }); 106 | // } 107 | return res.json({ 108 | success: 1, 109 | data: 'User deleted successfully' 110 | }); 111 | }); 112 | }, 113 | login: (req, res) => { 114 | const body = req.body; 115 | getShopByEmail(body.email, (err, results) => { 116 | if (err) { 117 | console.log(err); 118 | } 119 | if (!results) { 120 | return res.json({ 121 | success: 0, 122 | message: 'User not found' 123 | }); 124 | } 125 | const result = compareSync(body.password, results.password); 126 | if (result) { 127 | results.password = undefined; 128 | const jsontoken = sign({ result: results}, 'qwe1234', { 129 | expiresIn: '12h' 130 | }); 131 | return res.json({ 132 | success: 1, 133 | message: 'Login successfully', 134 | token: jsontoken, 135 | data: results 136 | }); 137 | } else { 138 | return res.json({ 139 | success: 0, 140 | message: 'Email or password incorrect' 141 | }); 142 | } 143 | }); 144 | } 145 | } -------------------------------------------------------------------------------- /api/users/user.controller.js: -------------------------------------------------------------------------------- 1 | const { create, 2 | getUsers, 3 | getUserById, 4 | updateUser, 5 | deleteUser, 6 | getUserByEmail 7 | } = require('./user.service'); 8 | 9 | const { genSaltSync, hashSync, compareSync } = require('bcrypt'); 10 | const { sign } = require('jsonwebtoken'); 11 | 12 | module.exports = { 13 | createUser: (req, res) => { 14 | const body = req.body; 15 | const salt = genSaltSync(10); 16 | body.password = hashSync(body.password, salt); 17 | create(body, (err, results) => { 18 | if (err) { 19 | console.log(err); 20 | return res.status(500).json({ 21 | success: 0, 22 | message: 'Database connection error' 23 | }); 24 | } 25 | return res.status(200).json({ 26 | success: 1, 27 | message: "Account created successfully", 28 | data: results 29 | }); 30 | }); 31 | }, 32 | getUsers: (req, res) => { 33 | getUsers((err, results) => { 34 | if (err) { 35 | console.log(err); 36 | return; 37 | } 38 | if (!results) { 39 | return res.json({ 40 | success: 0, 41 | message: 'No User not found' 42 | }); 43 | } 44 | return res.json({ 45 | success: 1, 46 | data: results 47 | }); 48 | }); 49 | }, 50 | getUserById: (req, res) => { 51 | const id = req.params.id; 52 | getUserById(id, (err, results) => { 53 | if (err) { 54 | console.log(err); 55 | return; 56 | } 57 | if (!results) { 58 | return res.json({ 59 | success: 0, 60 | message: 'User not found' 61 | }); 62 | } 63 | return res.json({ 64 | success: 1, 65 | data: results 66 | }); 67 | }); 68 | }, 69 | updateUser: (req, res) => { 70 | const id = req.params.id; 71 | const body = req.body; 72 | const salt = genSaltSync(10); 73 | body.password = hashSync(body.password, salt); 74 | updateUser(id, body, (err, results) => { 75 | if (err) { 76 | console.log(err); 77 | return res.status(500).json({ 78 | success: 0, 79 | message: 'Oops something went wrong' 80 | }); 81 | } 82 | if (!results) { 83 | return res.json({ 84 | success: 0, 85 | message: 'User not found' 86 | }); 87 | } 88 | return res.status(200).json({ 89 | success: 1, 90 | message: 'User updated successfully' 91 | }); 92 | }); 93 | }, 94 | deleteUser: (req, res) => { 95 | const id = req.params.id; 96 | deleteUser(id, (err, results) => { 97 | if (err) { 98 | console.log(err); 99 | return; 100 | } 101 | if (!results) { 102 | return res.json({ 103 | success: 0, 104 | message: 'User not found' 105 | }); 106 | } 107 | return res.json({ 108 | success: 1, 109 | data: 'User deleted successfully' 110 | }); 111 | }); 112 | }, 113 | login: (req, res) => { 114 | const body = req.body; 115 | getUserByEmail(body.email, (err, results) => { 116 | if (err) { 117 | console.log(err); 118 | } 119 | if (!results) { 120 | return res.json({ 121 | success: 0, 122 | message: 'User not found, please signup to access our services' 123 | }); 124 | } 125 | const result = compareSync(body.password, results.password); 126 | if (result) { 127 | results.password = undefined; 128 | const jsontoken = sign({ result: results}, 'qwe1234', { 129 | expiresIn: '24h' 130 | }); 131 | return res.json({ 132 | success: 1, 133 | message: 'Login successfully', 134 | token: jsontoken, 135 | data: results 136 | }); 137 | } else { 138 | return res.json({ 139 | success: 0, 140 | message: 'Email or password incorrect' 141 | }); 142 | } 143 | }); 144 | } 145 | } --------------------------------------------------------------------------------