Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
214 lines (187 loc) · 5.84 KB
/
Copy pathserver.js
File metadata and controls
214 lines (187 loc) · 5.84 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const express = require("express");
const cors = require("cors");
const multer = require("multer");
const Joi = require("joi");
const mongoose = require("mongoose");
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(cors());
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./public/images/");
},
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
mongoose
.connect("mongodb+srv://machine:Turbo@project.ob11xvh.mongodb.net/")
.then(() => {
console.log("connected to mongodb");
})
.catch((error) => {
console.log("couldn't connect to mongodb", error);
});
const houseSchema = new mongoose.Schema({
name:String,
description:String,
price:Number,
rating:Number,
main_image:String
});
const House = mongoose.model("House", houseSchema);
app.get("/",(req, res)=>{
res.sendFile(__dirname+"/index.html");
});
/*
let houses = [
{
"_id": "1",
"name": "Mower",
"description": "Useful for cutting the grass when it gets too tall.",
"price": "129.99",
"rating": "4.2",
"main_image": "mower.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "2",
"name": "Rake",
"description": "Useful for moving leaves into a pile.",
"price": "9.99",
"rating": "3.4",
"main_image": "rake.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "3",
"name": "Edger",
"description": "Useful for trimming up the edge of the grass.",
"price": "99.99",
"rating": "4.4",
"main_image": "edger.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "4",
"name": "Leaf Blower",
"description": "Useful for blowing leaves wherever you need to.",
"price": "38.99",
"rating": "3.3",
"main_image": "leafblower.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "5",
"name": "Spreader",
"description": "Useful for spreading pesticide and other chemicals over a wide area.",
"price": "49.99",
"rating": "2.7",
"main_image": "spreader.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
}
];
*/
app.get("/api/houses", async(req, res)=>{
const houses = await House.find();
console.log(houses);
res.send(houses);
});
app.post("/api/houses", upload.single("img"), async(req,res)=>{
const result = validateHouse(req.body);
if(result.error){
console.log("I have an error");
res.status(400).send(result.error.details[0].message);
return;
}
const house = new House({
name:req.body.name,
description:req.body.description,
price:req.body.price,
rating:req.body.rating,
});
if(req.file){
house.main_image = req.file.filename;
}
const newHouse = await house.save();
res.status(200).send(newHouse);
});
app.put("/api/houses/:id", upload.single("img"), async(req, res)=>{
const result = validateHouse(req.body);
if(result.error) {
res.status(400).send(result.error.details[0].message);
return;
}
const fieldsToUpdate = {
name:req.body.name,
description:req.body.description,
price:req.body.price,
rating:req.body.rating
}
if(req.file) {
fieldsToUpdate.main_image = req.file.filename;
}
const success = await House.updateOne({_id:req.params.id}, fieldsToUpdate);
const house = await House.findOne({_id:req.params.id});
res.status(200).send(house);
});
app.delete("/api/houses/:id", async(req, res)=>{
const house = await House.findByIdAndDelete(req.params.id);
res.status(200).send(house);
});
const validateHouse = (house) => {
const schema = Joi.object({
_id:Joi.allow(""),
name:Joi.string().min(3).required(),
description:Joi.string().min(3).required(),
price:Joi.number().required().min(0),
rating:Joi.number().required().min(0),
});
return schema.validate(house);
};
app.get("/api/plants", (req, res) =>{
const plants = [
{
"_id": "1",
"name": "Mulch",
"description": "Useful for keep the soil a good temperature and to prevent weeds from growing.",
"price": "3.99",
"rating": "4.2",
"main_image": "mulch.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "2",
"name": "Pine Straw",
"description": "Useful for fertilizing grass.",
"price": "4.99",
"rating": "3.4",
"main_image": "straw.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "3",
"name": "Pine Tree",
"description": "Tall, bushy tree. Commonly used as Christmas trees.",
"price": "32.99",
"rating": "4.4",
"main_image": "tree.png",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
},
{
"_id": "4",
"name": "Flowers",
"description": "Roses are our speciality, especially around this time.",
"price": "6.99",
"rating": "3.3",
"main_image": "flower.jpg",
"img2": "https://machine8822.github.io/project/part6/images/placeholder.jpg"
}
];
res.send(plants);
});
app.listen(3001, ()=>{
console.log("I'm listening");
});