Skip to content

Commit e9bf2c0

Browse files
committedDec 19, 2018
blog reaction system added
1 parent 6b68fb0 commit e9bf2c0

File tree

5 files changed

+57
-68
lines changed

5 files changed

+57
-68
lines changed
 

‎app/global/middlewares/ValidAuthToken.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import User from '../../models/user';
55
import { logger } from '../../../log';
66

77
const ValidAuthToken = (req, res, next) => {
8-
const token = req.headers.authorization;
8+
const token = (req.headers.authorization).split(' ')[1];
99
if (token) {
1010
jwt.verify(token, configServer.app.WEB_TOKEN_SECRET, (err, decodedUser) => {
1111
if (err) {

‎app/models/blog.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const BlogSchema = mongoose.Schema({
1818
},
1919

2020
likes: [{
21+
count: {
22+
type: Number,
23+
default: 0,
24+
required: true,
25+
},
2126
userId: {
2227
type: mongoose.Schema.Types.ObjectId,
2328
ref: 'User',

‎app/routes/auth/auth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ router.post('/onboard', (req, res) => {
9999
branch: req.body.data.branch.split('_')[1],
100100
course: req.body.data.branch.split('_')[0],
101101
},
102-
}, (error, modifiedUser) => {
102+
}, { new: true }, (error, modifiedUser) => {
103103
if (error) {
104104
res.json(ResponseTemplate.error(401, 'Some error occurred while onboarding'));
105105
}

‎app/routes/blog/blog.js

+49-57
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import express from 'express';
2-
import jwt from 'jsonwebtoken';
32
import async from 'async';
43
import ResponseTemplate from '../../global/templates/response';
54
import {
65
logger,
76
} from '../../../log';
87
import Blog from '../../models/blog';
9-
import config from '../../../config';
108

119
const router = express.Router();
1210

@@ -28,77 +26,69 @@ router.get('/', (req, res) => {
2826
}));
2927
}
3028
});
31-
// Blog.find({}, null, { limit: perPage, skip: (pageNo - 1) * perPage }, (err, blogs) => {
32-
// if (err) {
33-
// logger.error(err);
34-
// res.json(ResponseTemplate.error(401, 'Error while fetching the blogs'));
35-
// } else {
36-
// res.json(ResponseTemplate.success('Blog fetched successfully', { blogs }));
37-
// }
38-
// });
39-
});
40-
41-
router.post('/', (req, res) => {
42-
const {
43-
data,
44-
} = req.body;
45-
46-
jwt.verify(data.token, config.app.WEB_TOKEN_SECRET, (err, decoded) => {
47-
if (err) {
48-
res.json(ResponseTemplate.error(401, 'Invalid User'));
49-
} else {
50-
const blog = new Blog();
51-
blog.userId = decoded.user._id;
52-
blog.blogTitle = data.title;
53-
blog.blogContent = data.content;
54-
blog.save((error) => {
55-
if (error) {
56-
logger.error(err);
57-
res.json(ResponseTemplate.error(401, 'Some error occured while saving the blog'));
58-
} else {
59-
res.json(ResponseTemplate.success('Blog created Successfully'));
60-
}
61-
});
62-
}
63-
});
6429
});
6530

6631
router.get('/:id', (req, res) => {
6732
Blog.findById(req.params.id)
6833
.populate('userId')
34+
.populate('comments.userId')
6935
.exec((err, blog) => {
7036
if (err) {
7137
logger.error(err);
72-
res.json({
73-
success: false,
74-
err,
75-
});
38+
res.json(ResponseTemplate.error('Some Error Occured'));
7639
} else {
77-
res.json({
78-
success: true,
79-
data: {
80-
blog,
81-
},
82-
});
40+
res.json(ResponseTemplate.success('Blog Fetched Successfully', { blog }));
8341
}
8442
});
8543
});
8644

45+
router.post('/', (req, res) => {
46+
const {
47+
data,
48+
} = req.body;
49+
50+
const blog = new Blog();
51+
blog.userId = req.user._id;
52+
blog.blogTitle = data.title;
53+
blog.blogContent = data.content;
54+
blog.save((error) => {
55+
if (error) {
56+
logger.error(error);
57+
res.json(ResponseTemplate.error(401, 'Some error occured while saving the blog'));
58+
} else {
59+
res.json(ResponseTemplate.success('Blog created Successfully'));
60+
}
61+
});
62+
});
63+
8764
router.put('/:id', (req, res) => {
88-
console.log(req.user);
8965
const task = [
9066
(callback) => {
91-
Blog.findByIdAndUpdate(req.params.id, {
92-
$push: {
93-
likes: {
94-
userId: req.user._id,
95-
},
67+
Blog.findOneAndUpdate({ _id: req.params.id, 'likes.userId': req.user._id }, {
68+
$inc: {
69+
'likes.$.count': 1,
9670
},
97-
}, (err, result) => {
71+
}, { new: true }, (err, result) => {
9872
if (err) {
9973
return callback(err, null);
10074
}
101-
return callback(null, result);
75+
if (result == null) {
76+
Blog.findByIdAndUpdate(req.params.id, {
77+
$push: {
78+
likes: {
79+
count: 1,
80+
userId: req.user._id,
81+
},
82+
},
83+
}, { new: true }, (error, newResult) => {
84+
if (error) {
85+
return callback(err, null);
86+
}
87+
return callback(null, newResult);
88+
});
89+
} else {
90+
return callback(null, result);
91+
}
10292
});
10393
},
10494
];
@@ -112,7 +102,7 @@ router.put('/:id', (req, res) => {
112102
userId: req.user._id,
113103
},
114104
},
115-
}, (err, result) => {
105+
}, { new: true }, (err, result) => {
116106
if (err) {
117107
return callback(err, null);
118108
}
@@ -139,10 +129,12 @@ router.put('/:id', (req, res) => {
139129
success: false,
140130
err,
141131
});
132+
} else if (req.query.action === 'LIKE') {
133+
res.json(ResponseTemplate.success('Content Liked', { result }));
134+
} else if (req.query.action === 'COMMENT') {
135+
res.json(ResponseTemplate.success('COMMENTED SUCCESSFULLY', { result }));
142136
} else {
143-
res.json({
144-
result,
145-
});
137+
res.json(ResponseTemplate.success('Unknown action', null));
146138
}
147139
});
148140
});

‎docker-compose.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,4 @@ services:
1010
- /app/node_modules
1111
- .:/app
1212
ports:
13-
- 8000:8000
14-
mongo:
15-
image: mongo
16-
volumes:
17-
- storedData:/data/db
18-
- configData:/data/configdb
19-
volumes:
20-
storedData:
21-
configData:
13+
- 8000:8000

0 commit comments

Comments
 (0)
Please sign in to comment.