-
Notifications
You must be signed in to change notification settings - Fork 46
/
comment.controller.js
127 lines (118 loc) · 3.19 KB
/
comment.controller.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Promise from 'bluebird';
import mongoose from 'mongoose';
import map from 'lodash/map';
import Comment from '../models/comment.model';
/*
* Load comment and append to req.
*/
function load(req, res, next, id) {
Comment.get(id)
.then((comment) => {
req.comment = comment; // eslint-disable-line no-param-reassign
return next();
})
.catch(e => next(e));
}
/**
* @swagger
* tags:
* - name: comment
* description: Commenting of Episodes
*/
/**
* @swagger
* /posts/{postId}/comment:
* post:
* summary: Create comment for episode
* description: Create comment for episode
* tags: [comment]
* security:
* - Token: []
* parameters:
* - $ref: '#/parameters/postId'
* - in: body
* name: content
* type: string
* required: true
* description: Comment content
* responses:
* '201':
* description: successful created
* schema:
* type: object
* properties:
* result:
* $ref: '#/definitions/Comment'
* '401':
* $ref: '#/responses/Unauthorized'
* '404':
* $ref: '#/responses/NotFound'
*/
function create(req, res, next) {
const { postId } = req.params;
const { parentCommentId } = req.body;
const { content } = req.body;
const { user } = req;
const comment = new Comment();
comment.content = content
comment.post = postId
// If this is a child comment we need to assign it's parent
if (parentCommentId) {
comment.parentComment = parentCommentId
}
comment.author = user._id
comment.save()
.then((commentSaved) => {
// TODO: result key is not consistent with other responses, consider changing this
return res.status(201).json({result: commentSaved});
})
.catch( (err) => next(err));
}
/**
* @swagger
* /posts/{postId}/comments:
* get:
* summary: Get comments for episode
* description: Get comments for episode
* tags: [comment]
* security: []
* parameters:
* - $ref: '#/parameters/postId'
* responses:
* '200':
* description: successful operation
* schema:
* type: object
* properties:
* result:
* type: array
* items:
* $ref: '#/definitions/Comment'
* '404':
* $ref: '#/responses/NotFound'
*/
function list(req, res, next) {
const { postId } = req.params;
Comment.getTopLevelCommentsForItem(postId)
.then((comments) => {
// Here we are fetching our nested comments, and need everything to finish
let nestedCommentPromises = map(comments, (comment) => {
return Comment.fillNestedComments(comment);
});
return Promise.all(nestedCommentPromises);
})
.then((parentComments) => {
// If authed then fill in if user has liked:
if (req.user) {
// Let's get all our voe info for both children and parent comments:
return Comment.populateVoteInfo(parentComments, req.user);
} else {
return parentComments;
}
})
.then( (parentComments) => {
res.json({result: parentComments});
})
.catch(e => next(e));
}
export default {load, list, create};