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
2 changes: 1 addition & 1 deletion controllers/articleController.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
36 changes: 36 additions & 0 deletions controllers/certiController.js
Original file line number Diff line number Diff line change
@@ -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) => {


}
125 changes: 125 additions & 0 deletions controllers/projectController.js
Original file line number Diff line number Diff line change
@@ -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
})
}
})
}
16 changes: 5 additions & 11 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
});
Expand Down Expand Up @@ -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', ['*'])
Expand Down
10 changes: 0 additions & 10 deletions models/articleModel.js

This file was deleted.

126 changes: 126 additions & 0 deletions models/modal_schema.js
Original file line number Diff line number Diff line change
@@ -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);
26 changes: 0 additions & 26 deletions models/userModel.js

This file was deleted.

Loading