proper station code creation; Fixes #3
This commit is contained in:
parent
da33c0411d
commit
0ed0928a7b
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2233,6 +2233,7 @@ dependencies = [
|
||||
"futures",
|
||||
"maud",
|
||||
"password-auth",
|
||||
"rand 0.9.0",
|
||||
"rust-i18n",
|
||||
"serde",
|
||||
"sqlx",
|
||||
|
@ -21,6 +21,7 @@ password-auth = "1.0"
|
||||
tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["sqlite"] }
|
||||
tracing-subscriber = "0.3"
|
||||
futures = "0.3"
|
||||
rand = "0.9"
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
|
@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS station (
|
||||
amount_people INTEGER,
|
||||
last_login DATETIME,
|
||||
ready BOOLEAN NOT NULL DEFAULT false,
|
||||
pw TEXT NOT NULL DEFAULT (upper(hex(randomblob(4)))),
|
||||
pw TEXT NOT NULL,
|
||||
lat REAL,
|
||||
lng REAL
|
||||
);
|
||||
|
@ -6,6 +6,11 @@ use crate::{
|
||||
};
|
||||
use axum::Router;
|
||||
use chrono::{DateTime, Local, NaiveDateTime, Utc};
|
||||
use rand::{
|
||||
distr::{Distribution, Uniform},
|
||||
rng,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, SqlitePool};
|
||||
|
||||
@ -78,8 +83,17 @@ impl Station {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn generate_random_alphanumeric(length: usize) -> String {
|
||||
let mut rng = rng();
|
||||
let chars: Vec<char> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();
|
||||
let dist = Uniform::new(0, chars.len()).unwrap();
|
||||
|
||||
(0..length).map(|_| chars[dist.sample(&mut rng)]).collect()
|
||||
}
|
||||
|
||||
pub(crate) async fn create(db: &SqlitePool, name: &str) -> Result<(), String> {
|
||||
sqlx::query!("INSERT INTO station(name) VALUES (?)", name)
|
||||
let code = Self::generate_random_alphanumeric(8);
|
||||
sqlx::query!("INSERT INTO station(name, pw) VALUES (?, ?)", name, code)
|
||||
.execute(db)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user