import { NextApiRequest , NextApiResponse } from 'next' ; import { APIfeatures } from '../../utils/apiFeatures' ; import Comment , { IComment } from '../models/comment' ; import { StatusCodes } from 'http-status-codes' ; import catchErrors from '../../middleware/catchErrors' ; EXPORTS export { getAllApprovedComments , postNewComment } ; GET function param { NextApiRequest } req param { NextApiResponse } res return { undefined } const getAllApprovedComments = async ( req , res ) : Promise < void > => { const features = new APIfeatures ( Comment . find ( { isApproved : true } ) , req . query as { [ key ] : string ; } ) . sort ( ) ; const allComments = await features . query ; res . status ( StatusCodes . OK ) . json ( { success : true , total : allComments . length , comments : allComments , } ) ; } ; POST function param { NextApiRequest } req param { NextApiResponse } res returns { undefined } const postNewComment = catchErrors ( async ( req , res ) : Promise < void > => { const { comment , author } = req . body ; const newComment = await Comment . create ( { comment , author , } ) ; res . status ( StatusCodes . OK ) . json ( { success : true , newComment , } ) ; } ) ;