Skip to content

Commit 64ab82d

Browse files
authored
Merge pull request #79 from tauanesales/release/0.15.0
Release/0.15.0
2 parents 693b6f7 + 85d5dc2 commit 64ab82d

8 files changed

Lines changed: 399 additions & 3 deletions

File tree

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import userRoute from "./src/routes/user.route.js";
44
import adminRoute from "./src/routes/admin.route.js";
55
import rootRoute from "./src/routes/root.route.js";
66
import authRoute from "./src/routes/auth.route.js";
7+
import touristAttractionRoute from "./src/routes/touristAttraction.route.js";
78
import cors from 'cors';
89
import swaggerRoute from "./src/routes/swagger/swagger.route.js"
910
const app = express();
@@ -32,6 +33,7 @@ app.use(express.json());
3233
app.use("/admin", adminRoute);
3334
app.use("/user", userRoute);
3435
app.use("/auth", authRoute);
36+
app.use("/touristAttraction", touristAttractionRoute);
3537
app.use("/", rootRoute);
3638
app.use("/docs", swaggerRoute);
3739

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "back-matc84-laboratorio-de-programacao-web",
3-
"version": "0.14.0",
3+
"version": "0.15.0",
44
"type": "module",
55
"description": "Projeto MATC84",
66
"main": "app.js",
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import touristAttractionService from "../services/touristAttraction.service.js";
2+
3+
const getAttractions = async (req, res) => {
4+
try {
5+
let attractions = await touristAttractionService.findAllService();
6+
if (!attractions) attractions = []
7+
//TODO: colocar as avaliações
8+
9+
res.status(200).json(attractions);
10+
} catch (error) {
11+
return res.status(500).json({
12+
error: error.message,
13+
});
14+
}
15+
}
16+
17+
const addAttraction = async (req, res) => {
18+
try {
19+
20+
const requiredFields = [
21+
"name",
22+
"address",
23+
"openingHours",
24+
"typeOfAttraction",
25+
"description"
26+
]
27+
28+
for (const field of requiredFields) {
29+
if (!req.body[field]) {
30+
return res.status(400).json({ error: `Please add the field ${field}` });
31+
}
32+
}
33+
34+
const {
35+
name,
36+
address,
37+
openingHours,
38+
typeOfAttraction,
39+
description
40+
} = req.body
41+
42+
//TODO: testar se o usuário é admin
43+
44+
const attraction = await touristAttractionService.createService({
45+
name,
46+
address,
47+
openingHours,
48+
typeOfAttraction,
49+
description
50+
})
51+
52+
res.status(201).json({
53+
message: "Tourist Attraction registered successfully",
54+
id: attraction._id
55+
})
56+
57+
} catch (error) {
58+
return res.status(500).json({
59+
error: error.message,
60+
});
61+
}
62+
}
63+
64+
const updateAttraction = async (req, res) => {
65+
try {
66+
67+
const id = req.params.id;
68+
69+
if (!id) {
70+
return res.status(400).json({ error: `Please add the attraction id as a request param` });
71+
}
72+
73+
74+
const requiredFields = [
75+
"name",
76+
"address",
77+
"openingHours",
78+
"typeOfAttraction",
79+
"description"
80+
]
81+
82+
for (const field of requiredFields) {
83+
if (!req.body[field]) {
84+
return res.status(400).json({ error: `Please add the field ${field}` });
85+
}
86+
}
87+
88+
const {
89+
name,
90+
address,
91+
openingHours,
92+
typeOfAttraction,
93+
description
94+
} = req.body
95+
96+
//TODO: criar middleware que pega o id do usuário e testa se é admin
97+
98+
const result = await touristAttractionService.updateService(id, name, address, openingHours, typeOfAttraction, description)
99+
100+
if (!result) {
101+
res.status(404).json({message: "Tourist Attraction not found"})
102+
} else {
103+
res.status(204).json()
104+
}
105+
106+
} catch (error) {
107+
108+
return res.status(500).json({
109+
error: error.message,
110+
});
111+
112+
}
113+
}
114+
115+
const deleteAttraction = async (req, res) => {
116+
try {
117+
118+
const id = req.params.id;
119+
120+
if (!id) {
121+
return res.status(400).json({ message: `Please add the attraction id as a request param` });
122+
}
123+
124+
const result = await touristAttractionService.deleteService(id)
125+
126+
if (!result) {
127+
res.status(404).json({error: "Tourist Attraction not found"})
128+
} else {
129+
res.status(204).json()
130+
}
131+
132+
} catch (error) {
133+
134+
return res.status(500).json({
135+
error: error.message,
136+
});
137+
138+
}
139+
}
140+
141+
export default {
142+
getAttractions,
143+
addAttraction,
144+
deleteAttraction,
145+
updateAttraction
146+
}

src/models/TouristAttraction.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import mongoose from 'mongoose';
2+
3+
const TouristAttractionSchema = new mongoose.Schema({
4+
name: {
5+
type: String,
6+
required: true
7+
},
8+
address: {
9+
type: String,
10+
required: true
11+
},
12+
openingHours: {
13+
type: String,
14+
required: true
15+
},
16+
typeOfAttraction: {
17+
type: String,
18+
required: true
19+
},
20+
description: {
21+
type: String,
22+
required: true
23+
}
24+
})
25+
26+
const TouristAttraction = mongoose.model("TouristAttraction", TouristAttractionSchema);
27+
export default TouristAttraction;

0 commit comments

Comments
 (0)