Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require("express");
  2. var sqlite3 = require("sqlite3");
  3. var bodyParser = require("body-parser");
  4.  
  5. var app = express();
  6. var db = new sqlite3.Database('./Jeopardy.db');
  7.  
  8. var hat = require('hat'); //for generating tokens
  9.  
  10. app.use( bodyParser.urlencoded({ extended: true }));
  11.  
  12. app.use(bodyParser.json());
  13.  
  14.  
  15. app.get("/", function(req,res) {
  16.     db.get("select * from users",function(e,u){
  17.         return res.json(u);
  18.     })
  19.     return res.send("Hello World");
  20. })
  21.  
  22.  
  23. app.post('/auth/signin', function(req, res) {
  24.     var userID = req.body.userID;
  25.     var password = req.body.password;
  26.  
  27.     if(userID == null || password == null) {
  28.         return res.status(401).json({message: "invalid_credentials"});
  29.     }
  30.  
  31.     var dbQuery = "select * from Users where UserID = ? and UserPassword = ?";
  32.     var requestParams = [userID, password];
  33.  
  34.     db.get(dbQuery, requestParams, function(err, user) {
  35.         if(err) {
  36.             return res.status(500).json({message: "Internal server error"});
  37.         }
  38.  
  39.         if(user == null) {
  40.             return res.status(401).json({message: "invalid_credentials"});
  41.         }  
  42.         var id = hat();
  43.         var date = new Date();
  44.         requestParams = [id, date, userID];
  45.         var dbQuery1 = "UPDATE Users SET AuthToken = ? , AuthTokenIssued = ? WHERE UserID = ?"
  46.         db.run(dbQuery1, requestParams, function(err, user) {
  47.             if (err) {
  48.                 console.log(err);
  49.                 return res.status(500).json({message: "Internal server error"});
  50.             }
  51.             return res.status(200).json({message: "success", authToken: id});
  52.         });
  53.     });
  54. });
  55.  
  56.  
  57. app.get('/questions', function(req, res) {
  58.     var auth = req.query.auth;
  59.     if (auth === undefined) return res.status(400).json({message: "unauthorized access"});
  60.     var dbQuery = "select * from Users where AuthToken = ?";
  61.     var requestParams = [auth];
  62.     db.get(dbQuery, requestParams, function(err, user) {
  63.         if(err) {
  64.             return res.status(500).json({message: "Internal server error"});
  65.         }
  66.         if(user == null) {
  67.             return res.status(400).json({message: "unauthorized access"});
  68.         }
  69.         var time = parseInt(((new Date )- user.AuthTokenIssued)/1000/3600);
  70.         if (time > 1) {
  71.             return res.status(400).json({message: "auth token expired"});
  72.         }
  73.     });
  74.  
  75.     var categoryTitle = req.query.categoryTitle;
  76.     var dollarValue = req.query.dollarValue;
  77.     var questionText = req.query.questionText;
  78.     var answerText = req.query.answerText;
  79.     var showNumber = req.query.showNumber;
  80.     var airDate = req.query.airDate;
  81.  
  82.     var dbQuery = "select * from Questions join Categories on Questions.CategoryCode = Categories.CategoryCode where ";
  83.     var paramCount = 0;
  84.     var params = [];
  85.  
  86.     if (categoryTitle != null) {
  87.  
  88.         if(paramCount > 0) {
  89.             dbQuery = dbQuery + 'and ';
  90.         }
  91.  
  92.         paramCount++;
  93.         dbQuery = dbQuery + 'CategoryTitle = ? ';
  94.         params.push(categoryTitle.toUpperCase());
  95.     }
  96.  
  97.     if (dollarValue != null) {
  98.  
  99.         if(paramCount > 0) {
  100.             dbQuery = dbQuery + 'and ';
  101.         }
  102.  
  103.         paramCount++;
  104.         dbQuery = dbQuery + 'DollarValue = ? ';
  105.         dollarValue = "$" + dollarValue;
  106.         params.push(dollarValue);
  107.     }
  108.  
  109.     if (questionText) {
  110.  
  111.         if(paramCount > 0) {
  112.             dbQuery = dbQuery + 'and ';
  113.         }
  114.  
  115.         paramCount++;
  116.         dbQuery = dbQuery + 'QuestionText like ? ' ;
  117.         questionText = '%' + questionText + '%';
  118.         params.push(questionText);
  119.     }
  120.  
  121.     if (answerText) {
  122.  
  123.         if(paramCount > 0) {
  124.             dbQuery = dbQuery + 'and ';
  125.         }
  126.  
  127.         paramCount++;
  128.         dbQuery = dbQuery + 'AnswerText = ? ';
  129.         params.push(answerText);
  130.     }
  131.  
  132.     if (showNumber) {
  133.  
  134.         if(paramCount > 0) {
  135.             dbQuery = dbQuery + 'and ';
  136.         }
  137.  
  138.         paramCount++;
  139.         dbQuery = dbQuery + 'ShowNumber = ? ';
  140.         params.push(showNumber);
  141.     }
  142.  
  143.     if (airDate) {
  144.  
  145.         if(paramCount > 0) {
  146.             dbQuery = dbQuery + 'and ';
  147.         }
  148.  
  149.         paramCount++;
  150.         dbQuery = dbQuery + 'AirDate = ? ';
  151.         params.push(airDate);
  152.     }
  153.  
  154.     dbQuery = dbQuery + 'order by AirDate desc';
  155.  
  156.     if(paramCount == 0) {
  157.         dbQuery = "select * from Questions order by AirDate desc";
  158.     }
  159.  
  160.     db.all(dbQuery, params, (err, questions) => {
  161.  
  162.         if(questions.length > 5000) {
  163.             return res.status(400).json({message: "too_many_results"});
  164.         }
  165.  
  166.         if (err) {
  167.             console.log(err);
  168.             return res.status(500).json({message: "Internal server error"});
  169.         }
  170.  
  171.         return res.status(200).json(questions);
  172.     });
  173. });
  174.  
  175. var port = process.env.PORT || 8000;
  176. app.listen(port, function() {
  177.     console.log("Running server on port " + port);
  178. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement