-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-crud-with-json.ts
130 lines (108 loc) · 3.42 KB
/
console-crud-with-json.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as readline from 'readline';
// Interface for User data structure
interface User {
id: number;
name: string;
email: string;
}
// Our simulated "JSON database" (in-memory array)
let users: User[] = [];
// Global variable to track the next ID for new users
let nextId = 1;
// Function to create a new user
function createUser() {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('Enter name: ', (name) => {
rl.question('Enter email: ', (email) => {
const newUser: User = { id: nextId++, name, email };
users.push(newUser);
console.log(`User created successfully!`);
rl.close();
});
});
}
// Function to read all existing users
function readUsers() {
if (!users.length) {
console.log('No users found.');
return;
}
console.log('List of Users:');
users.forEach((user) => {
console.log(`ID: ${user.id}, Name: ${user.name}, Email: ${user.email}`);
});
}
// Function to update an existing user
function updateUser() {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('Enter the ID of the user to update: ', (id) => {
const userId = parseInt(id);
const existingUserIndex = users.findIndex((user) => user.id === userId);
if (existingUserIndex === -1) {
console.log('User not found.');
rl.close();
return;
}
rl.question('Enter new name (press Enter to keep existing): ', (newName) => {
const newEmail = newName.trim() ? newName : users[existingUserIndex].name;
rl.question('Enter new email (press Enter to keep existing): ', (newEmailInput) => {
const newEmail = newEmailInput.trim() ? newEmailInput : users[existingUserIndex].email;
users[existingUserIndex] = { id: userId, name: newEmail, email: newEmail };
console.log('User updated successfully!');
rl.close();
});
});
});
}
// Function to delete a user
function deleteUser() {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('Enter the ID of the user to delete: ', (id) => {
const userId = parseInt(id);
const existingUserIndex = users.findIndex((user) => user.id === userId);
if (existingUserIndex === -1) {
console.log('User not found.');
rl.close();
return;
}
users.splice(existingUserIndex, 1);
console.log('User deleted successfully!');
rl.close();
});
}
// Main function to display the menu and handle user input
function mainMenu() {
console.log('\nUser CRUD');
console.log('1. Create User');
console.log('2. Read Users');
console.log('3. Update User');
console.log('4. Delete User');
console.log('5. Exit');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('Enter your choice: ', (choice) => {
const option = parseInt(choice);
switch (option) {
case 1:
createUser();
break;
case 2:
readUsers();
break;
case 3:
updateUser();
break;
case 4:
deleteUser();
break;
case 5:
console.log('Exiting...');
rl.close();
process.exit(0); // Exit the program gracefully
default:
console.log('Invalid choice.');
}
rl.close(); // Close the readline interface after each option selection
});
}
// Start the program by displaying the main menu
mainMenu();