Mr_HO1A

API

Feb 27th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Main IMPORTS
  2. const express = require("express");
  3. const bodyParser = require("body-parser");
  4.  
  5. // IMPORTS END
  6. const app = express();
  7. // Server Configs Settings
  8. const PORT = process.env.PORT || 8000;
  9. app.use(bodyParser.urlencoded({
  10.     extended: false
  11. }));
  12. // Configure BodyParser to read json data
  13. app.use(bodyParser.json());
  14. // End
  15.  
  16. // Global Vars Configure
  17.  
  18. // Office Data Structure
  19. let officeData = {
  20.     "error":"none",
  21.     "officeName": "",
  22.     "officeAddress": "",
  23.     "audianceRT": "",
  24.     "estimatedTime": "",
  25.     "pastData": [],
  26.     "counterSchedule": ["", "", ""],
  27.     "lat": "",
  28.     "long": ""
  29. };
  30.  
  31. // Offices Array will hold every office location
  32. let offices = [];
  33. //End
  34.  
  35. // Dummy Offices Data With ID
  36. office = officeData;
  37. office.error="success";
  38. office.officeName = "SBI Bank";
  39. office.officeAddress = "Gahra Chowk Jabalpur, Madhya Pradesh, India";
  40. office.audianceRT = "10";
  41. office.estimatedTime = "20 Mins";
  42. office.counterSchedule = ["10:00 AM", "2:00 PM", "5:30 PM"];
  43. office.pastData = [15,8,9,12,9,15];
  44. office.lat = "23.165376";
  45. office.long = "79.898441";
  46. offices[121] = office;
  47.  
  48. // Allow Cross Domain Request Headers
  49. app.use((req,res,next)=>{
  50.     res.header("Access-Control-Allow-Origin", "*");
  51.     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  52.     next();
  53. });
  54.  
  55. // Server Endpoints
  56. app.get("/v1/officeData/:id", (req, res) => {
  57.     //Getting Office ID form the URL
  58.     const officeID = req.params.id;
  59.     try {
  60.         if (offices[req.params.id] == undefined) {
  61.             throw "Sad";
  62.         }
  63.         res.send(JSON.stringify(offices[req.params.id]));
  64.         console.log("Querry Sent! Success");
  65.     } catch (error) {
  66.         res.send(JSON.stringify({
  67.             "error": "Looks Like Something Messed UP!"
  68.         }));
  69.         console.log("Something Messed Up!");
  70.     }
  71. });
  72. //End
  73. app.get("/",(req,res)=>{
  74.     res.send("Looks Like You Forgot Something");
  75. });
  76.  
  77. // Data Update Endpoint
  78. app.get("/v1/officeDataUpdateRT/:id/:people",(req,res)=>{
  79.     const officeID = req.param.id;
  80.     const updatePeole = req.param.people;
  81.     // To Check if the id in array exist
  82.     try{
  83.         // Get office data object
  84.         let toUpdateOffice = offices[officeId];
  85.         toUpdateOffice.audianceRT = updatePeople;
  86.         console.log("Data Updated for Camera ID : "+officeID+"\nUpdated Audiance : "+updatePeole);
  87.     }
  88.     catch(error){
  89.         console.log("Some Thing Messed Up To Update For Camera ID : "+officeID);
  90.         console.log("With Error :\n"+error);
  91.     }
  92. });
  93.  
  94. app.post('/v1/setCamera',(req,res)=>{
  95.     let CameraSetData = req.body;
  96.     let setOffice = officeData;
  97.     // Setting Office Data From Camera Data
  98.     setOffice.officeName = CameraSetData.officeName;
  99.     setOffice.officeAddress = CameraSetData.officeAddress;
  100.     setOffice.audianceRT = CameraSetData.audianceRT == undefined ? 0 : CameraSetData.audianceRT;
  101.     console.log(setOffice);
  102. });
  103.  
  104. //For Message Server
  105. app.get("/v1/makeMessage/:id",(req,res)=>{
  106.     const officeID = req.params.id;
  107.     try {
  108.         if (offices[req.params.id] == undefined) {
  109.             throw "Sad";
  110.         }
  111.         let data = offices[req.params.id];
  112.         res.send("Office Name : "+data.officeName+"\nCurrent Audiance : "+data.audianceRT+"\nEstimated Time : "+data.estimatedTime);
  113.         console.log("Querry Sent! Success");
  114.     } catch (error) {
  115.         res.send(JSON.stringify({
  116.             "error": "Looks Like Something Messed UP!"
  117.         }));
  118.         console.log("Something Messed Up!");
  119.     }
  120. });
  121.  
  122. //Server Start
  123. app.listen(PORT, () => {
  124.     console.log("Server Started On : http://127.0.0.1:" + PORT);
  125. });
Add Comment
Please, Sign In to add comment