From 1f8e642612190ca37b4628d452bbaad1fcf93f29 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 29 Jun 2023 16:27:21 -0700 Subject: [PATCH] add `created_at()` on IDs --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/db_id.rs | 5 +++++ src/signup.rs | 3 ++- src/users.rs | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb98297..8c47268 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2338,6 +2338,7 @@ dependencies = [ "axum-login", "axum-macros", "axum-test", + "chrono", "justerror", "optional_optional_user", "password-hash", diff --git a/Cargo.toml b/Cargo.toml index 7ca6f86..8c0b7d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,10 +23,11 @@ password-hash = { version = "0.5", features = ["std", "getrandom"] } axum-login = { version = "0.5", features = ["sqlite", "sqlx"] } unicode-segmentation = "1" async-session = "3" -ulid = { version = "1.0.0", features = ["rand"] } +ulid = { version = "1", features = ["rand"] } # proc macros: optional_optional_user = {path = "optional_optional_user"} +chrono = { version = "0.4", default-features = false, features = ["std", "clock"] } [dev-dependencies] axum-test = "9.0.0" diff --git a/src/db_id.rs b/src/db_id.rs index 305c678..a535dfc 100644 --- a/src/db_id.rs +++ b/src/db_id.rs @@ -3,6 +3,7 @@ use std::{ fmt::{Debug, Display}, }; +use chrono::Utc; use serde::{de::Visitor, Deserialize, Serialize}; use sqlx::{ encode::IsNull, @@ -39,6 +40,10 @@ impl DbId { pub fn as_string(&self) -> String { self.0.to_string() } + + pub fn created_at(&self) -> chrono::DateTime { + self.0.datetime().into() + } } //-************************************************************************ diff --git a/src/signup.rs b/src/signup.rs index 81943b5..e586874 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -110,7 +110,8 @@ pub async fn post_create_user( let id = DbId::new(); let user = create_user(username, &displayname, &email, password, &pool, id).await?; - tracing::debug!("created {user:?}"); + let now = user.id.created_at(); + tracing::debug!("created {user:?} at {now:?}"); let id = user.id.as_string(); let location = format!("/signup_success/{id}"); diff --git a/src/users.rs b/src/users.rs index c04d810..885d40b 100644 --- a/src/users.rs +++ b/src/users.rs @@ -26,7 +26,7 @@ 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) + .field("id", &self.id.as_string()) .field("username", &self.username) .field("displayname", &self.displayname) .field("email", &self.email)