Initial commit
This commit is contained in:
1
blog/comments/.gitignore
vendored
Normal file
1
blog/comments/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
58
blog/comments/index.js
Normal file
58
blog/comments/index.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const { randomBytes } = require('crypto');
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
const axios = require('axios');
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(cors());
|
||||
|
||||
const commentsByPostID = {};
|
||||
|
||||
app.get('/posts/:id/comments', (req, res) => {
|
||||
res.send(commentsByPostID[req.params.id] || []);
|
||||
});
|
||||
|
||||
app.post('/posts/:id/comments', async (req, res) => {
|
||||
const commentID = randomBytes(4).toString('hex');
|
||||
const { content } = req.body;
|
||||
|
||||
const comments = commentsByPostID[req.params.id] || [];
|
||||
|
||||
comments.push({ id: commentID, content, status: 'pending' })
|
||||
|
||||
commentsByPostID[req.params.id] = comments;
|
||||
|
||||
await axios.post('http://localhost:4005/events', {
|
||||
type: 'CommentCreated',
|
||||
data: {
|
||||
id: commentID, content, postId: req.params.id, status: 'pending'
|
||||
}
|
||||
}).catch(error => console.log(error));
|
||||
|
||||
res.status(201).send(comments);
|
||||
});
|
||||
|
||||
app.post('/events', async (req, res) => {
|
||||
console.log('Received Event', req.body.type);
|
||||
const { type, data } = req.body;
|
||||
|
||||
if(type === 'CommentModerated') {
|
||||
const { postId, id, status, content } = data;
|
||||
const comments = commentsByPostID[postId];
|
||||
const comment = comments.find(comment => comment.id === id);
|
||||
comment.status = status;
|
||||
|
||||
await axios.post('http://localhost:4005/events', {
|
||||
type: 'CommentUpdated',
|
||||
data: { id, status, postId, content }
|
||||
}).catch(error => console.log(error));
|
||||
}
|
||||
|
||||
res.send({});
|
||||
});
|
||||
|
||||
app.listen(4001, () => {
|
||||
console.log('Listening on 4001');
|
||||
});
|
||||
1256
blog/comments/package-lock.json
generated
Normal file
1256
blog/comments/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
blog/comments/package.json
Normal file
18
blog/comments/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "comments",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
"nodemon": "^2.0.7"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user