-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (129 loc) · 3.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Import required modules
import express from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser";
// Create an Express app
const app = express();
const port = 3000;
// Connect to MongoDB database
mongoose.connect("mongodb://localhost:27017/sample", {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log("Database connected");
}).catch((err) => {
console.log(err);
});
// Middleware setup
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
// Define the product schema
const schema = new mongoose.Schema({
name: String,
description: String,
price: Number
});
// Create a model based on the schema
const Product = new mongoose.model("product", schema);
// Route to create a new product
app.post("/api/v1/product/new", async (req, res) => {
try {
// Create a new product using the request body
const product = await Product.create(req.body);
// Respond with a success message and the created product
res.status(200).json({
success: true,
product
});
} catch (error) {
console.error(error);
// Handle errors and respond with an error message
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
});
// Route to get all products
app.get("/api/v1/products", async (req, res) => {
try {
// Retrieve all products from the database
const products = await Product.find();
// Respond with a success message and the list of products
res.status(200).json({
success: true,
products
});
} catch (error) {
console.error(error);
// Handle errors and respond with an error message
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
});
// Route to update a product by ID
app.put("/api/v1/product/:id", async (req, res) => {
try {
// Find the product by ID
let product = await Product.findById(req.params.id);
// Check if the product exists
if (!product) {
return res.status(404).json({
success: false,
message: "Product not found"
});
}
// Update the product and return the updated document
product = await Product.findByIdAndUpdate(req.params.id, req.body, {
new: true,
runValidators: true,
useFindAndModify: false
});
// Respond with a success message and the updated product
res.status(200).json({
success: true,
product
});
} catch (error) {
console.error(error);
// Handle errors and respond with an error message
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
});
// Route to delete a product by ID
app.delete("/api/v1/product/:id", async (req, res) => {
try {
// Find the product by ID
const product = await Product.findById(req.params.id);
// Check if the product exists
if (!product) {
return res.status(404).json({
success: false,
message: "Product not found"
});
}
// Delete the product by ID
await Product.deleteOne({ _id: req.params.id });
// Respond with a success message
res.status(200).json({
success: true,
message: "Product is deleted"
});
} catch (error) {
console.error(error);
// Handle errors and respond with an error message
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});