File size: 1,719 Bytes
97f53b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const express = require("express");
const router = express.Router();
const User = require('../models/user');
const Post = require('../models/postNews');
const Comment = require('../models/comments');
const response = require('../utils/responseModel');


router.post('/', async (req, res) => {
    console.log(req.body)
    User.findOne({ unique_id: req.body.userId }, function (err, data) {
        if (!data) {
            res.send(failedResponse('Data not found!'))
        } else {
            console.log(" Data => " + data)
            Post.findOne({ postId: req.body.postId }, function (err, postData) {
                if (!postData) {
                    res.send(failedResponse('Data not found!: ' + err))
                } else {
                    var comment = Comment({
                        user: data,
                        post: postData,
                        userId: req.body.userId,
                        text: req.body.comment
                    })

                    comment.save()
                        .then((post) => {
                            res.status(200).json(post);
                        })
                        .catch((error) => {
                            res.status(500).json({ error: 'An error occurred while creating the post.' });
                        });

                }

            })
        }
    });

});


router.delete('/:id', async (req, res) => {

    Comment.remove({ _id: req.params.id })
        .exec()
        .then(result => {
            res.status(200).send(response.successResponse(result));
        })
        .catch(error => {
            res.send(500).send(response.failedResponse(error))
        });

});

module.exports = router;