Allow one to specify that a field of a model is a foreign key. It will generate a bunch of helper methods to query related entities from one entity.
95 lines
2.7 KiB
Rust
95 lines
2.7 KiB
Rust
use anyhow::{Context, Result};
|
|
|
|
use chrono::Utc;
|
|
use sqlx::types::Json;
|
|
use sqlxgentools_misc::ForeignRef;
|
|
|
|
use crate::{db::provide_database, models::user::{User, UserToken}, repositories::user_token_repository::UserTokenRepository};
|
|
|
|
pub mod models;
|
|
pub mod repositories;
|
|
pub mod db;
|
|
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
println!("Sandbox");
|
|
|
|
let users = vec![
|
|
User {
|
|
id: "idu1".into(),
|
|
handle: "john.doe".into(),
|
|
full_name: None,
|
|
prefered_color: None,
|
|
last_login_at: None,
|
|
status: models::user::UserStatus::Invited,
|
|
groups: Json(vec![]),
|
|
avatar_bytes: None
|
|
},
|
|
User {
|
|
id: "idu2".into(),
|
|
handle: "richard".into(),
|
|
full_name: None,
|
|
prefered_color: None,
|
|
last_login_at: None,
|
|
status: models::user::UserStatus::Invited,
|
|
groups: Json(vec![]),
|
|
avatar_bytes: None
|
|
},
|
|
User {
|
|
id: "idu3".into(),
|
|
handle: "manned".into(),
|
|
full_name: None,
|
|
prefered_color: None,
|
|
last_login_at: None,
|
|
status: models::user::UserStatus::Invited,
|
|
groups: Json(vec![]),
|
|
avatar_bytes: None
|
|
}
|
|
];
|
|
let user_token = UserToken {
|
|
id: "idtoken1".into(),
|
|
secret: "4LP5A3F3XBV5NM8VXRGZG3QDXO9PNAC0".into(),
|
|
last_use_time: None,
|
|
creation_time: Utc::now(),
|
|
expiration_time: Utc::now(),
|
|
user_id: ForeignRef::new(&users.get(0).unwrap())
|
|
};
|
|
|
|
let db = provide_database("tmp/db.db").await?;
|
|
|
|
let user_token_repo = UserTokenRepository::new(db);
|
|
user_token_repo.insert_many(&vec![
|
|
UserToken {
|
|
id: "idtoken2".into(),
|
|
secret: "4LP5A3F3XBV5NM8VXRGZG3QDXO9PNAC0".into(),
|
|
last_use_time: None,
|
|
creation_time: Utc::now(),
|
|
expiration_time: Utc::now(),
|
|
user_id: ForeignRef::new(&users.get(0).unwrap())
|
|
},
|
|
UserToken {
|
|
id: "idtoken3".into(),
|
|
secret: "CBHR6G41KSEMR1AI".into(),
|
|
last_use_time: None,
|
|
creation_time: Utc::now(),
|
|
expiration_time: Utc::now(),
|
|
user_id: ForeignRef::new(&users.get(1).unwrap())
|
|
},
|
|
UserToken {
|
|
id: "idtoken4".into(),
|
|
secret: "CBHR6G41KSEMR1AI".into(),
|
|
last_use_time: None,
|
|
creation_time: Utc::now(),
|
|
expiration_time: Utc::now(),
|
|
user_id: ForeignRef::new(&users.get(1).unwrap())
|
|
}
|
|
]).await?;
|
|
let user_tokens = user_token_repo.get_many_user_tokens_by_usersss(
|
|
vec!["idu2".into()]
|
|
).await?;
|
|
dbg!(&user_tokens);
|
|
|
|
Ok(())
|
|
}
|
|
|