-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
238 lines (141 loc) · 4.81 KB
/
Copy pathserver.js
File metadata and controls
238 lines (141 loc) · 4.81 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const express = require("express");
const mysql = require("mysql2");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.json());
// Connect to MySQL workbench
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: " ",
database: " "
});
// Check connection true or false
db.connect((err) => {
if (err) {
console.error("Database connection failed:", err);
return;
}
console.log("Connected to MySQL database.");
});
// ----------------- ROUTE FOR ADD BOOK -----------------
app.post("/add-book", (req, res) => {
const { title, author, isbn, publisher, year, category, quantity } = req.body;
const sql = ` INSERT INTO books2 (title, author, isbn, publisher, year, category, quantity)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE quantity = quantity + VALUES(quantity);
`;
db.query(sql, [title, author, isbn, publisher, year, category, quantity], (err, result) => {
if (err) {
console.error("Error adding book:", err);
return res.status(500).json({ message: "Database error" });
}
res.status(200).json({ message: "Book added successfully" });
});
});
// ----------------- NEW ROUTE FOR ISSUE BOOK -----------------
app.post("/issue-book", (req, res) => {
const { book_name, student_name, student_id, issue_date, return_date } = req.body;
const sql = `INSERT INTO issued_books2 (book_name, student_name, student_id, issue_date, return_date) VALUES (?, ?, ?, ?, ?)`;
db.query(sql, [book_name, student_name, student_id, issue_date, return_date], (err, result) => {
if (err) {
console.error("Error issuing book:", err);
return res.status(500).json({ message: "Database error" });
}
res.status(200).json({ message: "Book issued successfully" });
});
});
// for search books record
app.get('/searchbook', (req, res) => {
const searchTitle = req.query.title.toLowerCase();
if (!searchTitle) {
return res.json([]);
}
const sql = 'SELECT id, title, author, isbn, publisher, year, category, quantity FROM library2.books2 WHERE LOWER(title) LIKE ?';
db.query(sql, [`%${searchTitle}%`], (err, result) => {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Database error' });
}
res.json(result);
});
});
// for issued book list to see
app.get('/issued_books2', (req, res) => {
const sql = `
SELECT student_name AS studentName, book_name AS bookTitle, issue_date AS issueDate
FROM library2.issued_books2
`;
db.query(sql, (err, result) => {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Database error' });
}
res.json(result);
});
});
app.get('/test', (req, res) => {
res.send('Server is working!');
});
// Dashboard
app.get('/issued-books', (req, res) => {
const sql = `SELECT student_name, student_id, COUNT(*) AS total_books_issued FROM issued_books2 GROUP BY student_name, student_id`;
db.query(sql, (err, result) => {
if (err) throw err;
res.json(result);
});
});
// this is for return book
app.get('/returned-books', (req, res) => {
const sql = `SELECT
student_name,
student_id,
COUNT(*) AS total_books_returned
FROM returned_books
WHERE actual_return_date IS NOT NULL
GROUP BY student_name, student_id;
`;
db.query(sql, (err, result) => {
if (err) throw err;
res.json(result);
});
});
// this is for stock
app.get('/low-stock-books', (req, res) => {
const sql = `SELECT * FROM books2 WHERE quantity < 5`;
db.query(sql, (err, result) => {
if (err) throw err;
res.json(result);
});
});
// this is for return book form;
app.post('/return-book', (req, res) => {
const { studentId, studentName, bookTitle, returnDate } = req.body;
const insertSQL = `
INSERT INTO returned_books (student_id, student_name, book_name, actual_return_date)
VALUES (?, ?, ?, ?)
`;
db.query(insertSQL, [studentId, studentName, bookTitle, returnDate], (err, result) => {
if (err) {
console.error('Error inserting return record:', err);
return res.status(500).json({ error: 'Database error' });
}
res.status(200).json({ message: 'Book return recorded successfully' });
});
});
// this is for penalty page;
app.get('/penalties', (req, res) => {
const query = 'SELECT student_id, student_name, book_name, issue_date, return_date, penalty_amount FROM library2.penalties ORDER BY id DESC';
db.query(query, (err, results) => {
if (err) {
console.error('Error fetching penalties:', err);
return res.status(500).json({ error: err.message });
}
res.json(results); // this Send penalty data to frontend
});
});
// Start server
app.listen(5000, () => {
console.log("Server running on http://localhost:5000");
});