Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Serve JSON to our AngularJS client
- */
- var Sequelize = require('sequelize');
- //Setting up the config
- var sequelize = new Sequelize('workshop', 'root', 'root', {
- host: 'localhost',
- port: 3306,
- dialect: 'mysql'
- });
- //Checking connection status
- sequelize.authenticate().then(function(err) {
- if (err) {
- console.log('There is connection in ERROR.');
- } else {
- console.log('Connection has been established successfully');
- }
- });
- // test.authenticate().then(function (err) {
- // if (err) {
- // console.log('There is connection in ERROR');
- // } else {
- // console.log('Connection has been established successfully');
- // }
- // });
- var bcrypt = require('bcrypt-nodejs'),
- // request = require('request'),
- async = require('async'),
- nodemailer = require('nodemailer'),
- crypto = require('crypto'),
- fs = require('fs'),
- // timestamps = require('mongoose-timestamp'),
- moment = require('moment');
- // // passportLocalMongoose = require('passport-local-mongoose'),
- // mongoose.connect('mongodb://localhost/supportm', function(err, db) {
- // if (err) {
- // console.log('Unable to connect to the server. Please start the server. Error:', err);
- // } else {
- // console.log('Connected to Server successfully!');
- // }
- // });
- // mongoose.Promise = global.Promise;
- function encrypt(text) {
- return bcrypt.hashSync(text);
- }
- function compare(text, hash) {
- return bcrypt.compareSync(text, hash);
- }
- var Admin = sequelize.define('admin', {
- username: { type: Sequelize.STRING, unique: true, required: true },
- fName: Sequelize.STRING,
- lName: Sequelize.STRING,
- password: Sequelize.STRING,
- email: {
- type: Sequelize.STRING,
- // unique: true,
- required: true,
- validate: { isEmail: true }
- },
- mobile: Sequelize.STRING,
- role: Sequelize.STRING,
- address: Sequelize.TEXT,
- isActive: { type: Sequelize.BOOLEAN, defaultValue: true },
- resetPasswordToken: Sequelize.STRING,
- resetPasswordExpires: Sequelize.DATE,
- lastLoggedIn: Sequelize.DATE
- });
- // Admin.sync({ force: true }).then(function(err) {
- // if (err) {
- // console.log('An error occur while creating table');
- // } else {
- // console.log('Item table created successfully');
- // }
- // });
- var Store = sequelize.define('Store', {
- iname: Sequelize.STRING,
- sprice: Sequelize.STRING,
- sbrand: Sequelize.STRING,
- // createdAt: Date,
- // updatedAt: Date,
- });
- // Store.sync({ force: true }).then(function(err) {
- // if (err) {
- // console.log('An error occur while creating table');
- // } else {
- // console.log('Item table created successfully');
- // }
- // });
- // Create Item Table Structure
- var Man = sequelize.define('Man', {
- mname: Sequelize.STRING,
- mmbno: Sequelize.STRING,
- memail: Sequelize.STRING,
- maddr: Sequelize.STRING,
- });
- Man.belongsTo(Store, { onDelete: 'CASCADE' });
- // Man.sync({ force: true }).then(function(err) {
- // if (err) {
- // console.log('An error occur while creating table');
- // } else {
- // console.log('Item table created successfully');
- // }
- // });
- var main = sequelize.define('main', {
- cno: { type: Sequelize.INTEGER, unique: true },
- dt: Sequelize.DATE,
- stype: Sequelize.STRING,
- cname: Sequelize.STRING,
- product: Sequelize.STRING,
- brand: Sequelize.STRING,
- model: Sequelize.STRING,
- status: Sequelize.STRING,
- rid: Sequelize.INTEGER,
- cmbno: Sequelize.STRING,
- cemail: Sequelize.STRING,
- desc: Sequelize.STRING,
- req: Sequelize.STRING,
- });
- main.belongsTo(Man, { onDelete: 'CASCADE'});
- main.belongsTo(Store, { onDelete: 'CASCADE' });
- // main.sync({ force: true }).then(function(err) {
- // if (err) {
- // console.log('An error occur while creating table');
- // } else {
- // console.log('Item table created successfully');
- // }
- // });
- Store.hasMany(Man, { onDelete: 'CASCADE' });
- Man.hasMany(main, { onDelete: 'CASCADE' });
- Store.hasMany(main, { onDelete: 'CASCADE' });
- // exports.change = function(req, res) {
- // // console.log('change', {_id:req.body._id});
- // Admin.findById(req.user._id).exec(function(err, admin) {
- // console.log('id', req.body._id);
- // if (admin && compare(req.body.password, admin.password)) {
- // // console.log('pass', req.body.password);
- // Admin.update({ _id: req.user._id }, {
- // $set: {
- // password: encrypt(req.body.newpassword),
- // // repassword:req.body.repassword,
- // }
- // }, { new: true }).exec(function(err, admin) {
- // if (err) console.log('admin err:', err);
- // else res.status(200).send(admin);
- // });
- // } else res.status(500).send('not match');
- // });
- // }
- // exports.getadmin = function(req, res) {
- // console.log(' getadmin');
- // Admin.find().exec(function(err, data) {
- // if (err) console.log('getadmin find err:', err);
- // else res.status(200).send(data);
- // });
- // };
- exports.adminLogin = function(req, cb) {
- console.log('adminLogin', req);
- Admin.find({
- where: { username: req.username, isActive: true },
- attributes: ['id', 'username', 'fName', 'lName',
- 'role', 'password'
- ],
- raw: true
- }).then(function(admin, err) {
- if (admin && compare(req.password, admin.password)) {
- admin.password = undefined;
- cb(admin);
- } else if (err) {
- console.log('adminLogin findOne err:', err);
- cb(false);
- } else if (!!admin && !compare(req.password, admin.password)) {
- cb('Wrong password');
- } else {
- cb('No account found');
- }
- });
- }
- // exports.adminLogin = function(req, cb) {
- // console.log('adminLogin', req);
- // // new Admin({
- // // name: 'pandi',
- // // password: encrypt('pandi'),
- // // email: 'braveganesh127@gmail.com',
- // // mobile: 9095206042,
- // // role: 'admin'
- // // }).save(function(err, admin) {
- // // if (err) console.log('new admin err:', err);
- // // else cb(admin);
- // // });
- // Admin.findOne({
- // name: new RegExp(req.username, 'i')
- // }).lean().exec(function(err, admin) {
- // if (admin && compare(req.password, admin.password)) {
- // admin.password = undefined;
- // cb(admin);
- // } else if (err) {
- // console.log('adminLogin findOne err:', err);
- // cb(false);
- // } else if (!!admin && !compare(req.password, admin.password)) {
- // cb('Wrong password');
- // } else {
- // cb('No account found');
- // }
- // });
- // }
- // exports.singnup = function(req, res,cb) {
- // console.log('addman', req.body);
- // Admin.create({
- // // username: req.body.username,
- // // fName: req.body.username,
- // // lName: req.body.username,
- // // password: encrypt(req.body.password),
- // // email: req.body.email,
- // // mobile: req.body.mobile,
- // // role: req.body.role
- // }).then(function(admin, err) {
- // if (admin) {
- // admin.password = undefined;
- // cb(admin);
- // } else {
- // console.log('admin create err.', err);
- // }
- // })
- // }
- exports.addman = function(req, res) {
- console.log('addman', req.body);
- addman = Man.create({
- mname: req.body.mname,
- mmbno: req.body.mmbno,
- memail: req.body.memail,
- maddr: req.body.maddr,
- }).then(function(data) {
- return res.status(200).send(' Successfully created!');
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- exports.addstore = function(req, res) {
- console.log('addstore', req.body);
- var addstore = Store.create({
- iname: req.body.iname,
- sprice: req.body.sprice,
- sbrand: req.body.sbrand,
- // createdAt:new Date(),
- }).then(function(data) {
- return res.status(200).send(' Successfully created!');
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- //Inserting Data into database
- exports.addnew = function(req, res) {
- console.log('addnew', req.body);
- var addnew = main.create({
- cno: req.body.cno,
- dt: req.body.dt,
- stype: req.body.stype,
- cname: req.body.cname,
- product: req.body.product,
- brand: req.body.braveganesh127d,
- model: req.body.model,
- status: 'Unassigned',
- desc: req.body.desc,
- }).then(function(data) {
- return res.status(200).send(' Successfully created!');
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- // exports.remove = function(req, res) {
- // console.log('remove', req.body);
- // main.create({ where: { cno: req.body.cno } }).then(main => {
- // // now you see me...
- // return main.destroy();
- // }).then(() => {
- // console.log(arguments)
- // })
- // }
- // exports.remove = function(req, res) {
- // console.log('remove', req.body);
- // main.destroy({ where: { id: req.body.id } })
- // .then(function() {
- // console.log(arguments) // { '0': 0 }
- // })
- // }
- exports.remove = function(req, res) {
- main.destroy({ where: { id: req.params.id } }).then(function(data) {
- return res.status(200).send('Deleted Successfully!');
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- exports.getstore = function(req, res) {
- console.log('getstore');
- Store.findAll().then(function(data) {
- res.status(200).json(data)
- })
- .catch(function(error) {
- res.status(500).json(error)
- });
- };
- exports.getmans = function(req, res) {
- console.log('getmans');
- Man.findAll().then(function(data) {
- res.status(200).json(data)
- })
- .catch(function(error) {
- res.status(500).json(error)
- });
- };
- exports.getcomplaints = function(req, res) {
- console.log('at the complaint is:', req.params.status);
- main.findAll({
- where: {
- status: req.params.status
- }
- }).then(function(data) {
- res.status(200).json(data)
- })
- .catch(function(error) {
- res.status(500).json(error)
- });
- };
- exports.updatemains = function(req, res) {
- main.update(req.body, { where: { id: req.body.id } })
- .then(function(data) {
- console.log('include', req.body)
- main.findAll({
- where: { id: req.params.id },
- include: [{
- model: Man,
- }],
- include: [{
- model: Store,
- }]
- })
- })
- .then(function(data) {
- return res.status(200).send(data);
- }).catch(Sequelize.ValidationError, function(err) {
- return res.status(422).send(err.errors[0].message);
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- // exports.updatemains = function(req, res) {
- // console.log('updatemains', req.body);
- // main.update({
- // status: req.body.status,
- // ManId: req.body.ManId,
- // rid: req.body.rid,
- // cmbno: req.body.cmbno,
- // cemail: req.body.cemail,
- // StoreId: req.body.StoreId,
- // dt: req.body.dt,
- // req: req.body.req,
- // }, { where: { cno: req.body.cno } })
- // .then(function(data) {
- // console.log('include', req.body)
- // main.findAll({
- // where: { id: req.params.id },
- // include: [{
- // model: Tool,
- // include: [{
- // model: Teacher,
- // }]
- // }]
- // })
- // })
- // .then(function(data) {
- // return res.status(200).send(data);
- // }).catch(function(err) {
- // return res.status(400).send(err.message);
- // });
- // };
- // exports.updatemains = function(req, res) {
- // console.log('updatemains', req.body);
- // mains.findByIdAndUpdate(req.body._id, {
- // $set: {
- // status: req.body.status,
- // man: req.body.man,
- // rid: req.body.rid,
- // cmbno: req.body.cmbno,
- // cemail: req.body.cemail,
- // spare: req.body.spare,
- // dt: req.body.dt,
- // req: req.body.req,
- // updatedAt: new Date(),
- // }
- // }, { new: true }).exec(function(err, mains) {
- // if (err) console.log('mains err:', err);
- // else res.status(200).send(mains);
- // });
- // }
- exports.updatemans = function(req, res) {
- Man.update(req.body, { where: { id: req.body.id } }).then(function(data) {
- return res.status(200).send(data);
- }).catch(Sequelize.ValidationError, function(err) {
- return res.status(422).send(err.errors[0].message);
- }).catch(function(err) {
- return res.status(400).send(err.message);
- });
- };
- // exports.updatemans = function(req, res) {
- // console.log('updatemans', req.body);
- // mans.findByIdAndUpdate(req.body._id, {
- // $set: {
- // mmbno: req.body.mmbno,
- // memail: req.body.memail,
- // maddr: req.body.maddr,
- // updatedAt:new Date(),
- // }
- // }, { new: true }).exec(function(err, mans) {
- // if (err) console.log('mans err:', err);
- // else res.status(200).send(mans);
- // });
- // }
- exports.searchmembers = function(req, res) {
- console.log('searchmembers:', req.params.cno)
- main.findAll({
- where: {
- cno: req.params.cno
- }
- }).then(function(data) {
- res.status(200).json(data)
- })
- .catch(function(error) {
- res.status(500).json(error)
- });
- // mains.find({ cno: req.params.cno }).populate('man ').exec(function(err, data) {
- // if (err) console.log('searchmembers find err:', err);
- // else res.status(200).send(data);
- // });
- };
- // exports.searchmembers = function(req, res) {
- // console.log('searchmembers:', req.params.cno)
- // mains.find({ cno: req.params.cno }).populate('man ').exec(function(err, data) {
- // if (err) console.log('searchmembers find err:', err);
- // else res.status(200).send(data);
- // });
- // };
- exports.getreq = function(req, res) {
- console.log('getreq:', { req: { $ne: null } })
- main.findAll({
- where: {
- req: { $ne: null }
- }
- }).then(function(data) {
- res.status(200).json(data)
- })
- .catch(function(error) {
- res.status(500).json(error)
- });
- // mains.find({ req: { $ne: null } }).populate('man').exec(function(err, data) {
- // if (err) console.log('getreq find err:', err);
- // else res.status(200).send(data);
- // });
- };
- // exports.getreq = function(req, res) {
- // console.log('getreq:', { req: { $ne: null } })
- // mains.find({ req: { $ne: null } }).populate('man').exec(function(err, data) {
- // if (err) console.log('getreq find err:', err);
- // else res.status(200).send(data);
- // });
- // };
- // //Converter Class
- // var Converter = require("csvtojson").Converter;
- // var converter = new Converter({
- // noheader: false
- // // headers:['*flat*S.No','Name','Author','*flat*no.1','*flat*no.2','*flat*no.3','*flat*no.4','City','Type','*flat*no.5']
- // });
- exports.name = function(req, res) {
- res.json({
- name: 'Ganesh Pandiyan'
- });
- };
- // exports.getempProfiles = function(req, res) {
- // empProfile.find().exec(function(err, data) {
- // if (err) console.log('getempProfiles find err:', err);
- // else res.json(data);
- // });
- // };
- // exports.getempLogs = function(req, res) {
- // empLog.find().exec(function(err, data) {
- // if (err) console.log('getempLogs find err:', err);
- // else res.json(data);
- // });
- // };
- // exports.filterempProfiles = function(req, res) {
- // empProfile.find({
- // $or: [{
- // EnNo: new RegExp(req.params.filteremp, "i")
- // }, {
- // Name: new RegExp(req.params.filteremp, "i")
- // }]
- // }).exec(function(err, data) {
- // if (err) console.log('filterempProfiles err:', err);
- // else res.json(data);
- // });
- // };
- // exports.filterempLogs = function(req, res) {
- // empLog.find({
- // $or: [{
- // EnNo: new RegExp(req.params.filteremp, "i")
- // }, {
- // Name: new RegExp(req.params.filteremp, "i")
- // }]
- // }).exec(function(err, data) {
- // if (err) console.log('filterempLogs err:', err);
- // else res.json(data);
- // });
- // };
Add Comment
Please, Sign In to add comment