-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
175 lines (158 loc) · 4.34 KB
/
db.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import mysql from "mysql2";
import express from 'express';
const app = express();
const port = 3000;
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:5173');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(express.json());
const dbConfig = {
host: 'localhost',
user: 'root',
password: 'root',
database: 'library',
}
const connection = mysql.createConnection(dbConfig);
const pool = mysql.createPool(dbConfig);
connection.connect((err) => {
if (err) {
console.error('Error connecting to SQL:', err);
return;
}
console.log('Connected to SQL');
});
app.get('/', (req, res) => {
return res.json("welcome to database of library");
})
const endPoints = [
{
endPoint: '/addresses',
table: 'Address'
},
{
endPoint: '/books',
table: 'Book'
},
{
endPoint: '/authors',
table: 'Author'
},
{
endPoint: '/members',
table: 'Member'
},
{
endPoint: '/staff',
table: 'Staff'
},
{
endPoint: '/borrow',
table: 'Borrow'
}
]
const sortEndPoints = [
{
endPoint: '/booksS',
table: 'Book',
sortItem: "BookName",
sortType : "ASC"
},
{
endPoint: '/booksN',
table: 'Book',
sortItem: "BookName",
sortType : "DESC"
},
{
endPoint: '/authorsS',
table: 'Author',
sortItem: "FirstName",
sortType : "ASC"
},
{
endPoint: '/authorsN',
table: 'Author',
sortItem: "FirstName",
sortType : "DESC"
},
{
endPoint: '/membersS',
table: 'Member',
sortItem: "FirstName",
sortType : "ASC"
},
{
endPoint: '/membersN',
table: 'Member',
sortItem: "FirstName",
sortType : "DESC"
},
]
for (let i = 0; i < sortEndPoints.length; i++) {
app.get(sortEndPoints[i].endPoint, (req, res) => {
pool.query(`SELECT * FROM ${sortEndPoints[i].table} ORDER BY ${sortEndPoints[i].sortItem} ${sortEndPoints[i].sortType}`, (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error connecting to the database' });
}
res.json(results);
});
});
}
for (let i = 0; i < endPoints.length; i++) {
app.get(endPoints[i].endPoint, (req, res) => {
pool.query(`SELECT * FROM ${endPoints[i].table}`, (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error connecting to the database' });
}
res.json(results);
});
});
}
const query = `select Borrow.Id, Member.FirstName, Member.LastName, Member.NationalCode,
Book.BookName, Borrow.BorrowDate, Borrow.ReturnDate from
((Borrow
INNER JOIN Member ON Borrow.BorrowerName = Member.NationalCode)
INNER JOIN Book ON Borrow.BookName = Book.Isbn)`;
app.get('/listOfLoans', (req, res) => {
pool.query(query, (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error connecting to the database' });
}
res.json(results);
});
});
app.post('/borrow', (req, res) => {
const b = req.body;
const query = 'INSERT INTO Borrow (Id, BorrowerName, BookName, BorrowDate, ReturnDate) VALUES (?,?,?,?,?)';
pool.query(query, [b.id, b.borrowerName, b.bookName, b.borrowDate, b.returnDate], (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error connecting to the database' });
}
res.json(results);
});
})
app.post('/members', (req, res) => {
const m = req.body;
const query = 'INSERT INTO Member (FirstName, LastName, DateOfBirth, NationalCode, Education, Address, PhoneNumber, MemberShipDate) VALUES (?,?,?,?,?,?,?,?)';
pool.query(query, [m.firstName, m.lastName, m.dateOfBirth, m.nationalCode, m.education, m.address, m.phoneNumber, m.memberShipDate], (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error connecting to the database' });
}
res.json(results);
});
})
app.delete('/members/:NationalCode', (req, res) => {
const itemIdToDelete = req.params.NationalCode;
pool.query('DELETE FROM Member WHERE NationalCode = ?', [itemIdToDelete], (error, results) => {
if (error) {
return res.status(500).json({ error: 'Error deleting item' });
}
res.json({ message: `Item with ID ${itemIdToDelete} deleted successfully` });
});
})
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});