Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //LAB 2 PART 1
- const express = require('express');
- const massive = require('massive');
- var Sequelize = require('sequelize');
- var jwt = require('jsonwebtoken');
- var bp = require('body-parser');
- var crypt = require('cryptico');
- let sequelize = new Sequelize('postgres://postgres:@localhost:5432/court');
- const app = express();
- const db = massive.connectSync({
- db: 'court',
- });
- let Judge = sequelize.define('judge',{
- iD: {
- type: Sequelize.INTEGER,
- field: 'id', primaryKey: true // attribute that is iD when judge facing but 'id' in the db
- },
- Name: {
- type: Sequelize.STRING,
- field: 'name'
- },
- Room: {
- type:Sequelize.INTEGER,
- field: 'room'
- },
- Ext: {
- type: Sequelize.STRING,
- field: 'ext'
- }
- }, {
- freezeTableName: true // Model tableName will be the same as model name
- });
- 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
- });
- // crypt('12345', gen_salt('bf', 8));
- let User = sequelize.define('user', {
- userName : {
- type: Sequelize.STRING,
- field: 'username'
- },
- hashPass : {
- type: Sequelize.STRING,
- field: 'password'
- }
- }, {
- freezeTableName: true
- });
- Case.belongsTo(Judge, {foreignKey: 'judgeiD', primaryKey: true})
- Case.belongsTo(Participant, {foreignKey: 'claimant_iD', primaryKey: true})
- Case.belongsTo(Participant, {foreignKey: 'respondent_iD', primaryKey: true})
- Case.belongsTo(Courtroom, {foreignKey: 'courtroom_iD', primaryKey: true})
- // TEST DATA
- Judge.sync({force: true}).then(function () {
- // Table created
- return Judge.create({
- iD: 1,
- Name: 'Hancock',
- Room: 12,
- Ext: 'Yoke'
- });
- });
- Courtroom.sync({force: true}).then(function () {
- // Table created
- return Courtroom.create({
- iD: 1,
- Number: 3
- });
- });
- Participant.sync({force: true}).then(function () {
- // Table created
- return Participant.create({
- iD: 5,
- Name: 'Guy Rocinate',
- Address: "LaLaLand",
- Type: 'Yoke'
- });
- });
- /*
- Case.sync({force: true}).then(function () {
- // Table created
- return Case.create({
- judge_iD: 1,
- courtroom_iD: 1,
- claimant_iD: 11,
- respondent_iD: 20,
- //start_Date: 2012-04-25,
- //duratioN:10/10/17-11/10/17,
- resulT: 1
- });
- });
- */
- app.listen(3000, () => {
- console.log('listening on port 3000');
- });
- app.get('/', (req, res) => {
- res.send('HelloWorld');
- });
- app.get('/judgeSelect', (req, res) => {
- Judge.findAll({
- where: {
- iD: 1
- }
- }).then(function(Judge){res.send(Judge);})
- })
- app.get('/judgeupdate', (req, res) => {
- Judge.update({
- iD: 3,
- }, {
- where: {
- iD: 1
- }
- }).then(function(Judge){res.send('ID Updated');})
- })
- app.get('/judgeDel', (req, res) => {
- Judge.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- app.get('/judgeadd', (req, res) => {
- Judge.create({
- iD: 2,
- Name: 'Barry',
- Room: 12,
- Ext: '1'
- }).then(function(){res.send('user Barry Added');})
- });
- /*
- //-------------
- app.get('/caseSelect', (req, res) => {
- Case.findAll({
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send(Case);})
- })
- app.get('/caseupdate', (req, res) => {
- Case.update({
- judge_iD: 3,
- }, {
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send('ID Updated');})
- })
- app.get('/caseDel', (req, res) => {
- Case.destroy({
- where: {
- judge_iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- app.get('/caseadd', (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');})
- });
- */
- //-------------
- app.get('/CourtroomSelect', (req, res) => {
- Courtroom.findAll({
- where: {
- iD: 1
- }
- }).then(function(Case){res.send(Case);})
- })
- app.get('/Courtroomupdate', (req, res) => {
- Courtroom.update({
- iD: 3,
- }, {
- where: {
- judge_iD: 1
- }
- }).then(function(Case){res.send('ID Updated');})
- })
- app.get('/CourtroomDel', (req, res) => {
- Courtroom.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send(' Deleted');})
- })
- app.get('/Courtroomadd', (req, res) => {
- Courtroom.create({
- iD: 2,
- Number: 1
- }).then(function(){res.send('Courtroom Added');})
- });
- //--------
- app.get('/ParticipantSelect', (req, res) => {
- Judge.findAll({
- where: {
- iD: 1
- }
- }).then(function(Participant){res.send(Participant);})
- })
- app.get('/Participantupdate', (req, res) => {
- Participant.update({
- iD: 3,
- }, {
- where: {
- iD: 1
- }
- }).then(function(Participant){res.send('ID Updated');})
- })
- app.get('/ParticipantDel', (req, res) => {
- Participant.destroy({
- where: {
- iD: 2
- }
- }).then(function(){res.send('user Deleted');})
- })
- app.get('/Participantadd', (req, res) => {
- Participant.create({
- iD: 1,
- Name: 'Guy Mon',
- Address: "Tallaght",
- Type: 'claimant'
- }).then(function(){res.send(' Barry Added');})
- });
- // LAB 3B
- // Part 1
- var router = express.Router();
- var SECRET = "super-secret";
- var passphrase = "pass-phrase"
- app.get('/newUser', function(req, res){
- console.log(req);
- db.run("Insert into userslab3 (username, password) values ('dave', crypt('password', gen_salt('bf')));", [], function(err, result) {
- console.log(err);
- res.end(JSON.stringify(err));
- });
- })
- // Part 2
- app.get('/checkUser/:username/:password', function(req, res){
- 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)
- {
- // If username is there, return JWT with set of claims
- var token = jwt.sign(result[0], SECRET, {expiresIn: 1440});
- res.json({
- success: true,
- message:"Token given",
- token: token
- });
- }// end if
- else{
- console.log(result[0].username);
- }
- });
- })
- router.use(function(req, res, next){
- var token = req.body.token || req.query.token || req.headers['x-access-token'];
- if (token){
- jwt.verify(token, SECRET, function(err, ret) {
- if (err){
- return res.json({ success: false, message: 'failed auth token'});
- } else {
- req.ret = ret;
- next();
- }
- } );
- }
- else {
- // No token
- return res.status(403).send({
- success: false,
- message: 'no token'
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement