-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.js
126 lines (119 loc) · 3.89 KB
/
book.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
require('dotenv').config()
const mysql = require('mysql2');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: process.env.pw ,
database: "books"
});
let upid;
function showbook(req,res){
con.query(
"select * from book",
(err,result,field)=>{
res.render('result', {
process: 'sb',
result: 'Book in directory',
helo: result
});
}
);
}
function getbook(req,res){
con.query(
`select * from book where id = ${req.query["id"]}`,
(err,result,field)=>{
(result.length==0) ? res.render('result',{
result: `No book found with the entered book id'${req.query["id"]}'`,
}) : res.render('result', {
helo: result,
process: 'gb',
result: `Book details of the entered id '${req.query["id"]}'`
})
}
);
}
function addbook(req,res){
let id = req.body.id;
let name = req.body.name;
let author = req.body.author;
let year = req.body.year;
con.query(
"select max(id) as maxid from book",
(err,result,field)=>{
let len = result[0].maxid;
con.query(
"select * from book",
(err,result,field)=>{
for(let i=0; i < len; i++){
if(result[i] == undefined) continue
if(id == result[i].id){
return res.send(`intha id '${id}' la already "${result[i].name}" nu oru book iruku friend`);
}
if(name == result[i].name) {
return res.send(`"${name}" book already intha id '${result[i].id}' la iruku friend `);
}
}
con.query(
`insert into book (id, name, author, year) values (${Number(id)},"${name}", "${author}", ${Number(year)})`,
(err,result,field)=>{
res.render('result', {
result: `Book '${name}' is added to the directory'`,
process: 'audb'
});
}
);
}
);
}
);
}
function updatebook(req,res){
let id = req.query["id"];
upid=id;
con.query(
`select * from book where id = ${id}`,
(err,result,field)=>{
(result.length==0) ? res.render('result',{
result:`No book found with the id '${id}'`
}) : res.sendFile(__dirname + "/" + "updatebook.html");
}
);
}
function updateboook(req,res){
let {name, author, year} = req.query;
con.query(
`update book set name = "${name}", author = "${author}", year = ${year} where id = ${upid}`,
(err,result,field)=>{
res.render('result', {
id: upid,
result: `Book with id '${upid}' updated`,
process: 'audb'
});
}
);
}
function deletebook(req,res){
let id = req.body.id;
con.query(
`select * from book where id = ${id}`,
(err,result,field)=>{
if(result.length==0){
res.json(`Can't delete, No book found in the id '${id}' `);
}
else{
let name = result[0].name;
con.query(
`delete from book where id = ${id}`,
(err,result,field)=>{
res.render('result', {
result: `Book '${name}' deleted from the directory`,
process: 'audb'
});
}
);
}
}
);
}
module.exports = {showbook, getbook, addbook, updatebook, updateboook, deletebook}