Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = sequelize;
- var massive = require("massive");
- var express = require("express");
- var Sequelize = require('sequelize');
- var app = express();
- var jwt = require('jsonwebtoken');
- var bodyParser = require('body-parser');
- var cryptico = require('cryptico');
- var apiRoutes = express.Router();
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(bodyParser.json());
- var sequelize = new Sequelize('postgres://postgres:barry@localhost:5432/courtsystem');
- var SECRET = 'shhhhhhared-secret';
- app.get('/', function (req, res) {
- res.send('Welcome to court system!!! port 8095')
- })
- app.listen(8095, function () {
- console.log('Example app listening on port 8095!')
- })
- let Judge = sequelize.define('judge',{
- iD: {
- type: Sequelize.INTEGER,
- field: 'id',
- primaryKey: true
- },
- Name: {
- type: Sequelize.STRING,
- field: 'name'
- },
- Room: {
- type:Sequelize.INTEGER,
- field: 'room'
- },
- Ext: {
- type: Sequelize.STRING,
- field: 'ext'
- }
- }, {
- freezeTableName: true
- });
- let Courtroom = sequelize.define('courtroom', {
- iD: {
- type: Sequelize.INTEGER,
- field: 'id',
- primaryKey: true
- },
- Number: {
- type:Sequelize.STRING,
- field: 'number'
- }
- }, {
- freezeTableName: true
- });
- let Participant = sequelize.define('participant', {
- iD: {
- type: Sequelize.INTEGER,
- field: 'id',
- primaryKey: true
- },
- Name: {
- type: Sequelize.STRING,
- field: 'name'
- },
- Address: {
- type: Sequelize.STRING,
- field: 'address'
- },
- Type: {
- type: Sequelize.STRING,
- field: 'type'
- }
- }, {
- freezeTableName: true
- });
- let Case = sequelize.define('case', {
- judge_iD: {
- type: Sequelize.INTEGER,
- field: 'judge_id'
- },
- courtroom_iD: {
- type: Sequelize.INTEGER,
- field: 'courtroom_id'
- },
- claimant_iD: {
- type: Sequelize.INTEGER,
- field: 'claimant_id'
- },
- respondent_iD: {
- type: Sequelize.INTEGER,
- field: 'respondent_id'
- },
- start_Date: {
- type: Sequelize.DATEONLY,
- field: 'start_date'
- },
- duration: {
- type: Sequelize.RANGE(Sequelize.DATE),
- field: 'duration'
- },
- result: {
- type: Sequelize.BOOLEAN,
- field: 'result'
- }
- }, {
- freezeTableName: true
- });
- Judge.sync({force: true}).then(function () {
- return Judge.create({
- iD: 1,
- Name: 'Barry',
- Room: 12,
- Ext: '1'
- });
- });
- Participant.sync({force: true}).then(function () {
- return Participant.create({
- iD: 5,
- Name: 'Barry Burke',
- Address: "Tallaght",
- Type: 'claimant'
- });
- });
- Courtroom.sync({force: true}).then(function () {
- return Courtroom.create({
- iD: 1,
- Number: 1
- });
- });
- Case.sync({force: true}).then(function () {
- return Case.create({
- judge_iD: 1,
- courtroom_iD: 1,
- claimant_iD: 1,
- respondent_id: 2,
- //start_Date: 2012-04-25,
- //duratioN:10/10/17-11/10/17,
- resulT: 1
- });
- });
- Case.belongsTo(Judge, { foreignKey: 'judgeiD', primaryKey: true});
- Case.belongsTo(Participant, { foreignKey: 'claimantID', primaryKey: true });
- Case.belongsTo(Participant, { foreignKey: 'respondent_iD', primaryKey: true});
- Case.belongsTo(Courtroom, {foreignKey: 'courtroom_id', primaryKey: true});
- //var token = jwt.sign(user, app.get('superSecret'), {
- //expiresInMinutes: 1440 // expires in 24 hours
- //});
- app.get('/ValidateUser/:username/:password', function (req, res) {
- console.log(res)
- massive.connect({connectionString:'postgres://postgres:barry@localhost:5432/courtsystem'}, function(err, db)
- {
- db.run("Select * from userslab3 where username = $1 and password = crypt($2, password)", [req.params.username, req.params.password], function(err, result){
- console.log(result);
- console.log(err);
- if(result[0].username == req.params.username)
- {
- var token = jwt.sign(result[0], SECRET, { expiresIn: 1440 });
- res.json({
- success: true,
- message: 'Enjoy your token!',
- token: token
- });
- }
- });
- });
- })
- app.get('/InsertUser', function (req, res) {
- massive.connect({connectionString: 'postgres://postgres:barry@localhost:5432/courtsystem'}, function(err, db)
- {
- db.run("Insert into userslab3 (username, password) values ('Barry', crypt('Password1', gen_salt('bf')));", [], function(err, result){
- console.log(err);
- res.end(JSON.stringify(err));
- });
- });
- })
- var PassPhrase = "SuperSecret";
- var Bits = 160;
- var RSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
- var PublicKeyString = cryptico.publicKeyString(RSAkey);
- massive.connect({connectionString: "postgres://postgres:goldfish1@localhost:5432/postgres"}, function(err, db)
- {
- db.run("Update users set accesskey = $1, secretkey = $2 where username = 'Barry';", [PublicKeyString, RSAkey], function(err, result){
- console.log(err);
- console.log(result);
- });
- });
- apiRoutes.use(function(req, res, next) {
- // check header or url parameters or post parameters for token
- var token = req.body.token || req.query.token || req.headers['x-access-token'];
- if (token) {
- // verifies secret and checks exp
- jwt.verify(token, SECRET, function(err, decoded) {
- if (err) {
- return res.json({ success: false, message: 'Failed to authenticate token.' });
- } else {
- // if everything is good, save to request for use in other routes
- req.decoded = decoded;
- next();
- }
- });
- }
- else{
- // if there is no token
- // return an error
- return res.status(403).send({
- success: false,
- message: 'No token provided.'
- });
- }
- });
- app.use('/api', apiRoutes);
- apiRoutes.get('/Message/:message', (req,res) => {
- var EncryptionResult = cryptico.encrypt(req.params.message, PublicKeyString);
- res.send(EncryptionResult);
- })
- apiRoutes.get('/Judge', (req,res) => {
- Judge.findAll({
- where: {
- iD: 1
- }
- }).then(function(Judge){return res.json(Judge);})
- })
- apiRoutes.get('/UpdateJudge', (req, res) => {
- Judge.update({
- iD: 3,
- }, {
- where: {
- iD: 1
- }
- }).then(function(Judge){res.send('ID Updated');})
- })
- apiRoutes.get('/DeleteJudge', (req, res) => {
- Judge.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- apiRoutes.get('/AddJudge', (req, res) => {
- Judge.create({
- iD: 2,
- Name: 'Barry',
- Room: 12,
- Ext: '1'
- }).then(function(){res.send('user Barry Added');})
- });
- //-------------
- apiRoutes.get('/Case', (req, res) => {
- Case.findAll({
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send(Case);})
- })
- apiRoutes.get('/UpdateCase', (req, res) => {
- Case.update({
- judge_iD: 3,
- }, {
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send('ID Updated');})
- })
- apiRoutes.get('/DeleteCase', (req, res) => {
- Case.destroy({
- where: {
- judge_iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- apiRoutes.get('/AddCase', (req, res) => {
- Case.create({
- judge_iD: 1,
- courtroom_iD: 1,
- claimant_iD: 1,
- respondent_id: 2,
- //start_Date: 2012-04-25,
- //duratioN:10/10/17-11/10/17,
- resulT: 1
- }).then(function(){res.send('Case Added');})
- });
- //-------------
- apiRoutes.get('/Courtroom', (req, res) => {
- var bool = verify();
- if(bool)
- {
- Courtroom.findAll({
- where: {
- iD: 1
- }
- }).then(function(Case){res.send(Case);})
- }
- else{
- res.send(401);
- }
- })
- apiRoutes.get('/UpdatedCourtroom', (req, res) => {
- Courtroom.update({
- iD: 3,
- }, {
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send('ID Updated');})
- })
- apiRoutes.get('/DeleteCourtroom', (req, res) => {
- Courtroom.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send(' Deleted');})
- })
- apiRoutes.get('/AddCourtroom', (req, res) => {
- Courtroom.create({
- iD: 2,
- Number: 1
- }).then(function(){res.send('Courtroom Added');})
- });
- apiRoutes.get('/Participant', (req, res) => {
- Participant.findAll({
- where: {
- iD: 1
- }
- }).then(function(Participant){res.send(Participant);})
- })
- apiRoutes.get('/UpdateParticipant', (req, res) => {
- Participant.update({
- iD: 3,
- }, {
- where: {
- iD: 1
- }
- }).then(function(Participant){res.send('ID Updated');})
- })
- apiRoutes.get('/DeleteParticipant', (req, res) => {
- Participant.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- apiRoutes.get('/AddParticipant', (req, res) => {
- Participant.create({
- iD: 1,
- Name: 'Baz Burke',
- Address: "Tallaght",
- Type: 'claimant'
- }).then(function(){res.send(' Barry Added');})
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement