-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
289 lines (280 loc) · 8.25 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import program from "commander";
import { MongoClient } from "mongodb";
import { table, getBorderCharacters } from "table";
import {
register,
init,
login,
User,
addBooks,
removeBooks,
list,
borrowBook,
returnBook,
studentInfo,
bookInfo,
} from "./db";
import { firstQuestion, adminQuestion, studentQuestion } from "./questions";
program
.version("0.1.0")
.usage("[options] <file ...>")
.option("-h, --hostname <s>", "Hostname of database")
.option("-p, --port <n>", "Port of database", parseInt)
.option("-d, --database <s>", "Database name")
.parse(process.argv);
program.hostname = program.hostname || "39.108.175.151";
program.port = program.port || "4001";
program.database = program.database || "library";
(async function() {
// 链接数据库
const { hostname, port, database } = program;
const uri = `mongodb://${hostname}:${port}/${database}`;
const conn = await MongoClient.connect(
uri,
{ useNewUrlParser: true },
);
const db = (await conn).db();
// 初始化数据库
await init(db);
console.log("Welcome To Library Management System!");
// 等待用户选择登录、注册、退出中的一个
while (true) {
const { action, username, password } = await firstQuestion(db);
if (action === "Exit") {
// 退出
await conn.close();
process.exit(0);
return;
}
if (!username || !password) {
continue;
}
try {
switch (action) {
// 登录时
case "Sign In": {
await loop(
await login(db, username, password).then(user => {
console.log(`Ok: "${username}" logged in.`);
return user;
}),
);
break;
}
case "Sign Up": {
// 注册时
await loop({
_id: await register(db, "Student", username, password).then(_id => {
console.log(`OK: "${username}" signned up.`);
return _id;
}),
username,
role: "Student",
});
break;
}
default: {
// 默认情况下,设为配置该系统,设置管理员账号和密码
await loop({
_id: await register(db, "Admin", username, password).then(_id => {
console.log(`OK: "${username}" signned up.`);
return _id;
}),
username,
role: "Admin",
});
break;
}
}
} catch (e) {
console.log(`Error: ${e.message}`);
continue;
}
}
async function loop(user: User) {
if (user.role === "Admin") {
let answers;
do {
answers = await adminQuestion(db);
const { bookName, bookISBN, bookCount, studentUsername } = answers;
try {
switch (answers.cmd) {
// 添加书籍
case "Add books": {
if (!bookName || !bookISBN || !bookCount) {
continue;
}
if (
await addBooks(
db,
user._id,
bookName!,
bookISBN!,
Number(bookCount!),
)
) {
console.log(`OK: ${bookCount} books added.`);
continue;
}
}
// 移除书籍
case "Remove books": {
if (!bookISBN || !bookCount) {
continue;
}
const [removed, rest] = await removeBooks(
db,
bookISBN!,
Number(bookCount!),
);
console.log(`OK: ${removed} books removed, ${rest} remain.`);
continue;
}
// 查看学生信息
case "See student info": {
if (!studentUsername) {
continue;
}
const { username, role, record } = await studentInfo(
db,
studentUsername!,
);
console.log(`Username: ${username}, Role: ${role}`);
console.log(`Borrow & Return Records:`);
console.log(
table(
[
["No.", "Book Name", "Book ISBN", "Operation", "Time"],
...record.map(
({ bookName, bookISBN, operation, time }, i) => [
i + 1,
bookName,
bookISBN,
operation,
time.toLocaleString(),
],
),
],
{ border: getBorderCharacters("norc") },
),
);
break;
}
// 查看书籍信息
case "See book info": {
if (!bookISBN) {
continue;
}
const { borrowed, all, record, bookName } = await bookInfo(
db,
bookISBN!,
);
console.log(
`Book Name: ${bookName}, Amount: ${all}, Borrowed: ${borrowed}`,
);
console.log(`Borrow & Return Records:`);
console.log(
table(
[
["No.", "Username", "Operation", "Time"],
...record.map(({ username, operation, time }, i) => [
i + 1,
username,
operation,
time.toLocaleString(),
]),
],
{ border: getBorderCharacters("norc") },
),
);
break;
}
// 退出登录
case "Log out":
return;
}
} catch (e) {
console.log(`Error: ${e.message}`);
continue;
}
} while (answers);
} else if (user.role === "Student") {
let answers;
do {
answers = await studentQuestion();
const { cmd, bookISBN } = answers;
try {
switch (cmd) {
// 列出所有我的书籍
case "List books I borrowed": {
const data = (await list(db, user._id)).map(
({ bookName, bookISBN, record }, i) => {
const { time } = record.reverse().find(record => {
if (record.operation === "borrow") {
return record.operator.equals(user._id);
}
return false;
})!;
return [
i + 1,
bookName,
bookISBN,
time.toLocaleString(),
new Date(+time + 1000 * 3600 * 24 * 30).toLocaleString(),
];
},
);
if (data.length <= 0) {
console.log("Error: You havn't borrowed book yet!");
break;
}
data.unshift([
"No.",
"Book Name",
"Book ISBN",
"Borrowed Time",
"Return DDL",
]);
console.log(
table(data, {
border: getBorderCharacters("norc"),
}),
);
break;
}
// 借书
case "Borrow book": {
if (!bookISBN) {
continue;
}
const book = await borrowBook(db, user._id, bookISBN);
if (book) {
console.log(`OK: You borrowed "${book.bookName}" just now.`);
}
break;
}
// 还书
case "Return book": {
if (!bookISBN) {
continue;
}
const book = await returnBook(db, user._id, bookISBN);
if (book) {
console.log(`OK: You returned "${book.bookName}" just now.`);
}
break;
}
// 登出
case "Log out": {
return;
}
}
} catch (e) {
console.log(`Error: ${e.message}`);
}
} while (answers);
} else {
return;
}
}
})();