-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
155 lines (136 loc) · 4.5 KB
/
admin.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
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const bodyParser = require('body-parser');
const favicon = require('serve-favicon');
const sqlite3 = require('sqlite3');
app = express();
const port = 8081;
app.set('views',path.join(__dirname,"views"));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname,'public','favicon.ico')));
app.use('/css', express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use( morgan('dev') );
const db = new sqlite3.Database('./libri.db',()=>{
app.listen(port);
console.log(`Server running on http://localhost:${port}`);
console.log('database open')
});
app.get('/',(req,res)=>{
let sql = "SELECT * FROM Autori";
db.all(sql,(err,rows)=>{
autori=rows;
sql = "SELECT * FROM Libri";
db.all(sql,(err,rows)=>{
libri=rows;
sql = "SELECT * FROM autori_libri";
db.all(sql,(err,rows)=>{
relazioni=rows
res.render('admin',{autori,libri,relazioni});
});
});
});
});
app.post('/creadb',(req,res)=>{
sql='drop table if exists autori';
db.run(sql);
sql='drop table if exists libri';
db.run(sql);
sql='drop table if exists autori_libri';
db.run(sql);
sql='create table autori (id integer primary key,nome text,cognome text not null)';
db.run(sql,err=>{
sql='create table libri (id integer primary key,titolo text not null)';
db.run(sql,err=>{
sql='create table autori_libri (id integer primary key,id_autore integer not null,id_libro integer not null)';
db.run(sql,err=>{
if (err) res.sendFile(path.join(__dirname,'public','error.html'));
});
});
});
res.redirect("/");
});
app.get('/modifica/autore/:id',(req,res)=>{
sql=`select * from Autori where id = ${req.params.id}`;
db.each(sql,(err,row)=>{
res.render('modAutori',{autore:row});
});
});
app.post('/modautore',(req,res)=>{
const id=parseInt(req.body.id);
sql=`UPDATE Autori SET nome='${req.body.nome}',cognome='${req.body.cognome}' WHERE Autori.id = ${id}`;
db.run(sql);
res.redirect("/");
});
app.post('/delautore',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `DELETE FROM Autori WHERE Autori.id=${id}`;
db.run(sql);
res.redirect('/');
});
app.post('/addautore',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `INSERT INTO Autori(nome,cognome) VALUES('${req.body.nome}','${req.body.cognome}')`;
db.run(sql);
res.redirect('/');
});
app.get('/modifica/libro/:id',(req,res)=>{
sql=`select * from Libri where id = ${req.params.id}`;
db.each(sql,(err,row)=>{
res.render('modLibri',{libro:row});
});
});
app.post('/modlibro',(req,res)=>{
const id=parseInt(req.body.id);
sql=`UPDATE Libri SET titolo='${req.body.titolo}' WHERE Libri.id = ${id}`;
db.run(sql);
res.redirect("/");
});
app.post('/dellibro',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `DELETE FROM Libri WHERE Libri.id=${id}`;
db.run(sql);
res.redirect('/');
});
app.post('/addlibro',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `INSERT INTO Libri(titolo) VALUES('${req.body.titolo}')`;
db.run(sql);
res.redirect('/');
});
app.get('/modifica/relazione/:id',(req,res)=>{
sql=`select * from autori_libri where id = ${req.params.id}`;
db.each(sql,(err,row)=>{
relazione=row
db.all("select * from Autori",(err,rows)=>{
autori=rows
db.all("select * from libri",(err,rows)=>{
libri=rows
res.render('modRelazione',{relazione,autori,libri});
});
});
});
});
app.post('/modrelazione',(req,res)=>{
const id=parseInt(req.body.id);
sql=`UPDATE autori_libri SET id_autore='${req.body.id_autore}',id_libro='${req.body.id_libro}' WHERE autori_libri.id = ${id}`;
db.run(sql);
res.redirect("/");
});
app.post('/delrelazione',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `DELETE FROM autori_libri WHERE autori_libri.id=${id}`;
db.run(sql);;
res.redirect('/');
});
app.post('/addrelazione',(req,res)=>{
const id=parseInt(req.body.id);
let sql = `insert into autori_libri (id_autore, id_libro) values ('${req.body.id_autore}', '${req.body.id_libro}');`;
db.run(sql);
res.redirect('/');
});
app.use((req,res)=>{
res.status(404);
res.sendFile(path.join(__dirname,'public','404.html'));
});