From 2bc209b943c99bf63ef56ebe0105623f62a5a61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=9E=E5=9C=8B=E5=BF=A0=20/=20Yii=20Kuo=20Chong?= Date: Wed, 21 Aug 2024 19:30:17 +0800 Subject: [PATCH] Added /api/events to get all events created by user or participated by them --- controllers/event.controller.ts | 17 ++++++++++++++--- routes/event.route.ts | 7 +++---- tests/api/event.test.ts | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/controllers/event.controller.ts b/controllers/event.controller.ts index cef02f7..0b5863b 100644 --- a/controllers/event.controller.ts +++ b/controllers/event.controller.ts @@ -220,12 +220,23 @@ export const attendEvent = async ( }) } -// TODO -export const getAllEvents = async ( +export const getEvents = async ( req: Request, res: Response ) => { - const events = await Event.findAll({ where: { createdByUserId: req.user.id }}) + const eventsCreatedByUser = await Event.findAll({ where: { createdBy: req.user.id }}) + const eventsParticipatedByUser = await EventParticipation.findAll({ + where: { userId: req.user.id }, + include: [{ + model: Event + }] + }) + + const events: any = eventsCreatedByUser + eventsParticipatedByUser.map((eventParticipation) => { + events.push(eventParticipation.dataValues.event) + }) + return res.status(200).json({ status: "success", diff --git a/routes/event.route.ts b/routes/event.route.ts index f15bccc..09f284a 100644 --- a/routes/event.route.ts +++ b/routes/event.route.ts @@ -4,11 +4,11 @@ import { readEvent, updateEvent, deleteEvent, - getAllEvents, getEventCode, joinEvent, leaveEvent, attendEvent, + getEvents, } from '../controllers/event.controller' import inputValidation from '../middlewares/inputValidation.middleware' import { body, check, param } from 'express-validator' @@ -90,12 +90,11 @@ eventRouter.put( attendEvent ) -// TODO later eventRouter.get( - '/event/all/', + '/events/', inputValidation, mustBeAuthorized, - getAllEvents + getEvents ) export default eventRouter \ No newline at end of file diff --git a/tests/api/event.test.ts b/tests/api/event.test.ts index 732a82c..75b344b 100644 --- a/tests/api/event.test.ts +++ b/tests/api/event.test.ts @@ -241,4 +241,4 @@ describe('POST /api/event/:eventId/attend', () => { expect(response.status).toBe(201) }) -}) \ No newline at end of file +})