From 5b796d0c07375fc6afdbd44f788967d1480fe29e Mon Sep 17 00:00:00 2001 From: Le Nguyen Quang Minh <101281380+lnqminh3003@users.noreply.github.com> Date: Fri, 6 Feb 2026 22:14:33 -0700 Subject: [PATCH] Fetch created events by user id --- .../src/controllers/event_controller.ts | 38 +++++++++++++++++++ shatter-backend/src/routes/event_routes.ts | 3 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/shatter-backend/src/controllers/event_controller.ts b/shatter-backend/src/controllers/event_controller.ts index 9d1468b..4333226 100644 --- a/shatter-backend/src/controllers/event_controller.ts +++ b/shatter-backend/src/controllers/event_controller.ts @@ -328,3 +328,41 @@ export async function getEventById(req: Request, res: Response) { res.status(500).json({ success: false, error: err.message }); } } + +/** + * GET /api/events/createdEvents/user/:userId + * Get list of events created by a specific user + * + * @param req.params.userId - User ID (required) + * + * @returns 200 with list of events on success + * @returns 400 if userId is missing + * @returns 404 if no events are found for the user + */ +export async function getEventsByUserId(req: Request, res: Response) { + try { + const { userId } = req.params; + + if (!userId) { + return res + .status(400) + .json({ success: false, error: "userId is required" }); + } + + const events = await Event.find({ createdBy: userId }); + + if (!events || events.length === 0) { + return res.status(404).json({ + success: false, + error: "No events found for this user", + }); + } + + res.status(200).json({ + success: true, + events, + }); + } catch (err: any) { + res.status(500).json({ success: false, error: err.message }); + } +} \ No newline at end of file diff --git a/shatter-backend/src/routes/event_routes.ts b/shatter-backend/src/routes/event_routes.ts index 761a120..cf03550 100644 --- a/shatter-backend/src/routes/event_routes.ts +++ b/shatter-backend/src/routes/event_routes.ts @@ -1,5 +1,5 @@ import { Router } from 'express'; -import { createEvent, getEventByJoinCode, getEventById, joinEventAsUser, joinEventAsGuest } from '../controllers/event_controller'; +import { createEvent, getEventByJoinCode, getEventById, joinEventAsUser, joinEventAsGuest, getEventsByUserId } from '../controllers/event_controller'; import { authMiddleware } from '../middleware/auth_middleware'; const router = Router(); @@ -10,6 +10,7 @@ router.get("/event/:joinCode", getEventByJoinCode); router.get("/:eventId", getEventById); router.post("/:eventId/join/user", authMiddleware, joinEventAsUser); router.post("/:eventId/join/guest", joinEventAsGuest); +router.get("/createdEvents/user/:userId", authMiddleware, getEventsByUserId); export default router; \ No newline at end of file