Skip to content

Commit

Permalink
Create forum.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent bdfefa0 commit 874ef92
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/community/forum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// community/forum.js
class Forum {
constructor() {
this.posts = []; // Store forum posts
}

createPost(title, content, author) {
const post = {
id: this.posts.length + 1,
title: title,
content: content,
author: author,
comments: [],
createdAt: new Date(),
};
this.posts.push(post);
console.log(`Post created: ${title}`);
return post;
}

getPosts() {
return this.posts;
}

getPostById(postId) {
const post = this.posts.find(p => p.id === postId);
if (!post) {
throw new Error('Post not found.');
}
return post;
}

addComment(postId, commentContent, author) {
const post = this.getPostById(postId);
const comment = {
id: post.comments.length + 1,
content: commentContent,
author: author,
createdAt: new Date(),
};
post.comments.push(comment);
console.log(`Comment added to post ${postId}: ${commentContent}`);
return comment;
}
}

module.exports = Forum;

0 comments on commit 874ef92

Please sign in to comment.