Guest User

Untitled

a guest
Jul 28th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.86 KB | None | 0 0
  1. /*
  2. * Serve JSON to our AngularJS client
  3. */
  4. var Sequelize = require('sequelize');
  5.  
  6. //Setting up the config
  7. var sequelize = new Sequelize('workshop', 'root', 'root', {
  8. host: 'localhost',
  9. port: 3306,
  10. dialect: 'mysql'
  11. });
  12. //Checking connection status
  13. sequelize.authenticate().then(function(err) {
  14. if (err) {
  15. console.log('There is connection in ERROR.');
  16. } else {
  17. console.log('Connection has been established successfully');
  18. }
  19. });
  20.  
  21. // test.authenticate().then(function (err) {
  22. // if (err) {
  23. // console.log('There is connection in ERROR');
  24. // } else {
  25. // console.log('Connection has been established successfully');
  26. // }
  27. // });
  28.  
  29. var bcrypt = require('bcrypt-nodejs'),
  30. // request = require('request'),
  31. async = require('async'),
  32. nodemailer = require('nodemailer'),
  33. crypto = require('crypto'),
  34. fs = require('fs'),
  35. // timestamps = require('mongoose-timestamp'),
  36. moment = require('moment');
  37. // // passportLocalMongoose = require('passport-local-mongoose'),
  38. // mongoose.connect('mongodb://localhost/supportm', function(err, db) {
  39. // if (err) {
  40. // console.log('Unable to connect to the server. Please start the server. Error:', err);
  41. // } else {
  42. // console.log('Connected to Server successfully!');
  43. // }
  44. // });
  45. // mongoose.Promise = global.Promise;
  46.  
  47.  
  48. function encrypt(text) {
  49. return bcrypt.hashSync(text);
  50. }
  51.  
  52. function compare(text, hash) {
  53. return bcrypt.compareSync(text, hash);
  54. }
  55.  
  56. var Admin = sequelize.define('admin', {
  57. username: { type: Sequelize.STRING, unique: true, required: true },
  58. fName: Sequelize.STRING,
  59. lName: Sequelize.STRING,
  60. password: Sequelize.STRING,
  61. email: {
  62. type: Sequelize.STRING,
  63. // unique: true,
  64. required: true,
  65. validate: { isEmail: true }
  66. },
  67. mobile: Sequelize.STRING,
  68. role: Sequelize.STRING,
  69.  
  70. address: Sequelize.TEXT,
  71.  
  72. isActive: { type: Sequelize.BOOLEAN, defaultValue: true },
  73. resetPasswordToken: Sequelize.STRING,
  74. resetPasswordExpires: Sequelize.DATE,
  75. lastLoggedIn: Sequelize.DATE
  76. });
  77.  
  78.  
  79. // Admin.sync({ force: true }).then(function(err) {
  80. // if (err) {
  81. // console.log('An error occur while creating table');
  82. // } else {
  83. // console.log('Item table created successfully');
  84. // }
  85. // });
  86.  
  87.  
  88.  
  89. var Store = sequelize.define('Store', {
  90. iname: Sequelize.STRING,
  91. sprice: Sequelize.STRING,
  92. sbrand: Sequelize.STRING,
  93. // createdAt: Date,
  94. // updatedAt: Date,
  95. });
  96.  
  97. // Store.sync({ force: true }).then(function(err) {
  98. // if (err) {
  99. // console.log('An error occur while creating table');
  100. // } else {
  101. // console.log('Item table created successfully');
  102. // }
  103. // });
  104.  
  105. // Create Item Table Structure
  106. var Man = sequelize.define('Man', {
  107.  
  108. mname: Sequelize.STRING,
  109. mmbno: Sequelize.STRING,
  110. memail: Sequelize.STRING,
  111. maddr: Sequelize.STRING,
  112.  
  113. });
  114.  
  115. Man.belongsTo(Store, { onDelete: 'CASCADE' });
  116.  
  117. // Man.sync({ force: true }).then(function(err) {
  118. // if (err) {
  119. // console.log('An error occur while creating table');
  120. // } else {
  121. // console.log('Item table created successfully');
  122. // }
  123. // });
  124.  
  125.  
  126. var main = sequelize.define('main', {
  127.  
  128. cno: { type: Sequelize.INTEGER, unique: true },
  129. dt: Sequelize.DATE,
  130. stype: Sequelize.STRING,
  131. cname: Sequelize.STRING,
  132. product: Sequelize.STRING,
  133. brand: Sequelize.STRING,
  134. model: Sequelize.STRING,
  135. status: Sequelize.STRING,
  136. rid: Sequelize.INTEGER,
  137. cmbno: Sequelize.STRING,
  138. cemail: Sequelize.STRING,
  139. desc: Sequelize.STRING,
  140. req: Sequelize.STRING,
  141.  
  142. });
  143. main.belongsTo(Man, { onDelete: 'CASCADE'});
  144. main.belongsTo(Store, { onDelete: 'CASCADE' });
  145.  
  146. // main.sync({ force: true }).then(function(err) {
  147. // if (err) {
  148. // console.log('An error occur while creating table');
  149. // } else {
  150. // console.log('Item table created successfully');
  151. // }
  152. // });
  153.  
  154.  
  155. Store.hasMany(Man, { onDelete: 'CASCADE' });
  156. Man.hasMany(main, { onDelete: 'CASCADE' });
  157. Store.hasMany(main, { onDelete: 'CASCADE' });
  158.  
  159.  
  160. // exports.change = function(req, res) {
  161. // // console.log('change', {_id:req.body._id});
  162. // Admin.findById(req.user._id).exec(function(err, admin) {
  163. // console.log('id', req.body._id);
  164. // if (admin && compare(req.body.password, admin.password)) {
  165. // // console.log('pass', req.body.password);
  166. // Admin.update({ _id: req.user._id }, {
  167. // $set: {
  168. // password: encrypt(req.body.newpassword),
  169. // // repassword:req.body.repassword,
  170. // }
  171. // }, { new: true }).exec(function(err, admin) {
  172. // if (err) console.log('admin err:', err);
  173. // else res.status(200).send(admin);
  174. // });
  175. // } else res.status(500).send('not match');
  176. // });
  177. // }
  178. // exports.getadmin = function(req, res) {
  179. // console.log(' getadmin');
  180. // Admin.find().exec(function(err, data) {
  181. // if (err) console.log('getadmin find err:', err);
  182. // else res.status(200).send(data);
  183. // });
  184. // };
  185.  
  186.  
  187. exports.adminLogin = function(req, cb) {
  188. console.log('adminLogin', req);
  189.  
  190. Admin.find({
  191. where: { username: req.username, isActive: true },
  192. attributes: ['id', 'username', 'fName', 'lName',
  193. 'role', 'password'
  194. ],
  195. raw: true
  196. }).then(function(admin, err) {
  197. if (admin && compare(req.password, admin.password)) {
  198.  
  199. admin.password = undefined;
  200. cb(admin);
  201. } else if (err) {
  202. console.log('adminLogin findOne err:', err);
  203. cb(false);
  204. } else if (!!admin && !compare(req.password, admin.password)) {
  205. cb('Wrong password');
  206. } else {
  207. cb('No account found');
  208. }
  209. });
  210. }
  211.  
  212. // exports.adminLogin = function(req, cb) {
  213. // console.log('adminLogin', req);
  214. // // new Admin({
  215. // // name: 'pandi',
  216. // // password: encrypt('pandi'),
  217. // // email: 'braveganesh127@gmail.com',
  218. // // mobile: 9095206042,
  219. // // role: 'admin'
  220. // // }).save(function(err, admin) {
  221. // // if (err) console.log('new admin err:', err);
  222. // // else cb(admin);
  223. // // });
  224.  
  225. // Admin.findOne({
  226. // name: new RegExp(req.username, 'i')
  227. // }).lean().exec(function(err, admin) {
  228. // if (admin && compare(req.password, admin.password)) {
  229.  
  230. // admin.password = undefined;
  231. // cb(admin);
  232. // } else if (err) {
  233. // console.log('adminLogin findOne err:', err);
  234. // cb(false);
  235. // } else if (!!admin && !compare(req.password, admin.password)) {
  236. // cb('Wrong password');
  237. // } else {
  238. // cb('No account found');
  239. // }
  240. // });
  241. // }
  242.  
  243.  
  244.  
  245.  
  246.  
  247. // exports.singnup = function(req, res,cb) {
  248. // console.log('addman', req.body);
  249. // Admin.create({
  250. // // username: req.body.username,
  251. // // fName: req.body.username,
  252. // // lName: req.body.username,
  253. // // password: encrypt(req.body.password),
  254. // // email: req.body.email,
  255. // // mobile: req.body.mobile,
  256. // // role: req.body.role
  257. // }).then(function(admin, err) {
  258. // if (admin) {
  259. // admin.password = undefined;
  260. // cb(admin);
  261. // } else {
  262. // console.log('admin create err.', err);
  263. // }
  264. // })
  265. // }
  266.  
  267.  
  268. exports.addman = function(req, res) {
  269. console.log('addman', req.body);
  270. addman = Man.create({
  271. mname: req.body.mname,
  272. mmbno: req.body.mmbno,
  273. memail: req.body.memail,
  274. maddr: req.body.maddr,
  275.  
  276. }).then(function(data) {
  277. return res.status(200).send(' Successfully created!');
  278. }).catch(function(err) {
  279. return res.status(400).send(err.message);
  280. });
  281. };
  282.  
  283. exports.addstore = function(req, res) {
  284. console.log('addstore', req.body);
  285. var addstore = Store.create({
  286. iname: req.body.iname,
  287. sprice: req.body.sprice,
  288. sbrand: req.body.sbrand,
  289. // createdAt:new Date(),
  290. }).then(function(data) {
  291. return res.status(200).send(' Successfully created!');
  292. }).catch(function(err) {
  293. return res.status(400).send(err.message);
  294. });
  295. };
  296.  
  297. //Inserting Data into database
  298.  
  299. exports.addnew = function(req, res) {
  300. console.log('addnew', req.body);
  301. var addnew = main.create({
  302. cno: req.body.cno,
  303. dt: req.body.dt,
  304. stype: req.body.stype,
  305. cname: req.body.cname,
  306. product: req.body.product,
  307. brand: req.body.braveganesh127d,
  308. model: req.body.model,
  309. status: 'Unassigned',
  310. desc: req.body.desc,
  311.  
  312. }).then(function(data) {
  313. return res.status(200).send(' Successfully created!');
  314. }).catch(function(err) {
  315. return res.status(400).send(err.message);
  316. });
  317.  
  318. };
  319.  
  320.  
  321.  
  322. // exports.remove = function(req, res) {
  323. // console.log('remove', req.body);
  324. // main.create({ where: { cno: req.body.cno } }).then(main => {
  325. // // now you see me...
  326. // return main.destroy();
  327. // }).then(() => {
  328. // console.log(arguments)
  329. // })
  330.  
  331. // }
  332. // exports.remove = function(req, res) {
  333. // console.log('remove', req.body);
  334. // main.destroy({ where: { id: req.body.id } })
  335. // .then(function() {
  336. // console.log(arguments) // { '0': 0 }
  337. // })
  338.  
  339. // }
  340.  
  341. exports.remove = function(req, res) {
  342. main.destroy({ where: { id: req.params.id } }).then(function(data) {
  343. return res.status(200).send('Deleted Successfully!');
  344. }).catch(function(err) {
  345. return res.status(400).send(err.message);
  346. });
  347. };
  348.  
  349.  
  350. exports.getstore = function(req, res) {
  351. console.log('getstore');
  352. Store.findAll().then(function(data) {
  353. res.status(200).json(data)
  354. })
  355. .catch(function(error) {
  356. res.status(500).json(error)
  357. });
  358. };
  359.  
  360. exports.getmans = function(req, res) {
  361. console.log('getmans');
  362. Man.findAll().then(function(data) {
  363. res.status(200).json(data)
  364. })
  365. .catch(function(error) {
  366. res.status(500).json(error)
  367. });
  368.  
  369. };
  370.  
  371.  
  372. exports.getcomplaints = function(req, res) {
  373. console.log('at the complaint is:', req.params.status);
  374. main.findAll({
  375. where: {
  376.  
  377. status: req.params.status
  378. }
  379. }).then(function(data) {
  380. res.status(200).json(data)
  381. })
  382. .catch(function(error) {
  383. res.status(500).json(error)
  384. });
  385. };
  386. exports.updatemains = function(req, res) {
  387. main.update(req.body, { where: { id: req.body.id } })
  388. .then(function(data) {
  389.  
  390. console.log('include', req.body)
  391. main.findAll({
  392. where: { id: req.params.id },
  393.  
  394. include: [{
  395. model: Man,
  396.  
  397. }],
  398.  
  399. include: [{
  400. model: Store,
  401.  
  402. }]
  403.  
  404. })
  405.  
  406. })
  407. .then(function(data) {
  408. return res.status(200).send(data);
  409. }).catch(Sequelize.ValidationError, function(err) {
  410. return res.status(422).send(err.errors[0].message);
  411. }).catch(function(err) {
  412. return res.status(400).send(err.message);
  413. });
  414. };
  415.  
  416.  
  417.  
  418. // exports.updatemains = function(req, res) {
  419. // console.log('updatemains', req.body);
  420. // main.update({
  421. // status: req.body.status,
  422. // ManId: req.body.ManId,
  423. // rid: req.body.rid,
  424. // cmbno: req.body.cmbno,
  425. // cemail: req.body.cemail,
  426. // StoreId: req.body.StoreId,
  427. // dt: req.body.dt,
  428. // req: req.body.req,
  429. // }, { where: { cno: req.body.cno } })
  430. // .then(function(data) {
  431. // console.log('include', req.body)
  432. // main.findAll({
  433. // where: { id: req.params.id },
  434. // include: [{
  435. // model: Tool,
  436.  
  437. // include: [{
  438. // model: Teacher,
  439.  
  440. // }]
  441. // }]
  442. // })
  443.  
  444. // })
  445. // .then(function(data) {
  446. // return res.status(200).send(data);
  447. // }).catch(function(err) {
  448. // return res.status(400).send(err.message);
  449. // });
  450. // };
  451.  
  452. // exports.updatemains = function(req, res) {
  453. // console.log('updatemains', req.body);
  454. // mains.findByIdAndUpdate(req.body._id, {
  455. // $set: {
  456. // status: req.body.status,
  457. // man: req.body.man,
  458. // rid: req.body.rid,
  459. // cmbno: req.body.cmbno,
  460. // cemail: req.body.cemail,
  461. // spare: req.body.spare,
  462. // dt: req.body.dt,
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479. // req: req.body.req,
  480. // updatedAt: new Date(),
  481. // }
  482. // }, { new: true }).exec(function(err, mains) {
  483. // if (err) console.log('mains err:', err);
  484. // else res.status(200).send(mains);
  485. // });
  486. // }
  487. exports.updatemans = function(req, res) {
  488. Man.update(req.body, { where: { id: req.body.id } }).then(function(data) {
  489. return res.status(200).send(data);
  490. }).catch(Sequelize.ValidationError, function(err) {
  491. return res.status(422).send(err.errors[0].message);
  492. }).catch(function(err) {
  493. return res.status(400).send(err.message);
  494. });
  495. };
  496.  
  497.  
  498.  
  499. // exports.updatemans = function(req, res) {
  500. // console.log('updatemans', req.body);
  501. // mans.findByIdAndUpdate(req.body._id, {
  502. // $set: {
  503. // mmbno: req.body.mmbno,
  504. // memail: req.body.memail,
  505. // maddr: req.body.maddr,
  506. // updatedAt:new Date(),
  507. // }
  508. // }, { new: true }).exec(function(err, mans) {
  509. // if (err) console.log('mans err:', err);
  510. // else res.status(200).send(mans);
  511. // });
  512. // }
  513.  
  514. exports.searchmembers = function(req, res) {
  515. console.log('searchmembers:', req.params.cno)
  516. main.findAll({
  517. where: {
  518.  
  519. cno: req.params.cno
  520. }
  521. }).then(function(data) {
  522. res.status(200).json(data)
  523. })
  524. .catch(function(error) {
  525. res.status(500).json(error)
  526. });
  527. // mains.find({ cno: req.params.cno }).populate('man ').exec(function(err, data) {
  528. // if (err) console.log('searchmembers find err:', err);
  529. // else res.status(200).send(data);
  530. // });
  531.  
  532. };
  533.  
  534. // exports.searchmembers = function(req, res) {
  535. // console.log('searchmembers:', req.params.cno)
  536. // mains.find({ cno: req.params.cno }).populate('man ').exec(function(err, data) {
  537. // if (err) console.log('searchmembers find err:', err);
  538. // else res.status(200).send(data);
  539. // });
  540. // };
  541.  
  542.  
  543. exports.getreq = function(req, res) {
  544. console.log('getreq:', { req: { $ne: null } })
  545. main.findAll({
  546. where: {
  547.  
  548. req: { $ne: null }
  549. }
  550. }).then(function(data) {
  551. res.status(200).json(data)
  552. })
  553. .catch(function(error) {
  554. res.status(500).json(error)
  555. });
  556. // mains.find({ req: { $ne: null } }).populate('man').exec(function(err, data) {
  557. // if (err) console.log('getreq find err:', err);
  558. // else res.status(200).send(data);
  559. // });
  560. };
  561.  
  562. // exports.getreq = function(req, res) {
  563. // console.log('getreq:', { req: { $ne: null } })
  564. // mains.find({ req: { $ne: null } }).populate('man').exec(function(err, data) {
  565. // if (err) console.log('getreq find err:', err);
  566. // else res.status(200).send(data);
  567. // });
  568. // };
  569.  
  570.  
  571. // //Converter Class
  572. // var Converter = require("csvtojson").Converter;
  573. // var converter = new Converter({
  574. // noheader: false
  575. // // headers:['*flat*S.No','Name','Author','*flat*no.1','*flat*no.2','*flat*no.3','*flat*no.4','City','Type','*flat*no.5']
  576. // });
  577.  
  578.  
  579. exports.name = function(req, res) {
  580. res.json({
  581. name: 'Ganesh Pandiyan'
  582. });
  583. };
  584.  
  585. // exports.getempProfiles = function(req, res) {
  586. // empProfile.find().exec(function(err, data) {
  587. // if (err) console.log('getempProfiles find err:', err);
  588. // else res.json(data);
  589. // });
  590. // };
  591.  
  592. // exports.getempLogs = function(req, res) {
  593. // empLog.find().exec(function(err, data) {
  594. // if (err) console.log('getempLogs find err:', err);
  595. // else res.json(data);
  596. // });
  597. // };
  598.  
  599. // exports.filterempProfiles = function(req, res) {
  600. // empProfile.find({
  601. // $or: [{
  602. // EnNo: new RegExp(req.params.filteremp, "i")
  603. // }, {
  604. // Name: new RegExp(req.params.filteremp, "i")
  605. // }]
  606. // }).exec(function(err, data) {
  607. // if (err) console.log('filterempProfiles err:', err);
  608. // else res.json(data);
  609. // });
  610. // };
  611.  
  612. // exports.filterempLogs = function(req, res) {
  613. // empLog.find({
  614. // $or: [{
  615. // EnNo: new RegExp(req.params.filteremp, "i")
  616. // }, {
  617. // Name: new RegExp(req.params.filteremp, "i")
  618. // }]
  619. // }).exec(function(err, data) {
  620. // if (err) console.log('filterempLogs err:', err);
  621. // else res.json(data);
  622. // });
  623. // };
Add Comment
Please, Sign In to add comment