Skip to content

Demo/umn database microservice #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 02_A_Mini_Microservices_App/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Dependencies
node_modules/
package-lock.json
.pnp/
.pnp.js

# Testing
coverage/

# Production
build/

# Miscellaneous
.DS_Store

# Environment Variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
24,354 changes: 14,922 additions & 9,432 deletions 02_A_Mini_Microservices_App/app/client/package-lock.json

Large diffs are not rendered by default.

94 changes: 54 additions & 40 deletions 02_A_Mini_Microservices_App/app/comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,80 @@ app.use(cors());

const commentsByPostId = {};

// Handler untuk memproses event yang masuk
const handleEvent = (event) => {
const { type, data } = event;

if (type === 'CommentCreated') {
const { id, content, postId, status } = data;
const comments = commentsByPostId[postId] || [];
comments.push({ id, content, status });
commentsByPostId[postId] = comments;
}

if (type === 'CommentModerated') {
const { postId, id, status, content } = data;
const comments = commentsByPostId[postId];

if (comments) {
const comment = comments.find((comment) => comment.id === id);
if (comment) {
comment.status = status;
}
}

axios.post('http://localhost:4005/events', {
type: 'CommentUpdated',
data: { id, status, postId, content },
}).catch(err => console.error('Error sending CommentUpdated event:', err.message));
}

console.log('Handled event:', type);
};

// Endpoint untuk mendapatkan semua komentar dari post tertentu
app.get('/posts/:id/comments', (req, res) => {
return res.send(commentsByPostId[req.params.id] || []);
});

// Endpoint untuk menambahkan komentar ke sebuah post
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',
});
comments.push({ id: commentId, content, status: 'pending' });

commentsByPostId[req.params.id] = comments;

// Kirim event ke event bus
await axios.post('http://localhost:4005/events', {
type: 'CommentCreated',
data: {
id: commentId,
content,
postId: req.params.id,
status: 'pending',
},
});
data: { id: commentId, content, postId: req.params.id, status: 'pending' },
}).catch(err => console.error('Error sending CommentCreated event:', err.message));

return res.status(201).send(comments);
});

app.post('/events', async (req, res) => {
console.log('Event Received', 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) => {
return comment.id === id;
});

comment.status = status;

await axios.post('http://localhost:4005/events', {
type: 'CommentUpdated',
data: {
id,
status,
postId,
content,
},
});
}

// Endpoint untuk menerima event dari event bus
app.post('/events', (req, res) => {
console.log('Event Received:', req.body.type);
handleEvent(req.body);
return res.send({});
});

app.listen(4001, () => {
// Start server dan replay event dari event service saat startup
app.listen(4001, async () => {
console.log('Listening on 4001');

try {
const res = await axios.get('http://localhost:4005/events');

for (let event of res.data) {
console.log('Processing event:', event.type);
handleEvent(event);
}
} catch (error) {
console.error('Error fetching events:', error.message);
}
});
Loading