forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceController.js
More file actions
178 lines (159 loc) · 4.94 KB
/
Copy pathspaceController.js
File metadata and controls
178 lines (159 loc) · 4.94 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
import Space from "../models/Space.js";
import cloudinary from "../utils/cloudinary.js";
// 📚 Get all spaces
export const getSpaces = async (_req, res) => {
try {
const spaces = await Space.find().populate("host", "name email avatar");
res.status(200).json({ success: true, spaces });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
export const getSpaceById = async (req, res) => {
try {
const space = await Space.findById(req.params.id).populate(
"host",
"name email avatar"
);
if (!space)
return res
.status(404)
.json({ success: false, message: "Space not found" });
res.status(200).json({ success: true, space });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
// ➕ Create a new space
export const createSpace = async (req, res) => {
try {
const { title, description, category, price, status, eventDate, duration } =
req.body;
const user = req.user; // from auth middleware
// Handle thumbnail upload
let thumbnailUrl = "";
if (req.files && req.files.thumbnail && req.files.thumbnail[0]) {
const thumbnailUpload = await new Promise((resolve, reject) => {
const stream = cloudinary.uploader.upload_stream(
{ folder: "spaces/thumbnails" },
(error, result) => {
if (error) reject(error);
else resolve(result);
}
);
stream.end(req.files.thumbnail[0].buffer);
});
thumbnailUrl = thumbnailUpload.secure_url;
}
const space = await Space.create({
title,
description,
category,
thumbnail: thumbnailUrl,
price: price || 0,
status: "upcoming",
eventDate,
duration,
host: user._id,
});
res.status(201).json({ success: true, space });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
// 📝 Update a space
export const updateSpace = async (req, res) => {
try {
const { id } = req.params;
// Only allow these fields to be updated
const allowedUpdates = [
"title",
"description",
"category",
"thumbnail",
"price",
"status",
"eventDate",
"eventTime",
"duration",
"waitList",
"enrolledUsers",
];
const updates = {};
for (const key of allowedUpdates) {
if (req.body[key] !== undefined) updates[key] = req.body[key];
}
const existingSpace = await Space.findById(id);
if (!existingSpace) {
return res.status(404).json({ success: false, message: "Space not found" });
}
if (req.user.role !== "admin" && existingSpace.host.toString() !== req.user._id.toString()) {
return res.status(403).json({
success: false,
message: "Not authorized to update this space",
});
}
const space = await Space.findByIdAndUpdate(id, updates, {
new: true,
}).populate("host", "name email avatar");
res.status(200).json({ success: true, space });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
export const joinWaitList = async (req, res) => {
try {
const { id } = req.params; // space ID
const userId = req.user._id; // from auth middleware
// Add user to waitList if not already present
const space = await Space.findById(id);
if (!space) {
return res
.status(404)
.json({ success: false, message: "Space not found" });
}
if (space.waitList.includes(userId)) {
return res
.status(400)
.json({ success: false, message: "Already on waitlist" });
}
space.waitList.push(userId);
await space.save();
res.status(200).json({ success: true, message: "Joined waitlist" });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
// 📚 Get all spaces by a specific user (host)
export const getSpacesByHost = async (req, res) => {
try {
const { hostId } = req.params;
const spaces = await Space.find({ host: hostId }).populate(
"host",
"name email avatar"
);
res.status(200).json({ success: true, spaces });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};
// ❌ Delete a space
export const deleteSpace = async (req, res) => {
try {
const { id } = req.params;
const space = await Space.findById(id);
if (!space) {
return res.status(404).json({ success: false, message: "Space not found" });
}
if (req.user.role !== "admin" && space.host.toString() !== req.user._id.toString()) {
return res.status(403).json({
success: false,
message: "Not authorized to delete this space",
});
}
await Space.findByIdAndDelete(id);
res.status(200).json({ success: true, message: "Space deleted" });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
};