Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create admin in db from CLI #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
url = "^2.3"
url = "^2.4"
clap = { version = "^4.3", features = ["cargo"] }

portfolio-entity = { path = "../entity" }
Expand Down
53 changes: 47 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clap::{arg, ArgAction, ArgMatches, command, Command, value_parser};
use sea_orm::{Database, DatabaseConnection, DbConn};
use url::Url;

use portfolio_core::{crypto, Query};
use portfolio_core::services::portfolio_service::{FileType};
use portfolio_core::{crypto, Query, Mutation};
use portfolio_core::services::portfolio_service::FileType;
use portfolio_core::utils::csv::{ApplicationCsv, CsvExporter};

async fn get_admin_private_key(db: &DbConn, sub_matches: &ArgMatches) -> Result<String, Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -84,7 +84,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
arg!(
-a --admin_id <ADMIN_ID> "Admin ID"
)
.required(false),
.required(false)
.value_parser(value_parser!(i32)),
)
)
.subcommand(
Expand Down Expand Up @@ -128,7 +129,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
arg!(
-a --admin_id <ADMIN_ID> "Admin ID"
)
.required(false),
.required(false)
.value_parser(value_parser!(i32)),
)
)
.subcommand(
Expand Down Expand Up @@ -177,7 +179,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
arg!(
-a --admin_id <ADMIN_ID> "Admin ID"
)
.required(false),
.required(false)
.value_parser(value_parser!(i32)),
)
)
.subcommand(
Expand Down Expand Up @@ -244,8 +247,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
-p --password <PASWORD> "Password"
)
.required(true)
),
.value_parser(value_parser!(String))
)
.subcommand(
Command::new("db")
.about("Write to database")
.arg(
arg!(
-d --database <URL> "URL to the database or sql file with postgres:// or sqlite://"
)
.alias("url")
.required(true)
.value_parser(value_parser!(Url)),
)
// arg for id
.arg(
arg!(
-i --id <ID> "Admin ID"
)
.required(true)
.value_parser(value_parser!(i32))
)
.arg(
arg!(
-n --name <NAME> "Admin name"
)
.required(true)
.value_parser(value_parser!(String))
)
)
)
//
.get_matches();

match clap.subcommand() {
Expand Down Expand Up @@ -355,6 +387,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap();


if let Some(sub_matches) = sub_matches.subcommand_matches("db") {
let db = get_db_conn(sub_matches).await?;
let admin_id = sub_matches.get_one::<i32>("id").unwrap().to_owned();
let admin_name = sub_matches.get_one::<String>("name").unwrap().to_owned();
Mutation::set_admin(&db, admin_id, admin_name, pubkey.clone(), priv_key.clone(), password_hash.clone())
.await?;
println!("Admin {} created", admin_id);
}

println!("{}", pubkey);
println!("{}", priv_key);
println!("{}", password_hash);
Expand Down
39 changes: 39 additions & 0 deletions core/src/database/mutation/admin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use chrono::Utc;
use entity::admin;
use sea_orm::{DbConn, DbErr, Set, ActiveModelTrait, EntityTrait};

use crate::Mutation;

impl Mutation {
pub async fn set_admin(
db: &DbConn,
admin_id: i32,
name: String,
public_key: String,
private_key: String,
password: String,
) -> Result<admin::Model, DbErr> {
let admin_exists = admin::Entity::find_by_id(admin_id)
.one(db)
.await
.expect("Db Error");

let admin = admin::ActiveModel {
id: Set(admin_id),
name: Set(name),
public_key: Set(public_key),
private_key: Set(private_key),
password: Set(password),
created_at: Set(Utc::now().naive_local()),
updated_at: Set(Utc::now().naive_local())
};

if admin_exists.is_some() {
admin.update(db).await
} else {
admin.insert(db).await
}
}


}
3 changes: 2 additions & 1 deletion core/src/database/mutation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub(crate) struct Mutation;
pub struct Mutation;

pub mod admin;
pub mod application;
pub mod session;
pub mod candidate;
Expand Down