-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathserver.js
166 lines (136 loc) · 3.74 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const express = require('express');
const mustacheExpress = require('mustache-express');
const app = express();
const mustache = mustacheExpress();
const bodyParser = require('body-parser');
const { Client } = require('pg');
mustache.cache = null;
app.engine('mustache',mustache);
app.set('view engine','mustache');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:false}));
app.get('/add',(req,res)=>{
res.render('med-form');
});
app.post('/meds/add',(req,res)=>{
console.log('post body',req.body);
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
console.log('connection complete');
const sql = 'INSERT INTO meds (name,count,brand) VALUES ($1, $2, $3)';
const params = [req.body.name,req.body.count,req.body.brand];
return client.query(sql,params);
})
.then((result)=>
{
console.log('results?',result);
res.redirect('/meds');
});
});
//dashboard
app.get('/dashboard',(req,res)=>
{
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
return client.query('SELECT SUM(count) FROM meds; SELECT DISTINCT COUNT(brand) FROM meds')
})
.then((results)=>{
console.log('?results',results[0]);
console.log('?results',results[1]);
res.render('dashboard',{n1:results[0].rows,n2:results[1].rows});
})
});
app.get('/meds',(req,res)=>{
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
return client.query('SELECT * FROM meds');
})
.then((results)=>
{
console.log('results?',results);
res.render('meds',results);
});
});
app.post('/meds/delete/:id',(req,res)=>{
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
const sql = 'DELETE FROM meds WHERE mid=$1'
const params=[req.params.id];
return client.query(sql,params);
})
.then((results)=>
{
res.redirect('/meds');
});
});
app.get('/meds/edit/:id',(req,res)=>{
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
const sql = 'SELECT * FROM meds WHERE mid=$1'
const params=[req.params.id];
return client.query(sql,params);
})
.then((result)=>
{
console.log('results?',result);
res.render('med-edit',result);
});
});
app.post('/meds/update/:id',(req,res)=>{
const client = new Client({
user:'postgres',
host: 'localhost',
database: 'medical',
password: 'pklid471',
port: 5432,
});
client.connect()
.then(()=>{
console.log('connection complete');
const sql = 'UPDATE meds SET name=$1,count=$2,brand=$3 WHERE mid=$4';
const params = [req.body.name,req.body.count,req.body.brand,req.params.id];
return client.query(sql,params);
})
.then((results)=>
{
res.redirect('/meds');
});
});
app.listen(5001,()=>{
console.log('Listening to port 5001');
});