-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
180 lines (151 loc) · 5.06 KB
/
server.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
176
177
178
179
180
const express = require("express");
const mysql = require("mysql2/promise");
const fs = require("fs").promises;
const app = express();
const port = 3000;
const cors = require("cors");
app.use(cors());
const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "Himanshu22@",
database: "flora",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
app.use(express.json());
// Your existing authentication route
app.post("/api/authenticate", async (req, res) => {
try {
const { username, password } = req.body;
// Fetch user from the database
const [user] = await pool.query(
"SELECT * FROM users WHERE username = ? AND password = ?",
[username, password]
);
if (user.length > 0) {
res.json({ success: true, message: "Login successful!" });
} else {
res.json({
success: false,
message: "Invalid credentials. Please try again.",
});
}
} catch (error) {
console.error("Error authenticating user:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/api/authenticate", async (req, res) => {
try {
const { username, password } = req.query;
// Fetch user from the database
const [user] = await pool.query(
"SELECT * FROM users WHERE username = ? AND password = ?",
[username, password]
);
if (user.length > 0) {
res.json({ success: true, message: "Login successful!" });
} else {
res.json({
success: false,
message: "Invalid credentials. Please try again.",
});
}
} catch (error) {
console.error("Error authenticating user:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Route to handle retrieving cart items from the products table
app.get("/api/getCartItems", async (req, res) => {
try {
// Fetch all cart items from the database
const [cartItems] = await pool.query("SELECT * FROM products");
res.status(200).json({ cartItems });
} catch (error) {
console.error("Error retrieving cart items from the database:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Route to handle inserting cart items into the products table
app.post("/api/insertCartItems", async (req, res) => {
try {
const { productNames, prices, quantities, finalPrice } = req.body;
// Iterate through the cart items and insert into the database
for (let i = 0; i < productNames.length; i++) {
const productName = productNames[i];
const price = prices[i];
const quantity = quantities[i];
const result = await pool.query(
"INSERT INTO products (product_name, price, quantity) VALUES (?, ?, ?)",
[productName, price, quantity, finalPrice]
);
console.log(
`Inserted cart item: ${productName}, Price: ${price}, Quantity: ${quantity}`
);
}
res.status(200).send("Cart items inserted into the database successfully");
} catch (error) {
console.error("Error inserting cart items into the database:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Route to handle GET requests for /api/insertCartItems (respond with 405 Method Not Allowed)
app.get("/api/insertCartItems", (req, res) => {
res
.status(405)
.send("GET method not allowed. Use POST method to insert cart items.");
});
app.get("/api/products", async (req, res) => {
try {
// Fetch products from the database
const [plants, tools, fertilizers] = await Promise.all([
fetchProducts("plants"),
fetchProducts("tools"),
fetchProducts("fertilizer"),
]);
res.json({ plants, tools, fertilizers });
} catch (error) {
console.error("Error fetching products:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Function to fetch products from the database
async function fetchProducts(tableName) {
const [rows] = await pool.query(`SELECT * FROM ${tableName}`);
return rows;
}
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
app.post("/api/register", async (req, res) => {
const { username, password, email } = req.body;
try {
// Check if the username or email already exists
const [existingUsers] = await pool.query(
"SELECT * FROM users WHERE username = ? OR email = ?",
[username, email]
);
if (existingUsers.length > 0) {
return res.json({
success: false,
message:
"Username or email already exists. Please choose a different one.",
});
}
// Add the new user to the database
const result = await pool.query(
"INSERT INTO users (username, password, email) VALUES (?, ?, ?)",
[username, password, email]
);
// Log the result if needed
console.log("User registered successfully. Result:", result);
// Send a success response
res.json({ success: true, message: "User registered successfully!" });
} catch (error) {
console.error("Error registering user:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});