Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. module.exports = sequelize;
  2.  
  3. var massive = require("massive");
  4. var express = require("express");
  5. var Sequelize = require('sequelize');
  6. var app = express();
  7. var jwt = require('jsonwebtoken');
  8. var bodyParser = require('body-parser');
  9. var cryptico = require('cryptico');
  10.  
  11.  
  12.  
  13. var apiRoutes = express.Router();
  14. app.use(bodyParser.urlencoded({ extended: false }));
  15. app.use(bodyParser.json());
  16.  
  17. var sequelize = new Sequelize('postgres://postgres:barry@localhost:5432/courtsystem');
  18. var SECRET = 'shhhhhhared-secret';
  19.  
  20.  
  21. app.get('/', function (req, res) {
  22. res.send('Welcome to court system!!! port 8095')
  23. })
  24.  
  25. app.listen(8095, function () {
  26. console.log('Example app listening on port 8095!')
  27. })
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. let Judge = sequelize.define('judge',{
  36. iD: {
  37. type: Sequelize.INTEGER,
  38. field: 'id',
  39. primaryKey: true
  40. },
  41. Name: {
  42. type: Sequelize.STRING,
  43. field: 'name'
  44. },
  45. Room: {
  46. type:Sequelize.INTEGER,
  47. field: 'room'
  48. },
  49. Ext: {
  50. type: Sequelize.STRING,
  51. field: 'ext'
  52. }
  53. }, {
  54. freezeTableName: true
  55. });
  56.  
  57. let Courtroom = sequelize.define('courtroom', {
  58. iD: {
  59. type: Sequelize.INTEGER,
  60. field: 'id',
  61. primaryKey: true
  62. },
  63. Number: {
  64. type:Sequelize.STRING,
  65. field: 'number'
  66. }
  67. }, {
  68. freezeTableName: true
  69. });
  70.  
  71. let Participant = sequelize.define('participant', {
  72. iD: {
  73. type: Sequelize.INTEGER,
  74. field: 'id',
  75. primaryKey: true
  76. },
  77. Name: {
  78. type: Sequelize.STRING,
  79. field: 'name'
  80. },
  81. Address: {
  82. type: Sequelize.STRING,
  83. field: 'address'
  84. },
  85. Type: {
  86. type: Sequelize.STRING,
  87. field: 'type'
  88. }
  89. }, {
  90. freezeTableName: true
  91. });
  92.  
  93. let Case = sequelize.define('case', {
  94. judge_iD: {
  95. type: Sequelize.INTEGER,
  96. field: 'judge_id'
  97. },
  98. courtroom_iD: {
  99. type: Sequelize.INTEGER,
  100. field: 'courtroom_id'
  101. },
  102. claimant_iD: {
  103. type: Sequelize.INTEGER,
  104. field: 'claimant_id'
  105. },
  106. respondent_iD: {
  107. type: Sequelize.INTEGER,
  108. field: 'respondent_id'
  109. },
  110. start_Date: {
  111. type: Sequelize.DATEONLY,
  112. field: 'start_date'
  113. },
  114. duration: {
  115. type: Sequelize.RANGE(Sequelize.DATE),
  116. field: 'duration'
  117. },
  118. result: {
  119. type: Sequelize.BOOLEAN,
  120. field: 'result'
  121. }
  122. }, {
  123. freezeTableName: true
  124. });
  125.  
  126.  
  127.  
  128.  
  129.  
  130. Judge.sync({force: true}).then(function () {
  131. return Judge.create({
  132. iD: 1,
  133. Name: 'Barry',
  134. Room: 12,
  135. Ext: '1'
  136. });
  137. });
  138.  
  139. Participant.sync({force: true}).then(function () {
  140. return Participant.create({
  141. iD: 5,
  142. Name: 'Barry Burke',
  143. Address: "Tallaght",
  144. Type: 'claimant'
  145. });
  146. });
  147.  
  148.  
  149. Courtroom.sync({force: true}).then(function () {
  150. return Courtroom.create({
  151. iD: 1,
  152. Number: 1
  153. });
  154. });
  155.  
  156. Case.sync({force: true}).then(function () {
  157. return Case.create({
  158. judge_iD: 1,
  159. courtroom_iD: 1,
  160. claimant_iD: 1,
  161. respondent_id: 2,
  162. //start_Date: 2012-04-25,
  163. //duratioN:10/10/17-11/10/17,
  164. resulT: 1
  165. });
  166. });
  167.  
  168.  
  169.  
  170. Case.belongsTo(Judge, { foreignKey: 'judgeiD', primaryKey: true});
  171. Case.belongsTo(Participant, { foreignKey: 'claimantID', primaryKey: true });
  172. Case.belongsTo(Participant, { foreignKey: 'respondent_iD', primaryKey: true});
  173. Case.belongsTo(Courtroom, {foreignKey: 'courtroom_id', primaryKey: true});
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180. //var token = jwt.sign(user, app.get('superSecret'), {
  181. //expiresInMinutes: 1440 // expires in 24 hours
  182. //});
  183.  
  184. app.get('/ValidateUser/:username/:password', function (req, res) {
  185. console.log(res)
  186. massive.connect({connectionString:'postgres://postgres:barry@localhost:5432/courtsystem'}, function(err, db)
  187. {
  188. db.run("Select * from userslab3 where username = $1 and password = crypt($2, password)", [req.params.username, req.params.password], function(err, result){
  189. console.log(result);
  190. console.log(err);
  191.  
  192. if(result[0].username == req.params.username)
  193. {
  194. var token = jwt.sign(result[0], SECRET, { expiresIn: 1440 });
  195. res.json({
  196. success: true,
  197. message: 'Enjoy your token!',
  198. token: token
  199. });
  200. }
  201. });
  202. });
  203. })
  204.  
  205. app.get('/InsertUser', function (req, res) {
  206.  
  207. massive.connect({connectionString: 'postgres://postgres:barry@localhost:5432/courtsystem'}, function(err, db)
  208. {
  209. db.run("Insert into userslab3 (username, password) values ('Barry', crypt('Password1', gen_salt('bf')));", [], function(err, result){
  210. console.log(err);
  211. res.end(JSON.stringify(err));
  212. });
  213. });
  214. })
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. var PassPhrase = "SuperSecret";
  222. var Bits = 160;
  223. var RSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
  224. var PublicKeyString = cryptico.publicKeyString(RSAkey);
  225.  
  226. massive.connect({connectionString: "postgres://postgres:goldfish1@localhost:5432/postgres"}, function(err, db)
  227. {
  228. db.run("Update users set accesskey = $1, secretkey = $2 where username = 'Barry';", [PublicKeyString, RSAkey], function(err, result){
  229. console.log(err);
  230. console.log(result);
  231. });
  232. });
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. apiRoutes.use(function(req, res, next) {
  243.  
  244. // check header or url parameters or post parameters for token
  245. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  246.  
  247. if (token) {
  248.  
  249. // verifies secret and checks exp
  250. jwt.verify(token, SECRET, function(err, decoded) {
  251. if (err) {
  252. return res.json({ success: false, message: 'Failed to authenticate token.' });
  253. } else {
  254. // if everything is good, save to request for use in other routes
  255. req.decoded = decoded;
  256. next();
  257. }
  258. });
  259. }
  260. else{
  261. // if there is no token
  262. // return an error
  263. return res.status(403).send({
  264. success: false,
  265. message: 'No token provided.'
  266. });
  267. }
  268. });
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. app.use('/api', apiRoutes);
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. apiRoutes.get('/Message/:message', (req,res) => {
  287. var EncryptionResult = cryptico.encrypt(req.params.message, PublicKeyString);
  288. res.send(EncryptionResult);
  289. })
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. apiRoutes.get('/Judge', (req,res) => {
  303. Judge.findAll({
  304. where: {
  305. iD: 1
  306. }
  307. }).then(function(Judge){return res.json(Judge);})
  308. })
  309.  
  310.  
  311. apiRoutes.get('/UpdateJudge', (req, res) => {
  312. Judge.update({
  313. iD: 3,
  314. }, {
  315. where: {
  316. iD: 1
  317. }
  318. }).then(function(Judge){res.send('ID Updated');})
  319. })
  320.  
  321. apiRoutes.get('/DeleteJudge', (req, res) => {
  322. Judge.destroy({
  323. where: {
  324. iD: 2
  325. }
  326. }).then(function(){res.send('user Deleted');})
  327. })
  328.  
  329. apiRoutes.get('/AddJudge', (req, res) => {
  330. Judge.create({
  331. iD: 2,
  332. Name: 'Barry',
  333. Room: 12,
  334. Ext: '1'
  335. }).then(function(){res.send('user Barry Added');})
  336. });
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346. //-------------
  347. apiRoutes.get('/Case', (req, res) => {
  348. Case.findAll({
  349. where: {
  350. judge_iD: 1
  351. }
  352. }).then(function(Case){res.send(Case);})
  353. })
  354.  
  355.  
  356. apiRoutes.get('/UpdateCase', (req, res) => {
  357. Case.update({
  358. judge_iD: 3,
  359. }, {
  360. where: {
  361. judge_iD: 1
  362. }
  363. }).then(function(Case){res.send('ID Updated');})
  364. })
  365.  
  366. apiRoutes.get('/DeleteCase', (req, res) => {
  367. Case.destroy({
  368. where: {
  369. judge_iD: 2
  370. }
  371. }).then(function(){res.send('user Deleted');})
  372. })
  373.  
  374. apiRoutes.get('/AddCase', (req, res) => {
  375. Case.create({
  376. judge_iD: 1,
  377. courtroom_iD: 1,
  378. claimant_iD: 1,
  379. respondent_id: 2,
  380. //start_Date: 2012-04-25,
  381. //duratioN:10/10/17-11/10/17,
  382. resulT: 1
  383. }).then(function(){res.send('Case Added');})
  384. });
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. //-------------
  395. apiRoutes.get('/Courtroom', (req, res) => {
  396. var bool = verify();
  397. if(bool)
  398. {
  399. Courtroom.findAll({
  400. where: {
  401. iD: 1
  402. }
  403. }).then(function(Case){res.send(Case);})
  404. }
  405. else{
  406. res.send(401);
  407. }
  408. })
  409.  
  410.  
  411. apiRoutes.get('/UpdatedCourtroom', (req, res) => {
  412. Courtroom.update({
  413. iD: 3,
  414. }, {
  415. where: {
  416. judge_iD: 1
  417. }
  418. }).then(function(Case){res.send('ID Updated');})
  419. })
  420.  
  421. apiRoutes.get('/DeleteCourtroom', (req, res) => {
  422. Courtroom.destroy({
  423. where: {
  424. iD: 2
  425. }
  426. }).then(function(){res.send(' Deleted');})
  427. })
  428.  
  429. apiRoutes.get('/AddCourtroom', (req, res) => {
  430. Courtroom.create({
  431. iD: 2,
  432. Number: 1
  433. }).then(function(){res.send('Courtroom Added');})
  434. });
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445. apiRoutes.get('/Participant', (req, res) => {
  446. Participant.findAll({
  447. where: {
  448. iD: 1
  449. }
  450. }).then(function(Participant){res.send(Participant);})
  451. })
  452.  
  453.  
  454. apiRoutes.get('/UpdateParticipant', (req, res) => {
  455. Participant.update({
  456. iD: 3,
  457. }, {
  458. where: {
  459. iD: 1
  460. }
  461. }).then(function(Participant){res.send('ID Updated');})
  462. })
  463.  
  464. apiRoutes.get('/DeleteParticipant', (req, res) => {
  465. Participant.destroy({
  466. where: {
  467. iD: 2
  468. }
  469. }).then(function(){res.send('user Deleted');})
  470. })
  471.  
  472. apiRoutes.get('/AddParticipant', (req, res) => {
  473. Participant.create({
  474. iD: 1,
  475. Name: 'Baz Burke',
  476. Address: "Tallaght",
  477. Type: 'claimant'
  478. }).then(function(){res.send(' Barry Added');})
  479. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement