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