-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (102 loc) · 3.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var express = require("express");
var MongoClient = require("mongodb");
const bodyParser = require("body-parser");
let server=require('./server');
let config=require('./config');
let middleware=require('./middleware');
var app = express();
const url = "mongodb://127.0.0.1:27017";
const dbname = 'HospitalInventory';
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
let db
MongoClient.connect(url,(err,client) =>{
if(err) return console.log(err);
db = client.db(dbname);
console.log(`Connected Database: ${url}`);
console.log(`Database:${dbname}`);
});
//GET
app.get('/hosp',middleware.checkToken, (req,res) => {
console.log("Fetching data from hospital collection.....");
var data = db.collection("Hospital").find().toArray().then(result => res.json(result));
});
app.get('/vent', middleware.checkToken, (req,res) => {
console.log("Fetching data from ventilator collection.....");
var data = db.collection("Ventilator").find().toArray().then(result => res.json(result));
});
//SEARCH
app.post('/ventsearch',middleware.checkToken,(req,res) =>{
var status = req.body.status;
var data = db.collection("Ventilator").find({"status":status}).toArray().then(result=>res.json(result));
console.log(status);
});
app.post('/hospsearch',middleware.checkToken,(req,res) =>{
var name = req.body.name;
var data = db.collection("Hospital").find({"name":new RegExp(name, 'i')}).toArray().then(result=>res.json(result));
console.log(name);
});
//CREATE
app.post('/addvent',middleware.checkToken, (req, res) => {
var hId = req.body.hId;
var ventilatorId = req.body.ventilatorId;
var status = req.body.status;
var name = req.body.name;
console.log('Adding ventilator.....');
var add = {"hId":hId, "ventilatorId":ventilatorId,"status":status,"name":name};
var data = db.collection("Ventilator").insertOne(add, (err, result) => {
if (err) throw err;
res.json("Ventilator added!");
});
});
app.post('/addhosp',middleware.checkToken, (req, res) => {
var hId = req.body.hId;
var name = req.body.name;
var address = req.body.address;
var contactNo = req.body.contactNo;
console.log('Adding hospital.....');
var add = {"hId":hId, "name":name,"address":address,"contactNo":contactNo};
var data = db.collection("Hospital").insertOne(add,(err, result) => {
if (err) throw err;
res.json("Hospital added!");
});
});
//UPDATE
app.put('/updatevent',middleware.checkToken, (req, res) => {
var ventilatorId = req.body.ventilatorId;
console.log(ventilatorId);
var status = req.body.status;
console.log(status);
var data = db.collection("Ventilator").updateOne({ "ventilatorId": ventilatorId }, { $set: { "status": status } }, (err, result) => {
if (err) throw err;
res.json("Updated ventilator!");
});
});
app.put('/updatehosp',middleware.checkToken, (req, res) => {
var hId = req.body.hId;
console.log(hId);
var name = req.body.name;
console.log(name);
var data = db.collection("Hospital").updateOne({ "name": name }, { $set: { "hId": hId } }, (err, result) => {
if (err) throw err;
res.json("Updated hospital!");
});
});
//DELETE
app.delete('/deletevent',middleware.checkToken, (req, res) => {
var ventilatorId = req.body.ventilatorId;
console.log(ventilatorId);
var data = db.collection("Ventilator").deleteOne({ "ventilatorId": ventilatorId }, (err, obj) => {
if (err) throw err;
res.json("Ventilator deleted!!");
});
});
app.delete('/deletehosp', middleware.checkToken, (req, res) => {
var hId = req.body.hId;
console.log(hId);
var data = db.collection("Hospital").deleteOne({ "hId": hId }, (err, obj) => {
if (err) throw err;
res.json("Hospital deleted!!");
});
});
app.listen(3000);