-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.ts
238 lines (232 loc) · 5.49 KB
/
questions.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import inquirer from "inquirer";
import { Db } from "mongodb";
import { countAdmin, usernameExists } from "./db";
interface FirstAnswer {
action: "Sign In" | "Sign Up" | "Exit";
username: string;
password: string;
}
interface AdminAnswers {
cmd: string;
bookName?: string;
bookISBN?: string;
bookCount?: string;
studentUsername?: string;
}
interface StudentAnswers {
cmd: string;
bookISBN: string;
}
export const firstQuestion = (db: Db) =>
inquirer.prompt<FirstAnswer>([
{
// 用户在这里选择进行什么操作
type: "list",
name: "action",
message: "What do you want?",
choices: ["Sign In", "Sign Up", "Exit"],
async when() {
return (await countAdmin(db)) > 0;
},
},
{
// 用户输入用户名 登录
type: "input",
name: "username",
message: "[Sign In] Username:",
when(answers) {
return answers.action === "Sign In";
},
async validate(username) {
if (username && !(await usernameExists(db, username))) {
return "Username not exists!";
}
return true;
},
},
{
// 输入密码 登录
type: "password",
name: "password",
message: "[Sign In] Password:",
when(answers) {
return !!answers.username && answers.action === "Sign In";
},
},
{
// 输入用户名 注册
type: "input",
name: "username",
message: "[Sign Up] Username:",
when(answers) {
return answers.action === "Sign Up";
},
async validate(username) {
if (username && (await usernameExists(db, username))) {
return "Username exists!";
}
return true;
},
},
{
// 输入密码 注册
type: "password",
name: "password",
message: "[Sign Up] Password:",
when(answers) {
return !!answers.username && answers.action === "Sign Up";
},
},
{
// 输入用户名 配置系统
type: "input",
name: "username",
message: "[Set Up] Root Username:",
when(answers) {
return !answers.action;
},
async validate(username) {
if (username && (await usernameExists(db, username))) {
return "Username exists!";
}
return true;
},
},
{
// 输入密码 配置系统
type: "password",
name: "password",
message: "[Set Up] Root Password:",
when(answers) {
return !!answers.username && !answers.action;
},
},
]);
export const adminQuestion = (db: Db) =>
inquirer.prompt<AdminAnswers>([
{
// 选择进行何种操作
type: "list",
name: "cmd",
message: "Select what will do:",
choices: [
"Add books",
"Remove books",
"See student info",
"See book info",
"Log out",
],
},
{
// 输入书名
type: "input",
name: "bookName",
message: "[Add Books] Book Name:",
when(answers) {
return answers.cmd === "Add books";
},
},
{
// 输入书籍的ISBN
type: "input",
name: "bookISBN",
message: "[Add Books] Book ISBN:",
when(answers) {
return !!answers.bookName && answers.cmd === "Add books";
},
},
{
// 输入要添加的书的数量
type: "input",
name: "bookCount",
message: "[Add Books] Book Count:",
when(answers) {
return !!answers.bookISBN && answers.cmd === "Add books";
},
validate(count) {
if (!count) {
return true;
}
return isNaN(count) ? "Book count invalid!" : true;
},
},
{
// 移除书籍的ISBN
type: "input",
name: "bookISBN",
message: "[Remove Books] Book ISBN:",
when(answers) {
return answers.cmd === "Remove books";
},
},
{
// 移除数量
type: "input",
name: "bookCount",
message: "[Remove Books] Book Count:",
when(answers) {
return !!answers.bookISBN && answers.cmd === "Remove books";
},
validate(count) {
if (!count) {
return true;
}
return isNaN(count) ? "Book count invalid!" : true;
},
},
{
// 学生用户名
type: "input",
name: "studentUsername",
message: "[See student info] Student Username:",
when(answers) {
return answers.cmd === "See student info";
},
async validate(username) {
if (username && !(await usernameExists(db, username))) {
return "User not exists!";
}
return true;
},
},
{
// 要查看的书的信息
type: "input",
name: "bookISBN",
message: "[See student info] Book ISBN:",
when(answers) {
return answers.cmd === "See book info";
},
},
]);
export const studentQuestion = () =>
inquirer.prompt<StudentAnswers>([
{
// 进行何种操作
type: "list",
name: "cmd",
message: "Select what will do:",
choices: [
"List books I borrowed",
"Borrow book",
"Return book",
"Log out",
],
},
{
type: "input",
name: "bookISBN",
message: "[Borrow book] Book ISBN:",
when(answers) {
return answers.cmd === "Borrow book";
},
},
{
type: "input",
name: "bookISBN",
message: "[Return book] Book ISBN:",
when(answers) {
return answers.cmd === "Return book";
},
},
]);