diff --git a/controllers/articleController.js b/controllers/articleController.js index ca3b0f5..cdd2d44 100644 --- a/controllers/articleController.js +++ b/controllers/articleController.js @@ -1,5 +1,5 @@ require('dotenv').config(); -const Article = require('../models/articleModel'); +const Article = require('../models/modal_schema'); const jwt = require('jsonwebtoken'); const { roles } = require('../roles'); diff --git a/controllers/certiController.js b/controllers/certiController.js new file mode 100644 index 0000000..d90a6fb --- /dev/null +++ b/controllers/certiController.js @@ -0,0 +1,36 @@ +require('dotenv').config(); +const Certificate = require('../models/modal_schema'); +const jwt = require('jsonwebtoken'); +const { roles } = require('../roles'); + + +exports.getCertificates = (req, res, next) => { + Certificate.find({}, (err, foundCerties) => { + if (err) { + console.log(err); + } else { + res.json({ + data: foundCerties + }) + } + }); + +} + +exports.getCertificate = (req, res, next) => { + Certificate.findOne({ certiId: req.params.certiId }, (err, foundCerti) => { + if (err) { + console.log(err); + } else { + res.json({ + data: foundCerti + }) + } + }) + +} + +exports.postCertificate = (req, res, next) => { + + +} \ No newline at end of file diff --git a/controllers/projectController.js b/controllers/projectController.js new file mode 100644 index 0000000..a8c65ef --- /dev/null +++ b/controllers/projectController.js @@ -0,0 +1,125 @@ +require('dotenv').config(); +const Project = require('../models/modal_schema'); +const SubmitProject = require('../models/modal_schema'); +const jwt = require('jsonwebtoken'); +const { roles } = require('../roles'); + +exports.getProjects = (req, res, next) => { + Project.find({}, (err, foundProjects) => { + if (err) { + console.log(err); + } else { + res.status(200).json({ + data: foundProjects + }) + } + }) + +} + +exports.getProject = (req, res, next) => { + + Project.findOne({ projectId: req.params.projectId }, (err, foundProject) => { + if (err) { + console.log(err); + } else { + res.status(200).json({ + data: foundProject + }) + } + }) + +} + +exports.postProject = (req, res, next) => { + const { title, description, link, imageUrl, status } = req.body; + const newProject = new Project({ title, description, link, imageUrl, status }); + newProject.save((err, project) => { + if (err) { + console.log(err); + } else { + res.status(200).json({ + data: project, + message: "new project is saved" + }); + } + }) + +} + +exports.updateProject = (req, res, next) => { + + const update = req.body; + + Project.findOne({ projectId: req.params.projectId }, update, (err, updatedProject) => { + if (err) { + console.log(err); + } else { + res.json({ + data: updatedProject, + message: "project is updated successfully" + }) + } + }) + +} + +exports.deleteProject = (req, res, next) => { + + + Project.delete({ projectId: req.params.projectId }, (err) => { + if (err) { + console.log(err); + } else { + res.json({ + data: null, + message: "project id deleted successfully" + }) + } + }); + +} + +exports.get_submitProjects = (req, res, next) => { + + SubmitProject.find({}, (err, foundProjects) => { + if (err) { + console.log(err); + } else { + res.status(200).json({ + data: foundProjects + }) + } + }) +} + + +exports.post_submitProject = (req, res, next) => { + + const { title, description, email, phoneNumber } = req.body; + const new_submitProject = new SubmitProject({ title, description, email, phoneNumber }); + new_submitProject.save((err, savedProject) => { + if (err) { + console.log(err); + } else { + res.json({ + data: savedProject, + message: "your peoject is submitted successfully" + }); + } + }); + +} + +exports.get_submitProject = (req, res, next) => { + + SubmitProject.findOne({ title: req.params.title }, (err, foundProject) => { + if (err) { + console.log(err); + } else { + res.status(200).json({ + data: foundProject + }) + } + }) +} \ No newline at end of file diff --git a/controllers/userController.js b/controllers/userController.js index 1e77d8c..e6bc51a 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,16 +1,10 @@ require('dotenv').config(); -const User = require('../models/userModel'); +const User = require('../models/modal_schema'); const jwt = require('jsonwebtoken'); const bcrypt = require('bcrypt'); const { roles } = require('../roles') -// async function hashPassword(password) { -// return await bcrypt.hash(password, 10, (err, hashedPassword) => { -// if (!err) { -// return hashedPassword; -// } -// }); -// } + async function hashPassword(password) { const hashedPassword = await new Promise((resolve, reject) => { @@ -39,7 +33,7 @@ exports.signup = async(req, res, next) => { const { email, password, role } = req.body const hashedPassword = await hashPassword(password); - const newUser = new User({ email, password: hashedPassword, role: role || "public" }); + const newUser = new User({ email, password: hashedPassword, role: role || "teamMember" }); const accessToken = jwt.sign({ userId: newUser._id }, process.env.JWT_SECRET, { expiresIn: "1800s" @@ -71,7 +65,7 @@ exports.login = async(req, res, next) => { const validPassword = validatePassword(password, user.password); // console.log(validPassword); if (validPassword === false) return next(new Error('Password is not correct')) - if (user.role === 'public') return next(new Error('You cant login you are not team Member or admin')); + const accessToken = jwt.sign({ userId: user._id }, process.env.JWT_SECRET, { expiresIn: "1800s" }); @@ -151,7 +145,7 @@ exports.grantAccess = function(action, resource) { } } } - // to understand the above functionality + // to understand the above functionality (role based functionality) //const ac = new AccessControl(); // ac.grant('user') // define new or modify existing role. also takes an array. // .createOwn('video') // equivalent to .createOwn('video', ['*']) diff --git a/models/articleModel.js b/models/articleModel.js deleted file mode 100644 index eb84cfd..0000000 --- a/models/articleModel.js +++ /dev/null @@ -1,10 +0,0 @@ -const mongoose = require('mongoose'); - -const articleSchema = new mongoose.Schema({ - title: { type: String, required: true }, - content: { type: String, required: true } -}); - -const Article = new mongoose.model('Article', articleSchema); - -module.exports = Article; \ No newline at end of file diff --git a/models/modal_schema.js b/models/modal_schema.js new file mode 100644 index 0000000..488c762 --- /dev/null +++ b/models/modal_schema.js @@ -0,0 +1,126 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; +const AutoIncrement = require('mongoose-sequence')(mongoose); + +const UserSchema = new Schema({ + email: { + type: String, + required: true, + trim: true + }, + password: { + type: String, + required: true + }, + role: { + type: String, + default: 'public', + enum: ["public", "teamMember", "admin"] + }, + accessToken: { + type: String + }, + imageUrl: { + type: String + } +}); + +exports.User = mongoose.model('user', UserSchema); + + +const articleSchema = new Schema({ + title: { + type: String, + required: true + }, + description: { + type: String, + required: true + }, + userid: { + type: Schema.Types.ObjectId, + ref: "User", + required: true + }, + flag: { + type: Boolean, + default: false + } +}); + +exports.Article = new mongoose.model('Article', articleSchema); + +// when you will save certies then you dont need to provide certiNumber because it will be add automatically + +const certificateSchema = new Schema({ + userid: { + type: Schema.Types.ObjectId, + ref: "User", + required: true + }, + imageUrl: { + type: String, + required: true + }, + certiNumber: { + type: Number + } + +}); + +// this is mongoose-sequence plugin we have to add to automatically increasing the certiNumber field +certificateSchema.plugin(AutoIncrement, { id: 'certiNumber_seq', inc_field: 'certiNumber' }); + +exports.Certificate = new Mongoose.model('Certificate', certificateSchema); + +const projectSchema = new Schema({ + title: { + type: String, + required: true, + trim: true + }, + description: { + type: String, + required: true + }, + link: { + type: String, + required: true + }, + imageUrl: { + type: String, + required: true + }, + status: { + type: String, + default: "ongoing", + enum: ["ongoing", "completed", "coming"] + } + +}); + +exports.Project = new mongoose.model('Project', projectSchema); + +const submitProjectSchema = new Schema({ + title: { + type: String, + required: true, + trim: true + }, + description: { + type: String, + required: true + }, + email: { + type: String, + required: true + }, + phoneNumber: { + type: String, + required: true, + maxlength: 10 + } + +}) + +exports.SubmitProject = new mongoose.model('SubmitProject', submitProjectSchema); \ No newline at end of file diff --git a/models/userModel.js b/models/userModel.js deleted file mode 100644 index 32fedfb..0000000 --- a/models/userModel.js +++ /dev/null @@ -1,26 +0,0 @@ -const mongoose = require('mongoose'); -const Schema = mongoose.Schema; - -const UserSchema = new Schema({ - email: { - type: String, - required: true, - trim: true - }, - password: { - type: String, - required: true - }, - role: { - type: String, - default: 'public', - enum: ["public", "teamMember", "admin"] - }, - accessToken: { - type: String - } -}); - -const User = mongoose.model('user', UserSchema); - -module.exports = User; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index fc262ed..652a952 100644 --- a/package-lock.json +++ b/package-lock.json @@ -106,6 +106,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "async": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.3.tgz", + "integrity": "sha512-zflvls11DCy+dQWzTW2dzuilv8Z5X/pjfmZOWba6TNIVDm+2UDaJmXSOXlasHKfNBs8oo3M0aT50fDEWfKZjXg==", + "requires": { + "lodash": "^4.17.14" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -927,6 +935,11 @@ "package-json": "^6.3.0" } }, + "lodash": { + "version": "4.17.19", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" + }, "lodash.includes": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", @@ -1112,6 +1125,15 @@ "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" }, + "mongoose-sequence": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/mongoose-sequence/-/mongoose-sequence-5.2.2.tgz", + "integrity": "sha512-gtN33C4fXVgOH8SSQvwSf8+DcFtxw1n/Wk1RHEs+W3A/cqYgLjvjMalq/0q/TDboeapNi6RBymBnyw3fDoaDlg==", + "requires": { + "async": "^2.5.0", + "lodash": "^4.17.11" + } + }, "mpath": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.7.0.tgz", diff --git a/package.json b/package.json index 2dc7595..f6f2e81 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "express": "^4.17.1", "jsonwebtoken": "^8.5.1", "mongoose": "^5.9.25", + "mongoose-sequence": "^5.2.2", "nodemon": "^2.0.4" } } diff --git a/roles.js b/roles.js index eee46ff..149821a 100644 --- a/roles.js +++ b/roles.js @@ -5,6 +5,7 @@ const ac = new AccessControl(); exports.roles = (function() { ac.grant("public") .readAny("article") + .createAny('submit_project') ac.grant("teamMember") .readOwn("profile") @@ -20,6 +21,14 @@ exports.roles = (function() { .createAny('profile') .updateAny("profile") .deleteAny('profile') + .createAny('certificate') + .readAny('certificate') + .readAny('submit_project') + .createAny('project') + .updateAny('project') + .readAny('project') + .deleteAny('project') + return ac; })(); \ No newline at end of file diff --git a/routes/certiRoutes.js b/routes/certiRoutes.js new file mode 100644 index 0000000..a4dc120 --- /dev/null +++ b/routes/certiRoutes.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); +const userController = require('../controllers/userController'); +const certiConrtoller = require('../controllers/certiController'); + +router.get('/certis', userController.allowIfLoggedin, userController.grantAccess('readAny', 'certi'), certiConrtoller.getCertificates); + +router.get('/certi/:certiId', userController.allowIfLoggedin, userController.grantAccess('readAny', 'certi'), certiConrtoller.getCertificate); + +router.post('/certi', userController.allowIfLoggedin, userController.grantAccess('readAny', 'certi'), certiConrtoller.postCertificate); + + +module.exports = router; \ No newline at end of file diff --git a/routes/projectRoutes.js b/routes/projectRoutes.js new file mode 100644 index 0000000..676724d --- /dev/null +++ b/routes/projectRoutes.js @@ -0,0 +1,23 @@ +const express = require('express'); +const router = express.Router(); +const userController = require('../controllers/userController'); +const projectConrtoller = require('../controllers/projectController'); + +router.get('/projects', userController.allowIfLoggedin, userController.grantAccess('readAny', 'project'), projectConrtoller.getProjects); + +router.get('/project/:projectId', userController.allowIfLoggedin, userController.grantAccess('readAny', 'project'), projectConrtoller.getProject); + +router.post('/project', userController.allowIfLoggedin, userController.grantAccess('createAny', 'project'), projectConrtoller.postProject); + +router.put('/project/:projectId', userController.allowIfLoggedin, userController.grantAccess('updateAny'), 'project', projectConrtoller.updateProject); + +router.delete('/project/:projectId', userController.allowIfLoggedin, userController.grantAccess('deleteAny', 'project'), projectConrtoller.deleteProject); + +router.get('/submit_projects', userController.allowIfLoggedin, userController.grantAccess('readAny', 'submit_project'), projectConrtoller.get_submitProjects); + +router.get('/subit_project/:projectId', userController.allowIfLoggedin, userController.grantAccess('readAny', 'submit_project'), projectConrtoller.get_submitProject); + +router.post('/submit_project', userController.allowIfLoggedin, userController.grantAccess('createAny', 'submit_project'), projectConrtoller.post_submitProject); + + +module.exports = router; \ No newline at end of file diff --git a/routes/route.js b/routes/route.js index daddf38..31728e7 100644 --- a/routes/route.js +++ b/routes/route.js @@ -27,4 +27,6 @@ router.delete('/article/:articleId', userController.allowIfLoggedin, userControl router.post('/article', userController.allowIfLoggedin, userController.grantAccess('createAny', 'article'), articleController.createArticle); + + module.exports = router; \ No newline at end of file