simplify uuid/db handling
This commit is contained in:
parent
f50abaa4a6
commit
8237715066
2 changed files with 13 additions and 6 deletions
10
src/lib.rs
10
src/lib.rs
|
@ -5,3 +5,13 @@ pub mod db;
|
||||||
pub mod generic_handlers;
|
pub mod generic_handlers;
|
||||||
pub(crate) mod templates;
|
pub(crate) mod templates;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
|
||||||
|
pub trait ToBlob {
|
||||||
|
fn blob(&self) -> &[u8];
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToBlob for uuid::Uuid {
|
||||||
|
fn blob(&self) -> &[u8] {
|
||||||
|
self.as_bytes().as_slice()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ use sqlx::{sqlite::SqliteRow, Row, SqlitePool};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::templates::CreateUser;
|
use crate::{templates::CreateUser, ToBlob};
|
||||||
|
|
||||||
const CREATE_QUERY: &str =
|
const CREATE_QUERY: &str =
|
||||||
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
|
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
|
||||||
|
@ -147,9 +147,8 @@ pub async fn handle_signup_success(
|
||||||
let user: User = {
|
let user: User = {
|
||||||
let id = id.trim();
|
let id = id.trim();
|
||||||
let id = Uuid::try_parse(id).unwrap_or_default();
|
let id = Uuid::try_parse(id).unwrap_or_default();
|
||||||
let id_bytes = id.to_bytes_le();
|
|
||||||
sqlx::query_as(ID_QUERY)
|
sqlx::query_as(ID_QUERY)
|
||||||
.bind(id_bytes.as_slice())
|
.bind(id.blob())
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
@ -181,10 +180,8 @@ async fn create_user(
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
let id_bytes = id.to_bytes_le();
|
|
||||||
let id_bytes = id_bytes.as_slice();
|
|
||||||
let res = sqlx::query(CREATE_QUERY)
|
let res = sqlx::query(CREATE_QUERY)
|
||||||
.bind(id_bytes)
|
.bind(id.blob())
|
||||||
.bind(username)
|
.bind(username)
|
||||||
.bind(displayname)
|
.bind(displayname)
|
||||||
.bind(email)
|
.bind(email)
|
||||||
|
|
Loading…
Reference in a new issue