-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
72 lines (64 loc) · 2.39 KB
/
mod.rs
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
use crate::bot::core::db::client::admin_client::DatabaseAdminClient;
use crate::bot::core::db::client::DatabaseClient;
use crate::bot::core::db::connection::MyDatabaseConnection;
use crate::bot::core::db::user_representation::UserRole;
use crate::MyResult;
/// Manage the bot.
#[derive(clap::Parser)]
pub struct AdminCli {
#[command(subcommand)]
pub(crate) task: TaskCli,
}
#[derive(clap::Subcommand)]
pub enum TaskCli {
/// Show database
Show,
/// Add user
Add { user_name: String, role: UserRole },
/// Delete a user
Delete { user_name: String },
/// Link telegram id to user account
AddTelegram { start_token: String, telegram_id: i64 },
}
const PRINT_BARRIER: &str = "----------------------------------------";
fn print_header(msg: &str, with_newline: bool) {
if with_newline {
println!();
}
println!("{}", PRINT_BARRIER);
println!("{}", msg);
println!("{}", PRINT_BARRIER);
}
impl AdminCli {
pub(crate) async fn default_handling(&self) -> MyResult {
let database_connection = MyDatabaseConnection::new().await?;
let mut database_client = DatabaseClient::load(database_connection.clone()).await?;
match &self.task {
TaskCli::Show => {
print_header("List all users:", false);
let all_users = database_client.list_users().await?;
for user in all_users {
println!("{}", user);
}
print_header("List all telegram accounts:", true);
let telegram_accounts = database_client.list_telegram_accounts().await?;
for account in telegram_accounts {
println!("id={}: user_id={}", account.id, account.user_id);
}
}
TaskCli::Add { user_name, role } => {
let user = database_client.create_user(user_name, role).await?;
println!("Created user {}", user);
}
TaskCli::Delete { user_name } => {
let user = database_client.delete_user(user_name).await?;
println!("Deleted user {}", user);
}
TaskCli::AddTelegram { start_token, telegram_id } => {
let result = database_client.register_telegram_account_of_user(start_token, *telegram_id).await?;
println!("{:?}", result);
}
}
Ok(())
}
}