-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.js
102 lines (84 loc) · 2.9 KB
/
blog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import fs from 'fs/promises';
import readline from 'readline';
import chalk from 'chalk';
const FILE_PATH = './data/posts.json';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function askQuestion(query) {
return new Promise((resolve) => rl.question(query, resolve));
}
async function loadPosts() {
try {
const data = await fs.readFile(FILE_PATH, 'utf-8');
return JSON.parse(data);
} catch {
return [];
}
}
async function savePosts(posts) {
await fs.writeFile(FILE_PATH, JSON.stringify(posts, null, 2));
}
export async function createPost(userName) {
console.log(`\n${chalk.blue('Enter your post content (press Enter to save):')}`);
const content = await askQuestion('> ');
const posts = await loadPosts();
posts.push({ id: Date.now(), user: userName, content });
await savePosts(posts);
console.log(chalk.green('Post created successfully!'));
}
export async function viewPosts(userName) {
const posts = await loadPosts();
const userPosts = posts.filter(post => post.user === userName);
if (userPosts.length === 0) {
console.log(chalk.yellow('No posts found for your account.'));
} else {
userPosts.forEach((post, index) => {
console.log(`\n${index + 1}. ${chalk.bold(post.content)} (ID: ${post.id})`);
});
}
}
export async function updatePost(userName) {
const posts = await loadPosts();
const userPosts = posts.filter(post => post.user === userName);
if (userPosts.length === 0) {
console.log(chalk.yellow('No posts to update.'));
return;
}
userPosts.forEach((post, index) => {
console.log(`${index + 1}. ${post.content} (ID: ${post.id})`);
});
const choice = await askQuestion('Enter the number of the post to update: ');
const postIndex = parseInt(choice) - 1;
if (postIndex >= 0 && postIndex < userPosts.length) {
const newContent = await askQuestion('Enter new content: ');
userPosts[postIndex].content = newContent;
await savePosts(posts);
console.log(chalk.green('Post updated successfully!'));
} else {
console.log(chalk.red('Invalid post number.'));
}
}
export async function deletePost(userName) {
const posts = await loadPosts();
const userPosts = posts.filter(post => post.user === userName);
if (userPosts.length === 0) {
console.log(chalk.yellow('No posts to delete.'));
return;
}
userPosts.forEach((post, index) => {
console.log(`${index + 1}. ${post.content} (ID: ${post.id})`);
});
const choice = await askQuestion('Enter the number of the post to delete: ');
const postIndex = parseInt(choice) - 1;
if (postIndex >= 0 && postIndex < userPosts.length) {
const postToDelete = userPosts[postIndex];
const updatedPosts = posts.filter(post => post.id !== postToDelete.id);
await savePosts(updatedPosts);
console.log(chalk.green('Post deleted successfully!'));
} else {
console.log(chalk.red('Invalid post number.'));
}
}
``