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

View File

@ -25,7 +25,7 @@ pub struct ImportMovieOmega {
impl From<ImportMovieOmega> for Watch {
fn from(value: ImportMovieOmega) -> Self {
Watch {
id: Julid::omega(), // this is ignored by the inserter
id: OMEGA_ID, // this is ignored by the inserter
title: value.title,
release_date: year_to_epoch(value.year.as_deref()),
length: value.length.and_then(|v| v.parse::<i64>().ok()),
@ -39,7 +39,7 @@ impl From<ImportMovieOmega> for Watch {
impl From<&ImportMovieOmega> for Watch {
fn from(value: &ImportMovieOmega) -> Self {
Watch {
id: Julid::omega(),
id: OMEGA_ID,
title: value.title.to_string(),
release_date: year_to_epoch(value.year.as_deref()),
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 {
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
}
async fn check_omega_exists(db_pool: &SqlitePool) -> bool {
let id = Julid::omega();
let count = query_scalar(USER_EXISTS_QUERY)
.bind(id)
.bind(OMEGA_ID)
.fetch_one(db_pool)
.await
.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 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 id = user.id.as_string();
let location = format!("/signup_success/{id}");
let resp = axum::response::Redirect::to(&location);
let resp = axum::response::Redirect::to(&format!("/signup_success/{}", user.id));
Ok(resp)
}
@ -172,23 +170,20 @@ pub(crate) async fn create_user(
.fetch_one(pool)
.await;
match res {
Ok(user) => Ok(user),
Err(sqlx::Error::Database(db)) => {
if let Some(exit) = db.code() {
let exit = exit.parse().unwrap_or(0u32);
Ok(res.map_err(|e| {
match e {
sqlx::Error::Database(db) => {
let exit = db.code().unwrap_or_default().parse().unwrap_or(0);
// https://www.sqlite.org/rescode.html codes for unique constraint violations:
if exit == 2067u32 || exit == 1555 {
Err(CreateUserErrorKind::AlreadyExists.into())
CreateUserErrorKind::AlreadyExists
} else {
Err(CreateUserErrorKind::UnknownDBError.into())
CreateUserErrorKind::UnknownDBError
}
} else {
Err(CreateUserErrorKind::UnknownDBError.into())
}
_ => CreateUserErrorKind::UnknownDBError,
}
_ => Err(CreateUserErrorKind::UnknownDBError.into()),
}
})?)
}
//-************************************************************************

View File

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