Skip to content

Commit

Permalink
add query service for listing posts and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual committed May 27, 2020
1 parent 706c960 commit 2aa985f
Show file tree
Hide file tree
Showing 9 changed files with 1,346 additions and 17 deletions.
26 changes: 14 additions & 12 deletions client/src/CommentList.js
@@ -1,17 +1,19 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import React from 'react';
// import React, { useState, useEffect } from 'react';
// import axios from 'axios';

const CommentList = ({postId}) => {
const [comments, setComments] = useState([]); // match to what is returned by api
// const CommentList = ({postId}) => {
const CommentList = ({comments}) => {
// const [comments, setComments] = useState([]); // match to what is returned by api

const fetchData = async () => {
const res = await axios.get(`http://localhost:4001/posts/${postId}/comments`);
// axios returns array
setComments(res.data);
}
useEffect(() => {
fetchData();
}, []);
// const fetchData = async () => {
// const res = await axios.get(`http://localhost:4001/posts/${postId}/comments`);
// // axios returns array
// setComments(res.data);
// }
// useEffect(() => {
// fetchData();
// }, []);

// Object.values is a built-in JS function to return array
const renderedComments = comments.map(comment => {
Expand Down
4 changes: 2 additions & 2 deletions client/src/PostList.js
Expand Up @@ -7,7 +7,7 @@ const PostList = () => {
const [posts, setPosts] = useState({});

const fetchPosts = async () => {
const res = await axios.get('http://localhost:4000/posts');
const res = await axios.get('http://localhost:4002/posts');
// axios returns data object
setPosts(res.data);
}
Expand All @@ -27,7 +27,7 @@ const PostList = () => {
</div>
<div className="card-body">
<h3>Comments</h3>
<CommentList postId={post.id} />
<CommentList comments={post.comments} />
<CommentCreate postId={post.id} />
</div>
</div>
Expand Down
8 changes: 7 additions & 1 deletion comments/index.js
Expand Up @@ -30,7 +30,7 @@ app.post('/posts/:id/comments', async (req, res) => {
commentsByPostId[req.params.id] = comments; // save comment

await axios.post('http://localhost:4005/events', {
type: 'CommentCreate',
type: 'CommentCreated',
data: {
id: commentId,
content,
Expand All @@ -42,6 +42,12 @@ app.post('/posts/:id/comments', async (req, res) => {

});

// receives any event coming from event bus
app.post('/events', (req,res) => {
console.log('Received event ', req.body.type);
res.send({})
})

app.listen(4001, ()=>{
console.log('Comments listening on 4001')
})
2 changes: 1 addition & 1 deletion event-bus/index.js
Expand Up @@ -14,7 +14,7 @@ app.post('/events', (req, res) => {
axios.post('http://localhost:4001/events', event);
axios.post('http://localhost:4002/events', event);

res.status(200);
res.status(200).send({ status: 'OK' });
});

app.listen(4005, () => {
Expand Down
6 changes: 6 additions & 0 deletions posts/index.js
Expand Up @@ -35,6 +35,12 @@ app.post('/posts', async (req, res) => {
res.status(201).send(posts[id])
})

// receives any event coming from event bus
app.post('/events', (req,res) => {
console.log('Received event ', req.body.type);
res.send({})
})

app.listen(4000, () => {
console.log("Listening on port 4000")
})
43 changes: 43 additions & 0 deletions query/index.js
@@ -0,0 +1,43 @@
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
// Dont need axios since it won't need to emit events?

const app = express();
app.use(bodyParser.json());
app.use(cors());

// GET /posts
// POST /events
// Decide on a data structure for all the comments
const posts = {};

app.get('/posts', (req, res) => {
res.send(posts);
});

// receives events from event bus
app.post('/events', (req, res) => {
const { type, data } = req.body;

if (type === 'PostCreated') {
const { id, title } = data;

posts[id] = { id, title, comments: [] };
}

if (type === 'CommentCreated') {
const { id, content, postId } = data;
const post = posts[postId];
post.comments.push({ id, content });
}
// Node doesn't show content of nested structures
// to save on output clutter
// console.log(posts);
// Still a route handle so be sure to return something
res.send({});
});

app.listen(4002, ()=>{
console.log("Query service listening on 4002")
})

0 comments on commit 2aa985f

Please sign in to comment.