Skip to content

Instantly share code, notes, and snippets.

@tanvirstreame
Created May 18, 2024 15:36
Show Gist options
  • Save tanvirstreame/2959911be133a1315e71f6a5ffe89031 to your computer and use it in GitHub Desktop.
Save tanvirstreame/2959911be133a1315e71f6a5ffe89031 to your computer and use it in GitHub Desktop.
class User {
registerUser(userId, userName) {
this.userId = userId;
this.userName = userName;
return "User Created";
}
get() {
return {
userId: this.userId,
userName: this.userName
};
}
}
class Friend {
constructor(follower, followee) {
this.follower = follower;
this.followee = followee;
}
addFriend(userId, friendId) {
this.friends.push(userId, friendId)
this.friends.push(friendId, userId)
return "Added as friend"
}
removeFriend(userId, friendId) {
this.friends = this.friends.filter(friend => !(friend.followee === userId && friend.follower === friendId));
this.friends = this.friends.filter(friend => !(friend.followee === friendId && friend.follower === userId));
return "Unfriended successfully";
}
get() {
return this.friends
}
}
class Post {
constructor(userId, posts) {
this.userId = userId;
this.posts = posts;
this.postList = []
this.friends = []
}
addPost(userId, posts) {
this.postList.push(userId, posts);
return "Added post successfully";
}
getFriendPosts(userId) {
const friend = new Friend();
this.friends.push(friend.get())
const friendList = this.friends.filter(eachUser => eachUser.follower === userId).map(friend => friend.followee)
return this.postList.filter(post => friendList.includes(post.userId))
}
searchFriendPosts(userId, search) {
const friendList = this.friends.filter(eachUser => eachUser.follower === userId).map(friend => friend.followee)
const postList = this.postList.filter(post => friendList.includes(post.userId))
return postList.filter(post => post.posts.toLowerCase().includes(search.toLowerCase()));
}
get() {
return this.postList;
}
}
const userList = [];
const user1 = new User();
user1.registerUser(1, 'Tanvir')
const user2 = new User();
user2.registerUser(2, 'Tanvir')
userList.push(user1.get());
userList.push(user2.get());
const userInfo = user1.get();
console.log(userList,"userList")
// const friend = new Friend();
// friend.addFriend(1, 2);
// friend.removeFriend(1, 2)
// friend.get();
// const post = new Post()
// post.addPost(1, "bangal")
// post.addPost(2, "banal")
// post.getFriendPosts(1)
// post.searchFriendPosts(1, "banal")
// post.get();
// console.log("user", user.get());
// console.log("friend", friend.get());
// console.log("post", post.get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment