-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello3.js
More file actions
150 lines (121 loc) · 4.07 KB
/
hello3.js
File metadata and controls
150 lines (121 loc) · 4.07 KB
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
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs')
const express = require("express");
// const db = new sqlite3.Database(':memory:');
const dbfile = __dirname+'\\msg.dbl';
const db = new sqlite3.Database(dbfile);
const sqlSelect = "SELECT id, owner,topic,msg,status FROM msgtab";
const sqlUpdate = "UPDATE msgtab SET owner=?,topic=?,msg=?,status=\"_new\" WHERE id=?";
const sqlDel = "DELETE FROM msgtab WHERE id IN ";
const sqlInsert = "INSERT INTO msgtab(owner,topic,msg,status) VALUES (?,?,?,?)";
const sqlCreateTable = "CREATE TABLE msgtab (id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT,topic TEXT,msg TEXT,status TEXT)";
let initDb=function()
{
if (!fs.existsSync(dbfile))
{
db.run(sqlCreateTable);
// let stmt = db.prepare(sqlInsert);
// for (let i = 0; i < 3; i++)
// newMessage({owner: "vlad", topic: "msg", msg: "payMe"}, stmt)
// stmt.finalize();
}
}
let newMessage=function(msg,stmt)
{
// const stmt = db.prepare("INSERT INTO msgtab(owner,topic,msg,status) VALUES (?,?,?,?)");{
let res = stmt.run(msg.owner, msg.topic, msg.msg, "_new"
,
function (err) {
if (null == err) {
// row inserted successfully'
msg.id = this.lastID;
console.log("was add with id:"+this.lastID);
} else {
//Oops something went wrong
console.log(err);
}
}
);
// console.log("1:" + msg);
}
db.serialize(() => {
initDb();
});
const app = express();
const port = 3001;
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// app.use(express.static('C:/PapaWK/Programm/apache-tomcat-9.0.70/webapps/'));
app.use(express.static(__dirname));
// console.log(__dirname)
app.listen(port, () => {
console.log("Application started and Listening on port:" + port);
});
app.use(express.static(__dirname));
// const data = fs.readFileSync(__dirname + "/answer.html", 'utf8');
app.get("/api/getmsgs", (req, res) => {
db.serialize(() => {
db.all(sqlSelect, (err, rows) => {
res.send(rows)
});
});
});
app.post("/api/getmsgs", (req, res) => {
db.serialize(() => {
db.all(sqlSelect, (err, rows) => {
res.send(rows)
});
});
});
app.post("/api/sendmsg", (req, res) => {
db.serialize(() => {
try {
let stmt = db.prepare(sqlInsert);
let msg = req.body;
if (msg && msg.owner)
newMessage(msg, stmt)
stmt.finalize();
db.all(sqlSelect, (err, rows) => {
res.send(rows)
});
} catch (e) {
console.log(e);
}
});
});
app.post("/api/updmsgs", (req, res) => {
db.serialize(() => {
try {
let msgs = req.body;
let stmt = db.prepare(sqlUpdate);
for (let msg of msgs)
stmt.run(msg.owner, msg.topic, msg.msg, msg.id)
stmt.finalize();
db.all(sqlSelect, (err, rows) => {
res.send(rows)
});
} catch (e) {
console.log(e);
}
});
});
app.post("/api/delmsgs", (req, res) => {
db.serialize(() => {
try {
let msgs = req.body;
if (msgs && msgs.length>0)
{
let ixes=msgs.reduce((res,el)=>{
return res.length > 0 ? (res + ',' + el.id) : (res + el.id);
},'');
db.run(sqlDel+'('+ixes+')');
}
db.all(sqlSelect, (err, rows) => {
res.send(rows)
});
} catch (e) {
console.log(e);
}
});
});
// db.close();