helper binaries

* introduce helper binaries for each table

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2019-11-18 21:57:06 +01:00
parent 4790fe5ba1
commit b5befffab8
5 changed files with 475 additions and 0 deletions

88
src/bin/get-roles.rs Normal file
View File

@@ -0,0 +1,88 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
fn main() {
use diesel::RunQueryDsl;
use advotracker_db::functions::db_connection::establish_connection;
use advotracker_db::models::roles::Role;
use advotracker_db::schema::roles::dsl::*;
let connection = establish_connection();
// direct diesel method call
match roles
//.filter(name.eq("Administrator"))
.load::<Role>(&connection)
{
Err(err) => println!("{}", err),
Ok(rows) => {
println!("Matching roles (found: {})", rows.len());
//for row in rows {
// println!("{:?}: {:?}", row.id, row.name);
//}
//for field in role_row {
// println!("{}", field.id);
// println!("{}", field.name);
//}
}
}
// function call
let vec_rows = Role::get_all(&connection);
let rows = vec_rows.iter();
println!("Matching roles (count: {})", rows.len());
for row in rows {
println!("{:?}: {:?}", row.id, row.name);
}
// get a specific role, selected by attribute "name"
//let role_name = "Hacker";
let role_name = "Sekretariat";
println!("\nRole details for: {}", role_name);
let row = Role::get(&role_name, &connection).unwrap();
/*let row = match row {
Err(err) => println!("{}", err),
// will return a vector of matching rows
Ok(row) => {
//println!("\nNumber of matching rows: {}", row.len());
println!("Role id: {:?}", row[0].id);
println!("----------");
//println!("row: {:?}", row);
println!("Role name: {:?}", row[0].name);
println!("Role date_created: {:?}", row[0].date_created);
println!("Role updated by: {:?}", row[0].id_user_changed);
println!("Role date_changed: {:?}", row[0].date_changed);
}
};
*/
if row.len() > 0 {
println!("Role name: {:?}", row[0].name);
println!("Role date_created: {:?}", row[0].date_created);
println!("Role updated by: {:?}", row[0].id_user_changed);
println!("Role date_changed: {:?}", row[0].date_changed);
}
/*
// join tables "roles" and "users"
let join = roles::table.left_join(users::table);
// By default, all columns from both tables are selected
let role_struct = join
.select((roles::name, users::full_name.nullable()))
.load::<(String, Option<String>)>(&connection)?;
println!(
"\nRole id: {} (len: {})",
role_struct.name,
role_struct.len()
);
println!("----------\n");
println!("Role name: {}", role_struct.name);
*/
println!("Finish!");
}

58
src/bin/get-users.rs Normal file
View File

@@ -0,0 +1,58 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
fn main() {
use advotracker_db::functions::db_connection::establish_connection;
use advotracker_db::models::users::User;
let connection = establish_connection();
// function call
let vec_rows = User::get_all(&connection);
let rows = vec_rows.iter();
println!("Matching roles (count: {})", rows.len());
for row in rows {
println!("{:?}: {:?}", row.id, row.last_name);
}
/*
use advotracker_database::models::User;
use advotracker_database::schema::users::dsl::*;
let results = users
.load::<User>(&connection)
.expect("Error loading users");
//.filter(email_confirmed.eq(1))
//.limit(5)
//.filter(email_confirmed.eq(Confirmed))
//.limit(5)
println!("Displaying {} users", results.len());
for user in results {
println!("{}", user.id);
println!("----------\n");
println!("{:?}", user.first_name);
println!("{:?}", user.last_name);
println!("{:?}", user.alias);
println!("{:?}", user.email);
}
*/
/*
use advotracker_database::models::Role;
let results = roles
.load::<Role>(&connection)
.expect("Error loading Roles");
println!("Displaying {} roles", results.len());
for role in results {
println!("{}", role.id);
println!("----------\n");
println!("{:?}", role.name);
}
*/
}

71
src/bin/new-harm.rs Normal file
View File

@@ -0,0 +1,71 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
fn main() {
use chrono::Local;
use std::io::{stdin, Read};
use advotracker_db::functions::db_connection::*;
use advotracker_db::models::harms::Harm;
let connection = establish_connection();
let number_harm = "123456789";
let number_policyholder = "110-123456789";
let number_callback = "0221 93123456789";
let date_fallback = Local::now().naive_local();
let date_recording = Local::now().naive_local();
// WIP: there is emptyness in the beginning
// how do we make sure, that there is a 'root' user?
// real world: this needs to be the id of the logged in user
let user_id = 1;
//let user_id_changed = 1;
println!("\nOk! I got the following:\nNumber Harm: {}", number_harm);
println!(
"Policyholder: {}, Number callback: {}",
number_policyholder, number_callback
);
println!(
"If correct, let's create this harm '{}'. (Press {} when finished)",
number_harm, EOF
);
let mut body = String::new();
stdin().read_to_string(&mut body).unwrap();
// insert as multicolon
let ret = Harm::new(
&number_harm,
Some(&number_policyholder),
Some(&number_callback),
Some(date_fallback),
date_recording,
user_id,
&connection,
);
println!("\nCreated new harm {}, returns {:?}", number_harm, ret);
// get a given harm
let harm_vec = Harm::get(number_harm, &connection);
println!("\nHarm: {:?}", harm_vec);
//let json = r#"{ "number_harm": "123456789", "number_policyholder": "110-123456789", "number_callback": "0221 92123456789", "user_id": 102, "user_id_changed": 101}"#;
//let json =
// r#"{ "number_harm": "123456789", "number_policyholder": null, "number_callback": null }"#;
//let harm_struct = serde_json::from_str::<NewHarm>(json);
//insert_into(harms).values(&harm_struct).execute(connection);
}
#[cfg(not(windows))]
const EOF: &str = "CTRL+D";
#[cfg(windows)]
const EOF: &str = "CTRL+Z";

78
src/bin/new-role.rs Normal file
View File

@@ -0,0 +1,78 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
fn main() {
use std::io::{stdin, Read};
use advotracker_db::functions::db_connection::*;
use advotracker_db::models::roles::Role;
let connection = establish_connection();
println!("Role name?");
let mut role_name = String::new();
stdin().read_line(&mut role_name).unwrap();
// Drop the newline character form our String slice
let role_name = &role_name[..(role_name.len() - 1)];
// WIP: there is emptyness in the beginning
// how do we make sure, that there is a 'root' user?
// real world: this needs to be the id of the logged in user
let user_id_changed = 1;
println!("\nOk! I got the following:\nRole Name: {}", role_name);
println!(
"If correct, let's create this role {}. (Press {} when finished)",
role_name, EOF
);
let mut body = String::new();
stdin().read_to_string(&mut body).unwrap();
// insert as multicolon
let ret = Role::new(role_name, user_id_changed, &connection);
println!("\nCreated new role {}, returns {:?}", role_name, ret);
// get a given role_name
let role_vec = Role::get(role_name, &connection);
println!("\nRole: {:?}", role_vec);
//println!(
// "\nRole parameter:\nRole name: {}\nUpdated via: {}",
// role_vec.name, role_vec.id_updated
//);
// update a given role_name
//role_id = 2;
//role_name = "New Freelancer";
//role_vec = Role::update(role_id, role_name, &connection);
//println!("\nRole: {:?}", role_vec);
let role_names = ["Administrator", "Rechtsanwalt", "Sekretariat"];
//let roles = role_names.iter();
for role_name in &role_names {
let _ = Role::new(role_name, user_id_changed, &connection);
}
//role_name = "Sekretariat";
//let _ = Role::new(role_name, user_id_changed, &connection);
/*
let inserted_count = insert_into(user_roles)
.values(&vec![
(id.eq(1), name.eq("Rechtsanwalt")),
(id.eq(2), name.eq("Administrator")),
])
.execute(&connection)?;
*/
println!("Saved Role {}", role_name);
}
#[cfg(not(windows))]
const EOF: &str = "CTRL+D";
#[cfg(windows)]
const EOF: &str = "CTRL+Z";

180
src/bin/new-user.rs Normal file
View File

@@ -0,0 +1,180 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
fn main() {
use std::io::{stdin, Read};
use advotracker_db::functions::db_connection::*;
use advotracker_db::models::users::User;
let connection = establish_connection();
/*
println!("Users first name?");
let mut last_name = String::new();
stdin().read_line(&mut last_name).unwrap();
let last_name = &last_name[..(last_name.len() - 1)]; // Drop the newline character
println!("Users last name?");
let mut first_name = String::new();
stdin().read_line(&mut first_name).unwrap();
let first_name = &first_name[..(first_name.len() - 1)];
println!("Users alias?");
let mut alias = String::new();
stdin().read_line(&mut alias).unwrap();
let alias = &alias[..(alias.len() - 1)];
println!("Users email?");
let mut email = String::new();
stdin().read_line(&mut email).unwrap();
let email = &email[..(email.len() - 1)];
let email_confirmed = 0;
println!("Users password?");
let mut password_hash = String::new();
stdin().read_line(&mut password_hash).unwrap();
let password_hash = &password_hash[..(password_hash.len() - 1)];
println!("Users initials?");
let mut initials = String::new();
stdin().read_line(&mut initials).unwrap();
let initials = &initials[..(initials.len() - 1)];
*/
let first_name = "Ralf";
let last_name = "Zerres";
let alias = "ralf.zerres";
let email = "ralf.zerres@networkx.de";
let initials = "rze";
let email_confirmed = 0;
let password_hash = "testrze";
let id_changed = 1;
println!(
"\nOk! I got the following:\nName: {} {}, Alias: {}",
first_name, last_name, alias
);
println!(
"Email-address: {}, Status: {}\nInitials: {}, Password: {}",
email, email_confirmed, initials, password_hash
);
println!(
"If correct, let's create this user {}. (Press {} when finished)",
last_name, EOF
);
let mut body = String::new();
stdin().read_to_string(&mut body).unwrap();
// insert as multicolon
let mut ret = User::new(
last_name,
Some(first_name),
Some(alias),
Some(email),
Some(email_confirmed),
password_hash,
Some(initials),
//date_create: null,
//date_updated: null,
id_changed,
&connection,
);
println!("\nCreated new user {}, returns {:?}", last_name, ret);
use serde_json::json;
// prepare full record
let json_record = json!({
"first_name": "Daniela",
"last_name": "Knocke",
"alias": "Sekretariat",
"email": "knocke@kanzlei.hiedemann.de",
"email_confirmed": 1,
"password_hash": "",
"initials": "dkn",
"date_create": null,
"date_updated": null,
"id_changed": 1,
});
println!(
"\nCreate user {} via json record:\n{}",
json_record["last_name"], json_record
);
ret = User::set_user_struct_json_option(&connection);
println!(
"Created new user {} via json-struct returns {:?}",
json_record["last_name"], ret
);
// insert as vector
/*
//connection.test_transaction::<_, Error, _>(|| {
//use diesel::select;
use schema::users::dsl::*;
//let now = select(diesel::dsl::now).get_result::<NaiveDateTime>(&connection);
//let now = select(diesel::dsl::now).get_result::<NaiveDateTime>(&connection);
let inserted_count = insert_into(users)
.values(&vec![
(last_name.eq("Networkx"), first_name.eq("Admin"), alias.eq("Networkx"),
email.eq("support@networkx.de")),
(last_name.eq("Schlömer"), first_name.eq("Lothar"), alias.eq("lothar.schlömer"),
email.eq("schlömer@kanzlei-hiedemann.de")),
])
.execute(&connection);
//});
*/
// Some data structure.
//use advotracker_db::models::users::User;
println!(
"\nCreate new json stuct representing user: {} {}",
first_name, last_name
);
let user = json!({
"first_name": first_name,
"last_name": last_name,
"alias": alias,
"email": email,
//"email_confirmed": 1,
//"password_hash": "",
"initials": initials,
//"date_create": null,
//"date_updated": null,
"id_changed": 1,
});
// Convert to a string of JSON and print it out
println!(
"json stuct representing user: {} {}",
user["last_name"],
user.to_string()
);
// Serialize it to a JSON string.
let json = serde_json::to_string(&user);
// Print, write to a file, or send to an HTTP server.
println!("Serialized output: {:?}", json);
/*
let user_id = 1;
let role_id = 1;
use advotracker_db::functions::roles::*;
let _ = set_user_role(&connection, &user_id, &role_id);
*/
}
#[cfg(not(windows))]
const EOF: &str = "CTRL+D";
#[cfg(windows)]
const EOF: &str = "CTRL+Z";