-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataManager.js
187 lines (174 loc) · 4.97 KB
/
dataManager.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const { default: mongoose } = require("mongoose");
const users = require("./schemas/users");
const posts = require("./schemas/posts");
const comments = require("./schemas/comment");
const postAggregate = require("./schemas/postAggregate");
const {
statusCodes,
sendInternalErrorAsRespond,
} = require("./utils/httpUtils");
const ConnectionString = process.env.MONGO_CONNECT;
//connect to DB
mongoose.connect(
ConnectionString,
() => {
console.log("connected");
},
(e) => {
console.error(e);
}
);
/**
*add like to the post
*sends 403 if post doesnt exist.
*sends 500 if there was an internal error.
* @param {object} req
* @param {object} res
*/
async function addPostLike (req, res) {
const userID = mongoose.Types.ObjectId(req.user.id);
const { postId } = req.params;
//if true its post, false its a comment.
try {
const postExits = await posts.exists({ _id: postId });
if (postExits) {
await posts.findByIdAndUpdate(postId, { $addToSet: { likes: [userID] } });
res.json({ message: "added like" });
} else {
res.status(statusCodes.BAD_REQUEST)({ message: "cannot add like" });
}
} catch (err) {
sendInternalErrorAsRespond(res, error);
}
};
/**
*remove like from the post
*sends 403 if post doesnt exist.
*sends 500 if there was an internal error.
* @param {object} req
* @param {object} res
*/
async function removePostLike (req, res) {
const userID = mongoose.Types.ObjectId(req.user.id);
const { postId } = req.params;
try {
await posts.findByIdAndUpdate(postId, {
$pull: { likes: userID },
});
res.json({ message: "removed like" });
} catch (err) {
sendInternalErrorAsRespond(res, error);
}
};
/**
*remove like from the comment
*sends 403 if comment doesnt exist.
*sends 500 if there was an internal error.
* @param {object} req
* @param {object} res
*/
async function addCommentLike(req, res) {
const userID = mongoose.Types.ObjectId(req.user.id);
const { commentId } = req.params;
try {
const commentExist = await comments.exists({ _id: commentId });
if (!commentExist) {
res
.status(statusCodes.UNPROCESSABLE)
.json({ message: "cannot add like" });
return;
}
await comments.findByIdAndUpdate(commentId, {
$addToSet: { likes: [userID] },
});
res.status(statusCodes.OK).json({ message: "added like" });
} catch (err) {
sendInternalErrorAsRespond(err, res);
}
}
async function removCommentLike (req, res) {
const userID = mongoose.Types.ObjectId(req.user.id);
const { commentId } = req.params;
try {
await comments.findByIdAndUpdate(commentId, { $pull: { likes: userID } });
res.json({ message: "removed like" });
} catch (err) {
sendInternalErrorAsRespond(err, res);
}
};
/**
* Retrieves latest X posts from the DB.
* @param {number} numberOfPosts - Number of posts to retrieve.
* @param {number} from - Start index.
* @returns {Promise} - Promise with the retrieved posts.
*/
async function getLatestXPosts(numberOfPosts, from) {
const data = await posts.aggregate(postAggregate);
return data;
}
/**
* Adds a comment to an existing post.
* @param {object} req
* @param {object} res
*/
async function addComment(req, res) {
userID = req.user.id;
const { postId } = req.params;
const { content } = req.body;
const newComment = new comments({
userID: new mongoose.Types.ObjectId(userID),
content: content,
});
try {
const exists = await posts.exists({ _id: postId }).lean().exec();
if (exists) {
let insertedCommentID;
const com = await newComment.save();
insertedCommentID = com._id.toString();
await posts.findByIdAndUpdate(postId, {
$push: { commentsID: [insertedCommentID] },
});
let comment = await comments
.findById(insertedCommentID)
.populate({ path: "userID", select: ["-password"] })
.exec();
res.status(statusCodes.OK).json({ message: "success", data: comment });
} else {
res.status(statusCodes.NOT_FOUND).json({ message: "post wasn't found" });
}
} catch (err) {
sendInternalErrorAsRespond(err, res);
}
}
/**
* Sets a new post.
* @param {object} req
* @param {object} res
*/
async function setPost(req, res) {
try {
const userID = req.user.id;
const { content } = req.body;
const newPost = new posts({
userID: new mongoose.Types.ObjectId(userID),
content: content,
});
let post = await newPost.save();
post = await posts
.findById(post.id)
.populate({ path: "userID", select: ["-password"] })
.exec();
res.status(statusCodes.OK).json(post);
} catch (err) {
sendInternalErrorAsRespond(err, res);
}
}
module.exports = {
addPostLike,
removePostLike,
addCommentLike,
removCommentLike,
setPost,
addComment,
getLatestXPosts,
};