-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (75 loc) · 3.15 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
const express=require('express');
const app=express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const MongoClient = require('mongodb').MongoClient;
let server=require('./server');
let config=require('./config');
let middleware=require('./middleware');
const response=require('express');
const url='mongodb://127.0.0.1:27017';
const dbname="HospitalManagement";
let db
MongoClient.connect(url,{ useUnifiedTopology: true,useNewUrlParser:true},(err,client)=>{
if(err) return console.log(err);
db=client.db(dbname);
console.log(`connected database: ${url}`);
console.log(`Database:${dbname}`);
});
app.get('/hospital', middleware.checkToken, (req,res)=>{
console.log("getting things ready");
const data=db.collection("hospital").find().toArray().then(result => res.json(result));
});
app.get('/ventilator', middleware.checkToken, (req,res)=>{
console.log("getting things ready");
const data=db.collection("ventilators").find().toArray().then(result=>(res.json(result)));
});
app.post('/searchventbystatus', middleware.checkToken, (req,res) => {
const status = req.body.status;
console.log(status);
const data=db.collection('ventilators').find({"status":status}).toArray().then(result=>res.json(result));
});
app.post('/searchventbyname', middleware.checkToken, (req,res) => {
const name=req.body.name;
console.log(name);
const data=db.collection('ventilators').find({'name':new RegExp(name, 'i')}).toArray().then(result=>res.json(result));
});
app.post('/searchhospital', middleware.checkToken, (req,res) => {
const name=req.body.name;
console.log(name);
const data=db.collection('hospital').find({'name':new RegExp(name, 'i')}).toArray().then(result=>res.json(result));
});
app.put('/addvent',(req,res)=>{
const hId=req.body.hId;
const ventilatorId=req.body.ventilatorId;
const status=req.body.status;
const name=req.body.name;
console.log("adding ventilator, please wait a moment");
const item={"hId":hId, "ventilatorId":ventilatorId, "status":status, "name":name};
db.collection("ventilators").insertOne(item, function(err, result){
res.json("inserted successfully");
});
});
app.post('/updateventilator', middleware.checkToken, (req,res) => {
const ventilatorId= {ventilatorId: req.body.ventilatorId};
console.log(ventilatorId);
const newvalues={$set: {status:req.body.status}};
console.log("updating ventilator details, please wait a moment");
db.collection("ventilators").updateOne(ventilatorId, newvalues, function(err, result){
res.json('updated one document');
if(err) throw err;
});
});
app.delete('/deletevent', middleware.checkToken, (req,res) => {
const ventilatorId=req.body.ventilatorId;
console.log(ventilatorId);
const temp={"ventilatorId":ventilatorId};
db.collection("ventilators").deleteOne(temp, function(err,obj){
if(err) throw err;
res.json("deleted one element");
});
});
app.listen(9000,(req,res)=>{
console.log("working well");
})