create and list user examples
This commit is contained in:
parent
fca4582084
commit
5ac172aff8
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1573,6 +1573,7 @@ dependencies = [
|
||||
"axum",
|
||||
"barrel",
|
||||
"dotenvy",
|
||||
"entity",
|
||||
"mime_guess",
|
||||
"minijinja",
|
||||
"minijinja-autoreload",
|
||||
|
@ -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",
|
||||
|
7
Justfile
7
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
|
||||
|
@ -1 +1,5 @@
|
||||
pub mod user;
|
||||
pub mod user;
|
||||
pub mod permission;
|
||||
|
||||
pub use user::Entity as User;
|
||||
pub use permission::Entity as Permission;
|
||||
|
@ -9,7 +9,7 @@ pub struct Model {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub description: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
|
@ -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<entity::user::Model, DbErr> {
|
||||
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);
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user