-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cjs
154 lines (147 loc) · 4.07 KB
/
server.cjs
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
const express = require("express");
const mongoose = require("mongoose");
const Purchase = require("./models/purchaseModel.cjs");
const Coffee = require("./models/coffeeModel.cjs");
const Credential = require("./models/credentialModel.cjs");
const AdminCredential = require("./models/adminCredentialModel.cjs");
const app = express();
const cors = require("cors");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
//routes
//test
app.get("/", (req, res) => {
res.send("Hello");
});
app.get("/blog", (req, res) => {
res.send("Hello");
});
//coffee collection
app.get("/coffee", async (req, res) => {
try {
const coffee = await Coffee.find({});
res.status(200).json(coffee);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/coffee", async (req, res) => {
try {
const coffee = await Coffee.create(req.body);
res.status(200).json(coffee);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
// update coffee
app.get("/coffee/:id", async (req, res) => {
try {
const { id } = req.params;
const coffee = await Coffee.findById(id);
res.status(200).json(coffee);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.put("/coffee/:id", async (req, res) => {
try {
const { id } = req.params;
const coffee = await Coffee.findByIdAndUpdate(id, req.body);
// we cannot find any product in database
if (!coffee) {
return res
.status(404)
.json({ message: `cannot find any product with ID ${id}` });
}
const updatedCoffee = await Product.findById(id);
res.status(200).json(updatedCoffee);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// delete coffee
app.delete("/products/:id", async (req, res) => {
try {
const { id } = req.params;
const product = await Purchase.findByIdAndDelete(id);
if (!product) {
return res
.status(404)
.json({ message: `cannot find any product with ID ${id}` });
}
res.status(200).json(product);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
//user credential collection
app.get("/credential", async (req, res) => {
try {
const credential = await Credential.find({});
res.status(200).json(credential);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/credential", async (req, res) => {
try {
const credential = await Credential.create(req.body);
res.status(200).json(credential);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
//admin credential collection
app.get("/adminCredential", async (req, res) => {
try {
const credential = await AdminCredential.find({});
res.status(200).json(credential);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/adminCredential", async (req, res) => {
try {
const credential = await AdminCredential.create(req.body);
res.status(200).json(credential);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
//purchase collection
app.get("/purchase", async (req, res) => {
try {
const products = await Purchase.find({});
res.status(200).json(products);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/purchase", async (req, res) => {
try {
const purchase = await Purchase.create(req.body);
res.status(200).json(purchase);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
//connect to mongodb
mongoose.set("strictQuery", false);
mongoose
.connect(
"mongodb+srv://<username>:<password>@<cluster>/<shop>?retryWrites=true&w=majority"
)
.then(() => {
console.log("connected to MongoDB");
app.listen(3000, () => {
console.log(`API is running on port 3000`);
});
})
.catch((error) => {
console.log(error);
});