minor tidy

This commit is contained in:
Joe Ardent 2023-07-29 12:27:12 -07:00
parent b8481dc106
commit 71fae193be
4 changed files with 20 additions and 28 deletions

View file

@ -5,7 +5,7 @@ use julid::Julid;
use rand::{seq::SliceRandom, thread_rng, Rng}; use rand::{seq::SliceRandom, thread_rng, Rng};
use rand_distr::Normal; use rand_distr::Normal;
use sqlx::{ use sqlx::{
query_as, query_scalar,
sqlite::{SqliteConnectOptions, SqlitePoolOptions}, sqlite::{SqliteConnectOptions, SqlitePoolOptions},
SqlitePool, SqlitePool,
}; };
@ -14,7 +14,7 @@ use tokio_retry::Retry;
use what2watch::{ use what2watch::{
get_db_pool, get_db_pool,
import_utils::{add_omega_watches, add_users, add_watch_quests}, import_utils::{add_omega_watches, add_users, add_watch_quests},
User, Watch, WatchQuest, User, WatchQuest,
}; };
fn main() { fn main() {
@ -53,10 +53,9 @@ fn main() {
.block_on(add_omega_watches(&w2w_db, &movie_db, 10_000)) .block_on(add_omega_watches(&w2w_db, &movie_db, 10_000))
.unwrap(); .unwrap();
let movies: Vec<Watch> = rt let movies: Vec<Julid> = rt
.block_on(query_as("select * from watches").fetch_all(&w2w_db)) .block_on(query_scalar("select id from watches").fetch_all(&w2w_db))
.unwrap(); .unwrap();
let movies: Vec<Julid> = movies.into_iter().map(|m| m.id).collect();
let rng = &mut thread_rng(); let rng = &mut thread_rng();

View file

@ -25,7 +25,7 @@ pub struct ImportMovieOmega {
impl From<ImportMovieOmega> for Watch { impl From<ImportMovieOmega> for Watch {
fn from(value: ImportMovieOmega) -> Self { fn from(value: ImportMovieOmega) -> Self {
Watch { Watch {
id: Julid::omega(), // this is ignored by the inserter id: OMEGA_ID, // this is ignored by the inserter
title: value.title, title: value.title,
release_date: year_to_epoch(value.year.as_deref()), release_date: year_to_epoch(value.year.as_deref()),
length: value.length.and_then(|v| v.parse::<i64>().ok()), length: value.length.and_then(|v| v.parse::<i64>().ok()),
@ -39,7 +39,7 @@ impl From<ImportMovieOmega> for Watch {
impl From<&ImportMovieOmega> for Watch { impl From<&ImportMovieOmega> for Watch {
fn from(value: &ImportMovieOmega) -> Self { fn from(value: &ImportMovieOmega) -> Self {
Watch { Watch {
id: Julid::omega(), id: OMEGA_ID,
title: value.title.to_string(), title: value.title.to_string(),
release_date: year_to_epoch(value.year.as_deref()), release_date: year_to_epoch(value.year.as_deref()),
length: value.length.as_ref().and_then(|v| v.parse::<i64>().ok()), length: value.length.as_ref().and_then(|v| v.parse::<i64>().ok()),
@ -127,15 +127,14 @@ pub async fn add_omega_watches(
pub async fn ensure_omega(db_pool: &SqlitePool) -> Julid { pub async fn ensure_omega(db_pool: &SqlitePool) -> Julid {
if !check_omega_exists(db_pool).await { if !check_omega_exists(db_pool).await {
sqlx::query("insert into users (id, username, pwhash) values (?, 'the omega user', 'you shall not password')").bind(Julid::omega()).execute(db_pool).await.unwrap(); sqlx::query("insert into users (id, username, pwhash) values (?, 'the omega user', 'you shall not password')").bind(OMEGA_ID).execute(db_pool).await.unwrap();
} }
OMEGA_ID OMEGA_ID
} }
async fn check_omega_exists(db_pool: &SqlitePool) -> bool { async fn check_omega_exists(db_pool: &SqlitePool) -> bool {
let id = Julid::omega();
let count = query_scalar(USER_EXISTS_QUERY) let count = query_scalar(USER_EXISTS_QUERY)
.bind(id) .bind(OMEGA_ID)
.fetch_one(db_pool) .fetch_one(db_pool)
.await .await
.unwrap_or(0); .unwrap_or(0);

View file

@ -109,12 +109,10 @@ pub async fn post_create_user(
let email = validate_optional_length(&signup.email, 5..30, CreateUserErrorKind::BadEmail)?; let email = validate_optional_length(&signup.email, 5..30, CreateUserErrorKind::BadEmail)?;
let user = create_user(username, &displayname, &email, password, &pool).await?; let user = create_user(username, &displayname, &email, password, &pool).await?;
let when = user.id.created_at();
tracing::debug!("created {user:?} at {when}");
tracing::debug!("created {user:?}"); let resp = axum::response::Redirect::to(&format!("/signup_success/{}", user.id));
let id = user.id.as_string();
let location = format!("/signup_success/{id}");
let resp = axum::response::Redirect::to(&location);
Ok(resp) Ok(resp)
} }
@ -172,23 +170,20 @@ pub(crate) async fn create_user(
.fetch_one(pool) .fetch_one(pool)
.await; .await;
match res { Ok(res.map_err(|e| {
Ok(user) => Ok(user), match e {
Err(sqlx::Error::Database(db)) => { sqlx::Error::Database(db) => {
if let Some(exit) = db.code() { let exit = db.code().unwrap_or_default().parse().unwrap_or(0);
let exit = exit.parse().unwrap_or(0u32);
// https://www.sqlite.org/rescode.html codes for unique constraint violations: // https://www.sqlite.org/rescode.html codes for unique constraint violations:
if exit == 2067u32 || exit == 1555 { if exit == 2067u32 || exit == 1555 {
Err(CreateUserErrorKind::AlreadyExists.into()) CreateUserErrorKind::AlreadyExists
} else { } else {
Err(CreateUserErrorKind::UnknownDBError.into()) CreateUserErrorKind::UnknownDBError
}
} else {
Err(CreateUserErrorKind::UnknownDBError.into())
} }
} }
_ => Err(CreateUserErrorKind::UnknownDBError.into()), _ => CreateUserErrorKind::UnknownDBError,
} }
})?)
} }
//-************************************************************************ //-************************************************************************

View file

@ -27,12 +27,11 @@ pub struct User {
impl Debug for User { impl Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("User") f.debug_struct("User")
.field("id", &self.id.as_string())
.field("username", &self.username) .field("username", &self.username)
.field("id", &self.id.as_string())
.field("displayname", &self.displayname) .field("displayname", &self.displayname)
.field("email", &self.email) .field("email", &self.email)
.field("last_seen", &self.last_seen) .field("last_seen", &self.last_seen)
.field("pwhash", &"<redacted>")
.finish() .finish()
} }
} }