-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
104 lines (92 loc) · 2.47 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
const express = require("express");
const app = express();
const cors = require("cors");
var bodyParser = require("body-parser");
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var knex = require("knex")({
client: "mysql",
connection: {
host: "localhost",
port: 3306,
user: "root",
password: "112233",
database: "book"
},
pool: {
afterCreate: (conn, done) => {
conn.query("SET NAMES UTF8", err => {
done(err, conn);
});
}
}
});
function midleware(req, res, next) {
console.log("i am midleware...");
next();
}
app.use(midleware);
// r-1
app.get("/patients", async (req, res) => {
let sql = "select * from patient";
let raw = await knex.raw(sql);
res.json(raw[0]);
});
//r-2
app.get("/patient/:cid", async (req, res) => {
let sql = "select * from patient where cid = ?";
let cid = req.params.cid;
let raw = await knex.raw(sql, [cid]);
res.json(raw[0]);
});
//r-3
app.delete("/patient/:cid", async (req, res) => {
let sql = "delete from patient where cid = ?";
let cid = req.params.cid;
let raw = await knex.raw(sql, [cid]);
res.json(raw[0]);
});
app.post("/new-patient", async (req, res) => {
let data = req.body;
//console.log(data);
await knex("patient").insert(data);
res.send("ok");
});
app.post("/new-booking", async (req, res) => {
// code check
let sql_check = " SELECT count(t.id) as total from booking t ";
sql_check +=
" where t.bed_no = ? and t.book_date = ? and t.book_time = ? ";
let bed_no = req.body.bed_no;
let book_date = req.body.book_date;
let book_time = req.body.book_time;
let raw = await knex.raw(sql_check, [bed_no, book_date, book_time]);
console.log("total", raw[0][0].total);
if (raw[0][0].total > 0) {
res.end("can not book.");
} else {
let data = req.body;
// console.log(data);
await knex("booking").insert(data);
res.send("ok");
}
});
app.get("/bookings", async (req, res) => {
let sql = "select * from booking";
let raw = await knex.raw(sql);
res.json(raw[0]);
});
app.delete("/booking/:id", async (req, res) => {
let sql = "delete from booking where cid = ?";
let cid = req.params.id;
let raw = await knex.raw(sql, [id]);
res.json(raw[0]);
});
app.get("/booking/:id", async (req, res) => {
let sql = "select * from booking where id = ?";
let cid = req.params.id;
let raw = await knex.raw(sql, [id]);
res.json(raw[0]);
});
app.listen(4000, () => console.log("Running 4000!"));