Skip to content

Commit 211d8f5

Browse files
authored
feat: get event's comment API (Beyond-Imagination#178)
1 parent 69c1efc commit 211d8f5

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

apps/server/src/controllers/v1/events.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ router.post('/:id/comments', verifyToken, async (req: Request, res: Response) =>
8383
res.sendStatus(204)
8484
})
8585

86+
router.get('/:id/comments', verifyToken, async (req: Request, res: Response) => {
87+
const options = {
88+
page: Number(req.query.page) || 1,
89+
limit: Number(req.query.limit) || 10,
90+
}
91+
const comments = await CommentsModel.findByEventId(req.params.id, options)
92+
res.status(200).json(comments)
93+
})
94+
8695
router.post('/:id/likes', verifyToken, async (req: Request, res: Response) => {
8796
const event = await EventsModel.findById(req.params.id)
8897

apps/server/src/models/comments.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getModelForClass, plugin, prop, defaultClasses } from '@typegoose/typegoose'
1+
import { getModelForClass, plugin, prop, defaultClasses, ReturnModelType } from '@typegoose/typegoose'
22
import mongoose from 'mongoose'
33
import mongoosePaginate from 'mongoose-paginate-v2'
44
import { Events, User } from '@/models'
@@ -38,6 +38,17 @@ export class Comments extends defaultClasses.TimeStamps {
3838
updatedAt: this.updatedAt,
3939
}
4040
}
41+
42+
public static async findByEventId(
43+
this: ReturnModelType<typeof Comments>,
44+
eventId: string,
45+
options: {
46+
page: number
47+
limit: number
48+
},
49+
) {
50+
return this.paginate({ eventId: eventId }, options)
51+
}
4152
}
4253

4354
export const CommentsModel = getModelForClass(Comments)

0 commit comments

Comments
 (0)