Skip to content

Commit

Permalink
Added /api/events to get all events created by user or participated b…
Browse files Browse the repository at this point in the history
…y them
  • Loading branch information
gohanko committed Aug 21, 2024
1 parent 4f5ed99 commit 2bc209b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
17 changes: 14 additions & 3 deletions controllers/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions routes/event.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -90,12 +90,11 @@ eventRouter.put(
attendEvent
)

// TODO later
eventRouter.get(
'/event/all/',
'/events/',
inputValidation,
mustBeAuthorized,
getAllEvents
getEvents
)

export default eventRouter
2 changes: 1 addition & 1 deletion tests/api/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ describe('POST /api/event/:eventId/attend', () => {

expect(response.status).toBe(201)
})
})
})

0 comments on commit 2bc209b

Please sign in to comment.