All files / src/controllers comments_controller.ts

92.3% Statements 48/52
71.42% Branches 10/14
100% Functions 5/5
92.3% Lines 48/52

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        4x 13x 13x 13x 3x     10x 10x 1x     9x 9x 9x 9x 8x   1x       4x 5x 5x 5x 5x 4x       4x 4x 4x 4x 3x   3x 3x   2x       4x 3x 3x 3x 2x     2x 2x   1x       4x 3x 3x 3x 2x     2x 2x   1x       4x 3x 3x 3x              
import postModel from '../models/posts_model';
import commentModel from '../models/comments_model';
import mongoose from 'mongoose';
 
const createComment = async (req, res) => {
    const comment = req.body;
    try{
        if (!comment.postId || !mongoose.Types.ObjectId.isValid(comment.postId)) {
            return res.status(400).send("Invalid post ID");
        }
 
        const post = await postModel.findById(comment.postId);
        if (!post) {
            return res.status(404).send('Post not found');
        }
        
        const utcNow = new Date().toISOString();
        comment.createdAt = utcNow;
        comment.updatedAt = utcNow;
        const newComment = await commentModel.create(comment);
        return res.status(201).send(newComment);
    } catch(error) {
        return res.status(400).send("Bad Request");
    }
}
 
const updateCommentById = async (req, res) => {
    const id = req.params.id;
    const comment = req.body;
    try {
        const oldComment = await commentModel.findById(id);
        Iif (oldComment == null) {
            return res.status(404).send('Comment not found');
        }
        
        comment.postId = oldComment.postId;
        comment.updatedAt = new Date().toISOString();
        comment.createdAt = oldComment.createdAt;
        await new commentModel(comment).validate();
        await commentModel.findByIdAndUpdate(id, comment);
 
        comment._id = oldComment._id;
        return res.status(201).send(comment);
    } catch(error) {
        return res.status(400).send("Bad Request");
    }
}
 
const getByPostId = async (req, res) => {
    const postId = req.params.postId;
    try {
        const post = await postModel.findById(postId);
        Iif (post == null) {
            return res.status(400).send('Post not found');
        }
        const comments = await commentModel.find({ postId: post._id });
        return res.status(200).send(comments);
    } catch(error) {
        return res.status(400).send("Bad Request");
    }
}
 
const deleteCommentById = async (req, res) => {
    const id = req.params.id;
    try {
        const comment = await commentModel.findById(id);
        Iif (comment == null) {
            return res.status(404).send('Comment not found');
        }
        const deletedComment = await commentModel.findByIdAndDelete(id);
        return res.status(200).send(deletedComment);
    } catch(error) {
        return res.status(400).send("Bad Request");
    }
}
 
const getAllComments = async (req, res) => {
    try {
        const comments = await commentModel.find();
        return res.status(200).send(comments || []);
    } catch(error) {
        return res.status(400).send("Bad Request");
    }
}
 
export default {createComment, getByPostId, updateCommentById, deleteCommentById, getAllComments};