-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtourismgovernerController.js
220 lines (192 loc) · 5.26 KB
/
tourismgovernerController.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
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
214
215
216
217
218
219
220
const TourismGoverner = require("../Models/TourismGoverner"); // Adjust the path as needed
const Tag = require("../Models/HistoricalTags.js"); // Adjust the path as needed
const Historical = require("../Models/Historical.js");
const { default: mongoose } = require("mongoose");
const loginTourism = async (req, res) => {
const { user, password } = req.body;
try {
// Find the guest by username, password, and role
const Tourism = await TourismGoverner.findOne({ username: user, password });
// If guest not found or password does not match, return error
if (!Tourism) {
return res.status(404).json({ message: "Regiesterd First" });
}
res.status(200).json({
message: `Welcome ${user}`,
Tourism,
});
} catch (error) {
res.status(500).json({ message: "Error during authentication.", error });
}
};
const getTourismbyid = async (req, res) => {
const { id } = req.params;
try {
const tourism = await TourismGoverner.findById(id);
if (!tourism) {
return res.status(404).json({ message: "Tourism Governer not found." });
}
await tourism.save();
res.status(200).json({ username: tourism.username });
} catch (error) {
res
.status(400)
.json({ message: "Error retrieving Tourism Governer profile.", error });
}
};
const viewAllPlaces = async (req, res) => {
try {
const places = await Historical.find();
res.status(200).json(places);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const createPlaces = async (req, res) => {
const {
Name,
Description,
location,
Pictures,
opening_hours,
tags,
TicketPricesF,
TicketPricesN,
TicketPricesS,
managed_by,
} = req.body;
try {
const Place = await Historical.create({
Name,
Description,
location,
Pictures,
opening_hours,
tags,
TicketPricesF,
TicketPricesN,
TicketPricesS,
managed_by,
});
res.status(200).json(Place);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const getPlaces = async (req, res) => {
try {
const places = await Historical.find();
res.status(200).json(places);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const AllTags = async (req, res) => {
try {
const tags = await Tag.find();
res.status(200).json(tags);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const updatePlace = async (req, res) => {
const { id } = req.params;
const {
Name,
Description,
location,
Pictures,
opening_hours,
tags,
TicketPricesF,
TicketPricesN,
TicketPricesS,
managed_by,
} = req.body;
try {
const existingPlace = await Historical.findById(id);
if (!existingPlace) {
return res.status(404).json({ error: "Place not found" });
}
const updatedPlace = await Historical.updateOne(
{ _id: new mongoose.Types.ObjectId(id) },
{
Name,
Description,
location,
Pictures,
opening_hours,
tags,
TicketPricesF,
TicketPricesN,
TicketPricesS,
managed_by,
},
{ new: true }
);
res.status(200).json(updatedPlace);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const deletePlace = async (req, res) => {
const { id } = req.params;
try {
const deletedPlace = await Historical.findByIdAndDelete(id);
if (!deletedPlace) {
return res.status(404).json({ error: "Place not found" });
}
res.status(200).json({ message: "Place deleted successfully" });
} catch (error) {
res.status(400).json({ error: error.message });
}
};
const createTag = async (req, res) => {
const { name, type, period } = req.body;
try {
// Check if the tag already exists
const existingTag = await Tag.findOne({ name });
if (existingTag) {
return res.status(400).json({ error: "Tag already exists" });
}
// Create a new tag with the new fields
const newTag = new Tag({ name, type, period });
const savedTag = await newTag.save();
// Send response with the created tag
res.status(201).json(savedTag);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
const changePasswordTourismGoverner = async (req, res) => {
const { oldPassword, newPassword } = req.body;
const tourismgovernerId = req.params.id;
try {
const tourismgoverner = await TourismGoverner.findById(tourismgovernerId);
if (!tourismgoverner) {
return res.status(404).json({ message: "Tourismgoverner not found" });
}
// Skip password hashing, compare directly
if (tourismgoverner.password !== oldPassword) {
return res.status(400).json({ message: "Incorrect old password" });
}
// Directly assign the new password (plain-text)
tourismgoverner.password = newPassword;
await tourismgoverner.save();
res.status(200).json({ message: "Password updated successfully" });
} catch (error) {
res.status(500).json({ message: "Error updating password", error });
}
};
module.exports = {
viewAllPlaces,
createPlaces,
getPlaces,
AllTags,
updatePlace,
deletePlace,
createTag,
changePasswordTourismGoverner,
loginTourism,
getTourismbyid,
};