-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
128 lines (118 loc) · 3.45 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
119
120
121
122
123
124
125
126
127
128
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const Product = require("./models/product");
require("dotenv/config");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(swaggerDocument, { explorer: true })
);
app.use(bodyParser.json());
const api = process.env.API_URL;
mongoose
.connect(process.env.CONNECTION_STRING, {
dbName: "eshop-database",
})
.then(() => {
console.log("Database Connection is ready...");
})
.catch((err) => {
console.log(err);
});
const server = app.listen(3000, () =>
console.log("server is running on the 3000 port")
);
// Create a new product
app.post(`${api}/products`, async (req, res) => {
try {
console.log("entered here");
const { name, description, price, variants } = req.body;
console.log(req.body);
const product = await Product.create({
name,
description,
price,
variants,
});
console.log("2");
res.status(201).json(product);
//console.log(req.body);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
app.get(`${api}/products/search`, async (req, res) => {
try {
const { query } = req.query;
// Search products by name, description, or variant name using a regular expression
const products = await Product.find({
$or: [
{ name: { $regex: query, $options: "i" } }, // Case-insensitive search for product name
{ description: { $regex: query, $options: "i" } }, // Case-insensitive search for description
{ "variants.name": { $regex: query, $options: "i" } }, // Case-insensitive search for variant name
],
});
res.json(products);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Get all products
app.get(`${api}/products`, async (req, res) => {
try {
const products = await Product.find();
res.json(products);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Get a single product by ID
app.get(`${api}/products/:id`, async (req, res) => {
try {
const product = await Product.findById(req.params.id);
if (!product) {
return res.status(404).json({ message: "Product not found" });
}
res.json(product);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Update a product by ID
app.put(`${api}/products/:id`, async (req, res) => {
try {
const { name, description, price, variants } = req.body;
const updatedProduct = await Product.findByIdAndUpdate(
req.params.id,
{ name, description, price, variants },
{ new: true }
);
if (!updatedProduct) {
return res.status(404).json({ message: "Product not found" });
}
res.json(updatedProduct);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Delete a product by ID
app.delete(`${api}/products/:id`, async (req, res) => {
try {
const deletedProduct = await Product.findByIdAndDelete(req.params.id);
if (!deletedProduct) {
return res.status(404).json({ message: "Product not found" });
}
res.json({ message: "Product deleted successfully" });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// afterAll(() => {
// server.close();
// });
module.exports = app;