Initial commit
This commit is contained in:
1
blog/query/.gitignore
vendored
Normal file
1
blog/query/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
57
blog/query/index.js
Normal file
57
blog/query/index.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(cors());
|
||||
|
||||
const posts = {};
|
||||
|
||||
const handleEvent = (type, data) => {
|
||||
console.log(`Handling Event ${type}`, data);
|
||||
|
||||
if (type === 'PostCreated') {
|
||||
const { id, title } = data;
|
||||
posts[id] = { id, title, comments: [] };
|
||||
}
|
||||
|
||||
if(type === 'CommentCreated') {
|
||||
const { id, content, postId, status } = data;
|
||||
const post = posts[postId];
|
||||
post.comments.push({ id, content, status });
|
||||
}
|
||||
|
||||
if(type === 'CommentUpdated') {
|
||||
const { id, content, postId, status } = data;
|
||||
const post = posts[postId];
|
||||
const comment = post.comments.find(comment => comment.id === id);
|
||||
|
||||
comment.status = status;
|
||||
comment.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
app.get('/posts', (req, res) => {
|
||||
res.send(posts);
|
||||
});
|
||||
|
||||
app.post('/events', (req, res) => {
|
||||
const { type, data } = req.body;
|
||||
handleEvent(type, data);
|
||||
res.send({});
|
||||
});
|
||||
|
||||
app.listen(4002, async () => {
|
||||
console.log('Listening on 4002');
|
||||
|
||||
try {
|
||||
const res = await axios.get('http://localhost:4005/events');
|
||||
for (let event of res.data) {
|
||||
handleEvent(event.type, event.data);
|
||||
}
|
||||
}catch(error){
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
1256
blog/query/package-lock.json
generated
Normal file
1256
blog/query/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
blog/query/package.json
Normal file
18
blog/query/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "query",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon 4002"
|
||||
},
|
||||
"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