From 5ac172aff8e56047b4a4cd4ce904883dd86c4091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Thu, 21 Dec 2023 21:00:05 +0100 Subject: [PATCH] create and list user examples --- Cargo.lock | 1 + Cargo.toml | 1 + Justfile | 7 ++++++- entity/src/lib.rs | 6 +++++- entity/src/user.rs | 2 +- src/bin/create_user.rs | 21 ++++++++++----------- src/bin/list_users.rs | 22 +++++++++++----------- 7 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf0504e..cd18f13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1573,6 +1573,7 @@ dependencies = [ "axum", "barrel", "dotenvy", + "entity", "mime_guess", "minijinja", "minijinja-autoreload", diff --git a/Cargo.toml b/Cargo.toml index ef6a576..191f70b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ use_barrel = ["barrel", "sqlformat"] default = ["use_barrel"] [dependencies] +entity = { path = "./entity" } sea-orm = { version = "0.12.10", features = [ "runtime-tokio-native-tls", "sqlx-postgres", diff --git a/Justfile b/Justfile index 006b7a7..28fb6c2 100644 --- a/Justfile +++ b/Justfile @@ -1,3 +1,5 @@ +set dotenv-load := true + default: @echo "# Miniweb Project" @just --list @@ -32,4 +34,7 @@ dev-install: # Reset Database dev-reset: sea-orm-cli migrate reset - + +# Creates Entities from Database +db-create-entities: + sea-orm-cli generate entity -u $DATABASE_URL -o entity_generated/src --lib diff --git a/entity/src/lib.rs b/entity/src/lib.rs index 018ff2e..ce71b32 100644 --- a/entity/src/lib.rs +++ b/entity/src/lib.rs @@ -1 +1,5 @@ -pub mod user; \ No newline at end of file +pub mod user; +pub mod permission; + +pub use user::Entity as User; +pub use permission::Entity as Permission; diff --git a/entity/src/user.rs b/entity/src/user.rs index 065dea5..7af86a0 100644 --- a/entity/src/user.rs +++ b/entity/src/user.rs @@ -9,7 +9,7 @@ pub struct Model { pub id: i32, pub username: String, #[sea_orm(column_type = "Text")] - pub description: String, + pub description: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/src/bin/create_user.rs b/src/bin/create_user.rs index 7318f5e..8f1808c 100644 --- a/src/bin/create_user.rs +++ b/src/bin/create_user.rs @@ -1,6 +1,6 @@ use dotenvy::dotenv; -use miniweb::auth::models::{NewUser, User}; -use sea_orm::{DatabaseConnection, Database, DbConn}; +use entity; +use sea_orm::{DatabaseConnection, Database, DbConn, ActiveModelTrait, DbErr, Set}; use std::env; use std::io::stdin; @@ -11,14 +11,13 @@ pub async fn establish_connection() -> DatabaseConnection { db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } -pub fn create_user(conn: &DbConn, username: &str) -> User { - /* - diesel::insert_into(users::table) - .values(&new_user) - .returning(User::as_returning()) - .get_result(conn) - .expect("Error saving new user") - */ +pub async fn create_user(conn: &DbConn, username: &str) -> Result { + let user = entity::user::ActiveModel { + username: Set(username.to_owned()), + ..Default::default() + }; + let user = user.insert(conn).await?; + Ok(user) } @@ -32,6 +31,6 @@ async fn main() { stdin().read_line(&mut username).unwrap(); let username = username.trim_end(); // Remove the trailing newline - let user = create_user(&connection, username); + let user = create_user(&connection, username).await.expect("Error creating user"); println!("\nSaved user {} with id {}", username, user.id); } diff --git a/src/bin/list_users.rs b/src/bin/list_users.rs index 607949a..45292da 100644 --- a/src/bin/list_users.rs +++ b/src/bin/list_users.rs @@ -1,22 +1,22 @@ use dotenvy::dotenv; -use miniweb::auth::models::User; +use entity; +use sea_orm::{DatabaseConnection, Database, EntityTrait}; use std::env; -pub fn establish_connection() -> PgConnection { +pub async fn establish_connection() -> DatabaseConnection { dotenv().ok(); - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - PgConnection::establish(&database_url) - .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) + let db = Database::connect(&database_url).await; + db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } -fn main() { - let connection = &mut establish_connection(); - let results = users - .select(User::as_select()) - .load(connection) - .expect("Error loading posts"); +#[tokio::main] +async fn main() { + let connection = establish_connection().await; + + let results = entity::User::find() + .all(&connection).await.expect("Error loading users."); println!("Displaying {} users", results.len()); for user in results {