proper station code creation; Fixes #3
All checks were successful
CI/CD Pipeline / test (push) Successful in 4m8s
CI/CD Pipeline / deploy (push) Successful in 2m55s

This commit is contained in:
Philipp Hofer 2025-04-14 15:12:15 +02:00
parent da33c0411d
commit 0ed0928a7b
4 changed files with 18 additions and 2 deletions

1
Cargo.lock generated
View File

@ -2233,6 +2233,7 @@ dependencies = [
"futures", "futures",
"maud", "maud",
"password-auth", "password-auth",
"rand 0.9.0",
"rust-i18n", "rust-i18n",
"serde", "serde",
"sqlx", "sqlx",

View File

@ -21,6 +21,7 @@ password-auth = "1.0"
tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["sqlite"] } tower-sessions-sqlx-store-chrono = { version = "0.14", features = ["sqlite"] }
tracing-subscriber = "0.3" tracing-subscriber = "0.3"
futures = "0.3" futures = "0.3"
rand = "0.9"
[dev-dependencies] [dev-dependencies]

View File

@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS station (
amount_people INTEGER, amount_people INTEGER,
last_login DATETIME, last_login DATETIME,
ready BOOLEAN NOT NULL DEFAULT false, ready BOOLEAN NOT NULL DEFAULT false,
pw TEXT NOT NULL DEFAULT (upper(hex(randomblob(4)))), pw TEXT NOT NULL,
lat REAL, lat REAL,
lng REAL lng REAL
); );

View File

@ -6,6 +6,11 @@ use crate::{
}; };
use axum::Router; use axum::Router;
use chrono::{DateTime, Local, NaiveDateTime, Utc}; use chrono::{DateTime, Local, NaiveDateTime, Utc};
use rand::{
distr::{Distribution, Uniform},
rng,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool}; use sqlx::{FromRow, SqlitePool};
@ -78,8 +83,17 @@ impl Station {
.unwrap(); .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> { 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) .execute(db)
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;