2023-07-31 23:18:07 +00:00
|
|
|
import comment from 'services/comments'
|
2023-07-25 21:25:43 +00:00
|
|
|
import type { Request, Response } from 'express'
|
2023-07-31 23:18:07 +00:00
|
|
|
import { badRequest } from 'lib/http-errors'
|
2023-07-25 21:25:43 +00:00
|
|
|
|
2023-07-31 23:18:07 +00:00
|
|
|
async function commentCreateController (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<void> {
|
2023-07-25 21:25:43 +00:00
|
|
|
const { content, postId } = req.body
|
|
|
|
const id = req.user?.id ?? ''
|
|
|
|
|
|
|
|
if (postId === undefined) {
|
2023-07-31 23:18:07 +00:00
|
|
|
badRequest(res, 'Expected post id'); return
|
2023-07-25 21:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (content === undefined) {
|
2023-07-31 23:18:07 +00:00
|
|
|
badRequest(res, 'Expected comment content'); return
|
2023-07-25 21:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = await comment.create(postId, content, id)
|
|
|
|
|
|
|
|
if (result instanceof Error) {
|
2023-07-31 23:18:07 +00:00
|
|
|
badRequest(res, result.message); return
|
2023-07-25 21:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default commentCreateController
|