-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
65 lines (59 loc) · 1.74 KB
/
controller.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
const { db } = require("./DB.config");
const SERIALIZE = () => {
db.serialize(() => {
db.run("CREATE TABLE lorem (info TEXT)");
const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (let i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
console.log(row.id + ": " + row.info);
});
});
db.close();
};
const GetAllData = async (req, res) => {
try {
//await db.serialize(() => {
console.log("Database Connection open....");
await db.all("SELECT * FROM Data_Buku", (err, rows) => {
if (err) throw err;
//console.log(rows);
return res.status(200).json(rows);
});
//});
//db.close();
//console.log("Database Connection Closed....");
} catch (error) {
console.log(error);
return res.status(500).json(error);
}
};
const InsertData = async (body) => {
try {
await db.serialize(() => {
console.log("Database Connection open....");
db.run(
"CREATE TABLE IF NOT EXISTS Data_Buku(ID INTEGER PRIMARY KEY,judul_buku TEXT,tahun_terbit INTEGER)"
);
console.log(
`INSERT INTO Data_Buku(judul_buku,tahun_terbit) VALUES('${body.JudulBuku}',${body.TahunTerbit})`
);
db.run(
`INSERT INTO Data_Buku(judul_buku,tahun_terbit) VALUES('${body.JudulBuku}',${body.TahunTerbit})`,
(err, result) => {
if (err) throw err;
//console.log(result);
}
);
console.log("Data Successfully Inserted....");
});
//db.close();
console.log("Database Connection Closed....");
} catch (error) {
console.log(error);
return error;
}
};
module.exports = { SERIALIZE, InsertData, GetAllData };