-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 3.73 KB
/
app.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
// app.js
const express = require('express');
const mysql = require('mysql');
const app = express();
const PORT = process.env.PORT || 3000; // Change 3001 to any available port number
// MySQL Connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'cakes'
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to database:', err);
return;
}
console.log('Connected to the database!');
});
// Middleware for JSON data parsing
app.use(express.json());
// Get all cakes
// Add these endpoints to your existing Node.js app
// Get all orders
app.get('/orders', (req, res) => {
connection.query('SELECT * FROM orders', (error, results) => {
if (error) {
console.error('Error querying the database:', error);
res.status(500).json({ error: 'Database error' });
} else {
res.json(results);
}
});
});
// Get a specific order by ID
app.get('/orders/:id', (req, res) => {
const orderId = req.params.id;
connection.query('SELECT * FROM orders WHERE id = ?', [orderId], (error, results) => {
if (error) {
console.error('Error querying the database:', error);
res.status(500).json({ error: 'Database error' });
} else {
if (results.length === 0) {
res.status(404).json({ error: 'Order not found' });
} else {
res.json(results[0]);
}
}
});
});
// Create a new order
app.post('/orders', (req, res) => {
const { customer_name, email, phone_number, food_item, delivery_address } = req.body;
connection.query('INSERT INTO orders (customer_name, email, phone_number, food_item, delivery_address) VALUES (?, ?, ?, ?, ?)',
[customer_name, email, phone_number, food_item, delivery_address],
(error, results) => {
if (error) {
console.error('Error inserting into the database:', error);
res.status(500).json({ error: 'Database error' });
} else {
res.status(201).json({ message: 'Order placed successfully', orderId: results.insertId });
}
}
);
});
// Update an order by ID
app.put('/orders/:id', (req, res) => {
const orderId = req.params.id;
const { customer_name, email, phone_number, food_item, delivery_address } = req.body;
connection.query('UPDATE orders SET customer_name = ?, email = ?, phone_number = ?, food_item = ?, delivery_address = ? WHERE id = ?',
[customer_name, email, phone_number, food_item, delivery_address, orderId],
(error, results) => {
if (error) {
console.error('Error updating the database:', error);
res.status(500).json({ error: 'Database error' });
} else {
if (results.affectedRows === 0) {
res.status(404).json({ error: 'Order not found' });
} else {
res.json({ message: 'Order updated successfully' });
}
}
}
);
});
// Delete an order by ID
app.delete('/orders/:id', (req, res) => {
const orderId = req.params.id;
connection.query('DELETE FROM orders WHERE id = ?', [orderId], (error, results) => {
if (error) {
console.error('Error deleting from the database:', error);
res.status(500).json({ error: 'Database error' });
} else {
if (results.affectedRows === 0) {
res.status(404).json({ error: 'Order not found' });
} else {
res.json({ message: 'Order deleted successfully' });
}
}
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});