Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. //LAB 2 PART 1
  2. const express = require('express');
  3. const massive = require('massive');
  4. var Sequelize = require('sequelize');
  5. var jwt = require('jsonwebtoken');
  6. var bp = require('body-parser');
  7. var crypt = require('cryptico');
  8.  
  9.  
  10.  
  11. let sequelize = new Sequelize('postgres://postgres:@localhost:5432/court');
  12.  
  13. const app = express();
  14. const db = massive.connectSync({
  15. db: 'court',
  16. });
  17.  
  18. let Judge = sequelize.define('judge',{
  19. iD: {
  20. type: Sequelize.INTEGER,
  21. field: 'id', primaryKey: true // attribute that is iD when judge facing but 'id' in the db
  22. },
  23. Name: {
  24. type: Sequelize.STRING,
  25. field: 'name'
  26. },
  27. Room: {
  28. type:Sequelize.INTEGER,
  29. field: 'room'
  30. },
  31. Ext: {
  32. type: Sequelize.STRING,
  33. field: 'ext'
  34. }
  35. }, {
  36. freezeTableName: true // Model tableName will be the same as model name
  37. });
  38.  
  39.  
  40. let Courtroom = sequelize.define('courtroom', {
  41. iD: {
  42. type: Sequelize.INTEGER,
  43. field: 'id', primaryKey: true
  44. },
  45. Number: {
  46. type:Sequelize.STRING,
  47. field: 'number'
  48. }
  49. }, {
  50. freezeTableName: true
  51. });
  52.  
  53. let Participant = sequelize.define('participant', {
  54. iD: {
  55. type: Sequelize.INTEGER,
  56. field: 'id', primaryKey: true
  57. },
  58. Name: {
  59. type: Sequelize.STRING,
  60. field: 'name'
  61. },
  62. Address: {
  63. type: Sequelize.STRING,
  64. field: 'address'
  65. },
  66. Type: {
  67. type: Sequelize.STRING,
  68. field: 'type'
  69. }
  70. }, {
  71. freezeTableName: true
  72. });
  73.  
  74.  
  75. let Case = sequelize.define('case', {
  76. judge_iD: {
  77. type: Sequelize.INTEGER,
  78. field: 'judge_id'
  79. },
  80. courtroom_iD: {
  81. type: Sequelize.INTEGER,
  82. field: 'courtroom_id'
  83. },
  84. claimant_iD: {
  85. type: Sequelize.INTEGER,
  86. field: 'claimant_id'
  87. },
  88. respondent_iD: {
  89. type: Sequelize.INTEGER,
  90. field: 'respondent_id'
  91. },
  92. start_Date: {
  93. type: Sequelize.DATEONLY,
  94. field: 'start_date'
  95. },
  96. duratioN: {
  97. type: Sequelize.RANGE(Sequelize.DATE),
  98. field: 'duration'
  99. },
  100. resulT: {
  101. type: Sequelize.BOOLEAN,
  102. field: 'result'
  103. }
  104. }, {
  105. freezeTableName: true
  106.  
  107. });
  108.  
  109. // crypt('12345', gen_salt('bf', 8));
  110.  
  111. let User = sequelize.define('user', {
  112. userName : {
  113. type: Sequelize.STRING,
  114. field: 'username'
  115. },
  116. hashPass : {
  117. type: Sequelize.STRING,
  118. field: 'password'
  119. }
  120. }, {
  121. freezeTableName: true
  122.  
  123. });
  124.  
  125.  
  126. Case.belongsTo(Judge, {foreignKey: 'judgeiD', primaryKey: true})
  127. Case.belongsTo(Participant, {foreignKey: 'claimant_iD', primaryKey: true})
  128. Case.belongsTo(Participant, {foreignKey: 'respondent_iD', primaryKey: true})
  129. Case.belongsTo(Courtroom, {foreignKey: 'courtroom_iD', primaryKey: true})
  130.  
  131. // TEST DATA
  132. Judge.sync({force: true}).then(function () {
  133. // Table created
  134. return Judge.create({
  135. iD: 1,
  136. Name: 'Hancock',
  137. Room: 12,
  138. Ext: 'Yoke'
  139. });
  140. });
  141.  
  142. Courtroom.sync({force: true}).then(function () {
  143. // Table created
  144. return Courtroom.create({
  145. iD: 1,
  146. Number: 3
  147. });
  148. });
  149.  
  150. Participant.sync({force: true}).then(function () {
  151. // Table created
  152. return Participant.create({
  153. iD: 5,
  154. Name: 'Guy Rocinate',
  155. Address: "LaLaLand",
  156. Type: 'Yoke'
  157. });
  158. });
  159. /*
  160. Case.sync({force: true}).then(function () {
  161. // Table created
  162. return Case.create({
  163. judge_iD: 1,
  164. courtroom_iD: 1,
  165. claimant_iD: 11,
  166. respondent_iD: 20,
  167. //start_Date: 2012-04-25,
  168. //duratioN:10/10/17-11/10/17,
  169. resulT: 1
  170. });
  171. });
  172.  
  173.  
  174. */
  175.  
  176. app.listen(3000, () => {
  177. console.log('listening on port 3000');
  178.  
  179. });
  180.  
  181. app.get('/', (req, res) => {
  182. res.send('HelloWorld');
  183. });
  184.  
  185.  
  186. app.get('/judgeSelect', (req, res) => {
  187. Judge.findAll({
  188. where: {
  189. iD: 1
  190. }
  191. }).then(function(Judge){res.send(Judge);})
  192. })
  193.  
  194.  
  195. app.get('/judgeupdate', (req, res) => {
  196. Judge.update({
  197. iD: 3,
  198. }, {
  199. where: {
  200. iD: 1
  201. }
  202. }).then(function(Judge){res.send('ID Updated');})
  203. })
  204.  
  205. app.get('/judgeDel', (req, res) => {
  206. Judge.destroy({
  207. where: {
  208. iD: 2
  209. }
  210. }).then(function(){res.send('user Deleted');})
  211. })
  212.  
  213. app.get('/judgeadd', (req, res) => {
  214. Judge.create({
  215. iD: 2,
  216. Name: 'Barry',
  217. Room: 12,
  218. Ext: '1'
  219. }).then(function(){res.send('user Barry Added');})
  220. });
  221.  
  222. /*
  223. //-------------
  224. app.get('/caseSelect', (req, res) => {
  225. Case.findAll({
  226. where: {
  227. judge_iD: 1
  228. }
  229. }).then(function(Case){res.send(Case);})
  230. })
  231.  
  232.  
  233. app.get('/caseupdate', (req, res) => {
  234. Case.update({
  235. judge_iD: 3,
  236. }, {
  237. where: {
  238. judge_iD: 1
  239. }
  240. }).then(function(Case){res.send('ID Updated');})
  241. })
  242.  
  243. app.get('/caseDel', (req, res) => {
  244. Case.destroy({
  245. where: {
  246. judge_iD: 2
  247. }
  248. }).then(function(){res.send('user Deleted');})
  249. })
  250.  
  251. app.get('/caseadd', (req, res) => {
  252. Case.create({
  253. judge_iD: 1,
  254. courtroom_iD: 1,
  255. claimant_iD: 1,
  256. respondent_id: 2,
  257. //start_Date: 2012-04-25,
  258. //duratioN:10/10/17-11/10/17,
  259. resulT: 1
  260. }).then(function(){res.send('Case Added');})
  261. });
  262.  
  263. */
  264. //-------------
  265. app.get('/CourtroomSelect', (req, res) => {
  266. Courtroom.findAll({
  267. where: {
  268. iD: 1
  269. }
  270. }).then(function(Case){res.send(Case);})
  271. })
  272.  
  273.  
  274. app.get('/Courtroomupdate', (req, res) => {
  275. Courtroom.update({
  276. iD: 3,
  277. }, {
  278. where: {
  279. judge_iD: 1
  280. }
  281. }).then(function(Case){res.send('ID Updated');})
  282. })
  283.  
  284. app.get('/CourtroomDel', (req, res) => {
  285. Courtroom.destroy({
  286. where: {
  287. iD: 2
  288. }
  289. }).then(function(){res.send(' Deleted');})
  290. })
  291.  
  292. app.get('/Courtroomadd', (req, res) => {
  293. Courtroom.create({
  294. iD: 2,
  295. Number: 1
  296. }).then(function(){res.send('Courtroom Added');})
  297. });
  298.  
  299. //--------
  300.  
  301.  
  302. app.get('/ParticipantSelect', (req, res) => {
  303. Judge.findAll({
  304. where: {
  305. iD: 1
  306. }
  307. }).then(function(Participant){res.send(Participant);})
  308. })
  309.  
  310.  
  311. app.get('/Participantupdate', (req, res) => {
  312. Participant.update({
  313. iD: 3,
  314. }, {
  315. where: {
  316. iD: 1
  317. }
  318. }).then(function(Participant){res.send('ID Updated');})
  319. })
  320.  
  321. app.get('/ParticipantDel', (req, res) => {
  322. Participant.destroy({
  323. where: {
  324. iD: 2
  325. }
  326. }).then(function(){res.send('user Deleted');})
  327. })
  328.  
  329. app.get('/Participantadd', (req, res) => {
  330. Participant.create({
  331. iD: 1,
  332. Name: 'Guy Mon',
  333. Address: "Tallaght",
  334. Type: 'claimant'
  335. }).then(function(){res.send(' Barry Added');})
  336. });
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346. // LAB 3B
  347. // Part 1
  348. var router = express.Router();
  349. var SECRET = "super-secret";
  350. var passphrase = "pass-phrase"
  351.  
  352.  
  353.  
  354. app.get('/newUser', function(req, res){
  355. console.log(req);
  356. db.run("Insert into userslab3 (username, password) values ('dave', crypt('password', gen_salt('bf')));", [], function(err, result) {
  357. console.log(err);
  358. res.end(JSON.stringify(err));
  359. });
  360. })
  361.  
  362.  
  363. // Part 2
  364. app.get('/checkUser/:username/:password', function(req, res){
  365. db.run("select * from userslab3 where username = $1 and password = crypt($2, password)",
  366. [req.params.username, req.params.password], function(err, result){
  367. console.log(result);
  368. console.log(err);
  369. if(result[0].username == req.params.username)
  370. {
  371. // If username is there, return JWT with set of claims
  372. var token = jwt.sign(result[0], SECRET, {expiresIn: 1440});
  373. res.json({
  374. success: true,
  375. message:"Token given",
  376. token: token
  377. });
  378. }// end if
  379. else{
  380. console.log(result[0].username);
  381. }
  382. });
  383. })
  384.  
  385.  
  386. router.use(function(req, res, next){
  387. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  388. if (token){
  389. jwt.verify(token, SECRET, function(err, ret) {
  390. if (err){
  391. return res.json({ success: false, message: 'failed auth token'});
  392. } else {
  393. req.ret = ret;
  394. next();
  395. }
  396. } );
  397. }
  398. else {
  399. // No token
  400. return res.status(403).send({
  401. success: false,
  402. message: 'no token'
  403. });
  404. }
  405. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement