-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (121 loc) · 3.49 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
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
const express = require("express");
const cors = require("cors");
require("./db/config");
const User = require("./db/User");
const Product = require("./db/Product");
const app = express();
app.use(express.json());
app.use(cors());
app.post("/register", async (req, res) => {
try {
const user = new User(req.body);
const result = await user.save();
res.send(result);
} catch (error) {
res
.status(500)
.json({ error: "An error occurred while registering the user." });
}
});
app.post("/login", async (req, res) => {
if (req.body.password && req.body.email) {
try {
let user = await User.findOne({ email: req.body.email }).select(
"-password"
);
if (user) {
res.send(user);
} else {
res.status(404).send({ result: "No User Found" });
}
} catch (error) {
console.error("Error:", error);
res.status(500).send({ result: "Internal Server Error" });
}
} else {
res.status(400).send({ result: "Bad Request" });
}
});
app.post("/add-product", async (req, res) => {
let product = new Product(req.body);
let result = await product.save();
res.send(result);
});
app.get("/products", async (req, res) => {
try {
let products = await Product.find();
if (products.length > 0) {
res.send(products);
} else {
res.status(404).send({ message: "No Products Found." });
}
} catch (error) {
res.status(500).send({ message: "Error fetching products." });
}
});
app.delete("/product/:id", async (req, res) => {
try {
const result = await Product.deleteOne({ _id: req.params.id });
if (result.deletedCount === 1) {
res.send({ message: "Product deleted successfully" });
} else {
res.status(404).send({ message: "Product not found" });
}
} catch (error) {
console.error("Error deleting product:", error);
res.status(500).send({ message: "Internal server error" });
}
});
app.get("/product/:id", async (req, res) => {
try {
let result = await Product.findOne({ _id: req.params.id });
if (result) {
res.send(result);
} else {
res.send({ result: "No result found." });
}
} catch (error) {
console.error("Error finding product", error);
res.status(500).send({ message: "Internal server error" });
}
});
app.put("/product/:id", async (req, res) => {
try {
const result = await Product.updateOne(
{ _id: req.params.id },
{ $set: req.body }
);
res.send(result);
} catch (error) {
res.status(500).send("Error updating product");
}
});
// app.get("/search/:key", async (req, res) => {
// let result = await Product.findOne({
// $or: [
// {
// name: { $regex: req.params.key },
// company: { $regex: req.params.key },
// category: { $regex: req.params.key },
// },
// ],
// });
// });
app.get("/search/:key", async (req, res) => {
try {
const result = await Product.find({
$or: [
{
name: { $regex: req.params.key, $options: "i" }, // Case-insensitive search
company: { $regex: req.params.key, $options: "i" }, // Case-insensitive search
category: { $regex: req.params.key, $options: "i" }, // Case-insensitive search
},
],
});
res.json(result); // Send the search results as JSON
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" }); // Handle errors appropriately
}
});
app.listen(5000, () => console.log("App is running on port 5000"));