diff options
author | Mateja <mail@matejamaric.com> | 2021-07-11 13:32:43 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-07-11 13:32:43 +0200 |
commit | 3fbd440fe39a2677a12c234f06aaa1bae6a197a4 (patch) | |
tree | b27e26d567a5e8106d597a84fd1d664e61a08cd8 | |
parent | 54677c68f50b7c105a1f6cc5e9593877781fc8d6 (diff) | |
download | mevn-ecommerce-3fbd440fe39a2677a12c234f06aaa1bae6a197a4.tar.gz mevn-ecommerce-3fbd440fe39a2677a12c234f06aaa1bae6a197a4.zip |
Added Product routes, model and boilerplate controller.
-rw-r--r-- | server/controllers/example.js | 7 | ||||
-rw-r--r-- | server/controllers/products.js | 23 | ||||
-rw-r--r-- | server/models/Product.js | 26 | ||||
-rw-r--r-- | server/models/User.js | 4 | ||||
-rw-r--r-- | server/routes/api.js | 18 |
5 files changed, 68 insertions, 10 deletions
diff --git a/server/controllers/example.js b/server/controllers/example.js deleted file mode 100644 index 400bafc..0000000 --- a/server/controllers/example.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - index(req, res) { - res.json({ - status: "success" - }); - } -}; diff --git a/server/controllers/products.js b/server/controllers/products.js new file mode 100644 index 0000000..6265e27 --- /dev/null +++ b/server/controllers/products.js @@ -0,0 +1,23 @@ +module.exports = { + + index(req, res) { + res.json({products: "index"}); + }, + + show(req, res) { + res.json({products: "show"}); + }, + + store(req, res) { + res.json({products: "store"}); + }, + + update(req, res) { + res.json({products: "update"}); + }, + + destroy(req, res) { + res.json({products: "destroy"}); + }, + +}; diff --git a/server/models/Product.js b/server/models/Product.js new file mode 100644 index 0000000..f825a8b --- /dev/null +++ b/server/models/Product.js @@ -0,0 +1,26 @@ +const mongoose = require('mongoose'); + +const ProductSchema = new mongoose.Schema({ + name: { + type: String, + required: [true, "Product must have a name."], + maxLength: [100, "Product name can't have more then 100 characters."] + }, + description: { + type: String, + required: false + }, + imagePath: { + type: String, + required: false, + maxLength: [256, "Product's image path can't have more then 256 characters."] + }, + price: { + type: Number, + required: [true, "Product must have a price."] + } +}); + +const ProductModel = new mongoose.model('Product', ProductSchema); + +module.exports = ProductModel; diff --git a/server/models/User.js b/server/models/User.js index 294c016..e508f4c 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -34,6 +34,10 @@ const UserSchema = new mongoose.Schema({ type: String, required: [true, "You need to provide a password."], }, + admin: { + type: Boolean, + default: false + } }); const UserModel = new mongoose.model('User', UserSchema); diff --git a/server/routes/api.js b/server/routes/api.js index a0890a7..3e9c068 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -2,12 +2,24 @@ const express = require('express'); const router = express.Router(); const passport = require('passport'); -const exampleController = require('../controllers/example'); const userController = require('../controllers/user'); +const productsController = require('../controllers/products'); + +const isAuth = passport.authenticate('jwt', {session: false}); +const isAdmin = (req, res, next) => { + if (!req.user.admin) + res.status(401).json({status: "You need to be an administrator!"}); + else next(); +} -router.get('/', exampleController.index); -router.get('/protected', passport.authenticate('jwt', {session: false}), exampleController.index); router.post('/register', userController.register); router.post('/login', userController.login); +router.get('/products', productsController.index); +router.get('/products/:id', productsController.show); + +router.post('/products', isAuth, isAdmin, productsController.store); +router.patch('/products/:id', isAuth, isAdmin, productsController.update); +router.delete('/products/:id', isAuth, isAdmin, productsController.destroy); + module.exports = router; |