forked from aarshiyavashisht/library-management-system-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (71 loc) · 2.37 KB
/
index.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
var express = require("express");
var app = express();
var cors = require("cors");
var connection = require("./database");
app.use(cors());
app.use(express.json());
// Get all books
app.get("/", function (req, res) {
let sql = "SELECT * FROM books";
connection.query(sql, function (err, results) {
res.send(results);
});
});
// Add a new book
app.post("/addBook", function (req, res) {
const formData = req.body;
// Construct the SQL query
let sql = "INSERT INTO books (title, author, isbn, quantity, genre) VALUES (?, ?, ?, ?, ?)";
// Execute the query
connection.query(sql, [formData.title, formData.author, formData.isbn, formData.quantity, formData.genre], function (err, result) {
if (err) {
console.error('Error executing SQL query:', err);
res.status(500).json({ success: false, error: 'Internal Server Error' });
} else {
console.log('Data inserted successfully');
res.status(200).json({ success: true });
}
});
});
app.delete("/deleteBook/:id", function (req, res) {
const bookId = req.params.id;
// Construct the SQL query
const sql = "DELETE FROM books WHERE book_id = ?";
// Execute the query
connection.query(sql, [bookId], function (err, result) {
if (err) {
console.error('Error executing SQL query:', err);
res.status(500).json({ success: false, error: 'Internal Server Error' });
} else {
console.log('Book deleted successfully');
res.status(200).json({ success: true });
}
});
});
app.put("/updateBook/:id", function (req, res) {
const bookId = req.params.id;
const formData = req.body;
// Construct the SQL query
const sql = "UPDATE books SET title=?, author=?, isbn=?, quantity=?, genre=? WHERE book_id=?";
// Execute the query
connection.query(
sql,
[formData.title, formData.author, formData.isbn, formData.quantity, formData.genre, bookId],
function (err, result) {
if (err) {
console.error('Error executing SQL query:', err);
res.status(500).json({ success: false, error: 'Internal Server Error' });
} else {
console.log('Book updated successfully');
res.status(200).json({ success: true });
}
}
);
});
app.listen(4000, function () {
console.log("App Listening on port 4000");
connection.connect(function (err) {
if (err) throw err;
console.log("Database Connected");
});
});