Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 2x 2x | import { Request, Response } from 'express';
import * as commentsService from '../services/comments_service';
import * as postsService from '../services/posts_service';
import { handleError } from '../utils/handle_error';
export const addComment = async (req: Request, res: Response): Promise<void> => {
try {
const { postId } = req.body;
// Validate if the post exists
const postExists = await postsService.getPostById(postId);
if (!postExists) {
res.status(404).json({ message: "Post not found: " + postId });
return;
}
const savedComment = await commentsService.addComment(req.body);
res.status(201).json(savedComment);
} catch (err) {
handleError(err, res);
}
};
export const getCommentById = async (req: Request, res: Response): Promise<void> => {
try {
const comment = await commentsService.getCommentById(req.params.comment_id);
if (comment == null) {
res.status(204).json({ message: 'No comments found for this post' });
} else {
res.json(comment);
}
} catch (err) {
handleError(err, res);
}
};
export const getCommentsByPostId = async (req: Request, res: Response): Promise<void> => {
try {
const comments = await commentsService.getCommentsByPostId(req.params.post_id);
if (comments.length === 0) {
res.status(204).json({ message: 'No comments found for this post' });
} else {
res.json(comments);
}
} catch (err) {
handleError(err, res);
}
};
export const getAllComments = async (req: Request, res: Response): Promise<void> => {
try {
const comments = await commentsService.getAllComments();
res.json(comments);
} catch (err) {
handleError(err, res);
}
};
export const updateComment = async (req: Request, res: Response): Promise<void> => {
try {
const updatedComment = await commentsService.updateComment(req.params.comment_id, req.body);
if (!updatedComment) {
res.status(404).json({ message: 'Comment not found' });
} else {
res.json(updatedComment);
}
} catch (err) {
handleError(err, res);
}
};
export const deleteComment = async (req: Request, res: Response): Promise<void> => {
try {
const deletedComment = await commentsService.deleteComment(req.params.comment_id);
if (!deletedComment) {
res.status(404).json({ message: 'Comment not found' });
} else {
res.json({ message: 'Comment deleted successfully' });
}
} catch (err) {
handleError(err, res);
}
}; |