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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 7x 9x 9x 9x 9x 9x 1x 1x 8x 8x 8x 7x 1x 1x 1x 1x 7x 3x 3x 3x 3x 3x 1x 2x 7x 4x 4x 4x 1x 3x 7x 3x 3x 3x 3x 7x 2x 2x 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'; import {CommentData} from "types/comment_types"; import {CustomRequest} from "types/customRequest"; export const addComment = async (req: CustomRequest, res: Response): Promise<void> => { try { const { postId, content } = req.body; const owner = req.user.id; // Validate if the post exists const postExists = await postsService.getPostById(postId); if (!postExists) { res.status(404).json({ message: "Post not found: " + postId }); return; } const commentData: CommentData = { postId, owner, content }; const savedComment = await commentsService.addComment(commentData); 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.commentId); Iif (!comment) { res.status(404).json({ message: "Comment not found: " + req.params.commentId }); return; } else { res.json(comment); } } catch (err) { handleError(err, res); } }; export const getCommentsByPostId = async (req: Request, res: Response): Promise<void> => { try { const postExists = await postsService.getPostById(req.params.postId); Iif (!postExists) { res.status(400).json({ message: "Post not found: " + req.params.postId }); return; } const comments = await commentsService.getCommentsByPostId(req.params.postId); if (comments.length === 0) { res.status(200).json([]); } 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(); if (comments.length === 0) { res.status(200).json([]); } else { res.json(comments); } } catch (err) { handleError(err, res); } }; // TODO - make different functions for updating comment and updating comment content export const updateComment = async (req: Request, res: Response): Promise<void> => { try { // TODO - parse the body before sending to server const updatedComment = await commentsService.updateComment(req.params.commentId, req.body); Iif (!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.commentId); Iif (!deletedComment) { res.status(404).json({ message: 'Comment not found' }); } else { res.json({ message: 'Comment deleted successfully' }); } } catch (err) { handleError(err, res); } }; |