· 9 years ago · Mar 26, 2017, 09:06 PM
1module.exports = sequelize;
2
3var massive = require("massive");
4var express = require("express");
5var Sequelize = require('sequelize');
6var app = express();
7var jwt = require('jsonwebtoken');
8var bodyParser = require('body-parser');
9var cryptico = require('cryptico');
10
11
12
13var apiRoutes = express.Router();
14app.use(bodyParser.urlencoded({ extended: false }));
15app.use(bodyParser.json());
16
17var sequelize = new Sequelize('postgres://postgres:barry@localhost:5432/courtsystem');
18var SECRET = 'shhhhhhared-secret';
19
20
21app.get('/', function (req, res) {
22 res.send('Welcome to court system!!! port 8095')
23})
24
25app.listen(8095, function () {
26 console.log('Example app listening on port 8095!')
27})
28
29
30
31
32
33
34
35let 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
57let 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
71let 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
93let 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
139Participant.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
149Courtroom.sync({force: true}).then(function () {
150 return Courtroom.create({
151 iD: 1,
152 Number: 1
153 });
154});
155
156Case.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
170Case.belongsTo(Judge, { foreignKey: 'judgeiD', primaryKey: true});
171Case.belongsTo(Participant, { foreignKey: 'claimantID', primaryKey: true });
172Case.belongsTo(Participant, { foreignKey: 'respondent_iD', primaryKey: true});
173Case.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
184app.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
205app.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
221var PassPhrase = "SuperSecret";
222var Bits = 160;
223var RSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
224var PublicKeyString = cryptico.publicKeyString(RSAkey);
225
226massive.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
242apiRoutes.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
279app.use('/api', apiRoutes);
280
281
282
283
284
285
286apiRoutes.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
302apiRoutes.get('/Judge', (req,res) => {
303 Judge.findAll({
304 where: {
305 iD: 1
306 }
307 }).then(function(Judge){return res.json(Judge);})
308})
309
310
311apiRoutes.get('/UpdateJudge', (req, res) => {
312Judge.update({
313 iD: 3,
314}, {
315 where: {
316 iD: 1
317 }
318}).then(function(Judge){res.send('ID Updated');})
319})
320
321apiRoutes.get('/DeleteJudge', (req, res) => {
322Judge.destroy({
323 where: {
324 iD: 2
325 }
326}).then(function(){res.send('user Deleted');})
327})
328
329apiRoutes.get('/AddJudge', (req, res) => {
330Judge.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//-------------
347apiRoutes.get('/Case', (req, res) => {
348Case.findAll({
349 where: {
350 judge_iD: 1
351 }
352}).then(function(Case){res.send(Case);})
353})
354
355
356apiRoutes.get('/UpdateCase', (req, res) => {
357Case.update({
358 judge_iD: 3,
359}, {
360 where: {
361 judge_iD: 1
362 }
363}).then(function(Case){res.send('ID Updated');})
364})
365
366apiRoutes.get('/DeleteCase', (req, res) => {
367Case.destroy({
368 where: {
369 judge_iD: 2
370 }
371}).then(function(){res.send('user Deleted');})
372})
373
374apiRoutes.get('/AddCase', (req, res) => {
375Case.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//-------------
395apiRoutes.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
411apiRoutes.get('/UpdatedCourtroom', (req, res) => {
412Courtroom.update({
413 iD: 3,
414}, {
415 where: {
416 judge_iD: 1
417 }
418}).then(function(Case){res.send('ID Updated');})
419})
420
421apiRoutes.get('/DeleteCourtroom', (req, res) => {
422Courtroom.destroy({
423 where: {
424 iD: 2
425 }
426}).then(function(){res.send(' Deleted');})
427})
428
429apiRoutes.get('/AddCourtroom', (req, res) => {
430Courtroom.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
445apiRoutes.get('/Participant', (req, res) => {
446Participant.findAll({
447 where: {
448 iD: 1
449 }
450}).then(function(Participant){res.send(Participant);})
451})
452
453
454apiRoutes.get('/UpdateParticipant', (req, res) => {
455Participant.update({
456 iD: 3,
457}, {
458 where: {
459 iD: 1
460 }
461}).then(function(Participant){res.send('ID Updated');})
462})
463
464apiRoutes.get('/DeleteParticipant', (req, res) => {
465Participant.destroy({
466 where: {
467 iD: 2
468 }
469}).then(function(){res.send('user Deleted');})
470})
471
472apiRoutes.get('/AddParticipant', (req, res) => {
473Participant.create({
474 iD: 1,
475 Name: 'Baz Burke',
476 Address: "Tallaght",
477 Type: 'claimant'
478}).then(function(){res.send(' Barry Added');})
479 });