From 3afd9cd8b2c0368ab339c7a53fa890af8e994bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asger=20Juul=20Brunsh=C3=B8j?= Date: Thu, 27 Feb 2025 17:03:43 +0100 Subject: [PATCH] feat: UserInteraction table --- crates/ascend/src/app.rs | 2 +- crates/ascend/src/lib.rs | 2 +- crates/ascend/src/models.rs | 37 ++++++++++++++++++++++++++ crates/ascend/src/pages/settings.rs | 4 +-- crates/ascend/src/pages/wall.rs | 2 +- crates/ascend/src/server/db.rs | 29 +++++++++++++++++++- crates/ascend/src/server/migrations.rs | 18 ++++++++++++- 7 files changed, 87 insertions(+), 7 deletions(-) diff --git a/crates/ascend/src/app.rs b/crates/ascend/src/app.rs index 1264772..8efd20c 100644 --- a/crates/ascend/src/app.rs +++ b/crates/ascend/src/app.rs @@ -8,7 +8,7 @@ pub fn shell(options: LeptosOptions) -> impl IntoView { use leptos_meta::MetaTags; view! { - + diff --git a/crates/ascend/src/lib.rs b/crates/ascend/src/lib.rs index f24eb19..cb3f5f1 100644 --- a/crates/ascend/src/lib.rs +++ b/crates/ascend/src/lib.rs @@ -13,9 +13,9 @@ pub mod components { pub mod button; pub mod header; + pub mod icons; pub mod problem; pub mod problem_info; - pub mod icons; } pub mod resources { diff --git a/crates/ascend/src/models.rs b/crates/ascend/src/models.rs index 68146bd..f497118 100644 --- a/crates/ascend/src/models.rs +++ b/crates/ascend/src/models.rs @@ -15,6 +15,43 @@ pub use v2::Wall; pub use v2::WallDimensions; pub use v2::WallUid; +pub mod v3 { + use super::v2; + use serde::Deserialize; + use serde::Serialize; + + /// Registers user interaction with a problem + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct UserInteraction { + pub wall_uid: v2::WallUid, + pub problem_uid: v2::ProblemUid, + + /// Climbed problem + pub is_completed: bool, + + /// Flashed problem + pub is_flashed: bool, + + /// Is among favorite problems + pub is_favorite: bool, + + /// Added to personal challenges + pub is_challenge: bool, + } + impl UserInteraction { + pub fn new(wall_uid: v2::WallUid, problem_uid: v2::ProblemUid) -> Self { + Self { + wall_uid, + problem_uid, + is_completed: false, + is_flashed: false, + is_favorite: false, + is_challenge: false, + } + } + } +} + pub mod v2 { use super::v1; use derive_more::Display; diff --git a/crates/ascend/src/pages/settings.rs b/crates/ascend/src/pages/settings.rs index 3b22377..a1d7fdf 100644 --- a/crates/ascend/src/pages/settings.rs +++ b/crates/ascend/src/pages/settings.rs @@ -23,8 +23,8 @@ pub fn Settings() -> impl IntoView {
-
// {move || view! { }} -
+ // {move || view! { }} +
} } diff --git a/crates/ascend/src/pages/wall.rs b/crates/ascend/src/pages/wall.rs index 18ea7b6..ba3b69d 100644 --- a/crates/ascend/src/pages/wall.rs +++ b/crates/ascend/src/pages/wall.rs @@ -1,10 +1,10 @@ use crate::codec::ron::RonEncoded; -use crate::components::icons; use crate::components::ProblemInfo; use crate::components::button::Button; use crate::components::header::HeaderItem; use crate::components::header::HeaderItems; use crate::components::header::StyledHeader; +use crate::components::icons; use crate::models; use crate::models::HoldRole; use leptos::Params; diff --git a/crates/ascend/src/server/db.rs b/crates/ascend/src/server/db.rs index 0332361..3a607f7 100644 --- a/crates/ascend/src/server/db.rs +++ b/crates/ascend/src/server/db.rs @@ -86,6 +86,7 @@ impl Version { } } +// TODO: implement test #[tracing::instrument(skip_all, err)] pub async fn init_at_current_version(db: &Database) -> Result<(), DatabaseOperationError> { db.write(|txn| { @@ -116,6 +117,13 @@ pub async fn init_at_current_version(db: &Database) -> Result<(), DatabaseOperat let table = txn.open_table(current::TABLE_PROBLEMS)?; assert!(table.is_empty()?); } + + // User table + { + // Opening the table creates the table + let table = txn.open_table(current::TABLE_USER)?; + assert!(table.is_empty()?); + } } Ok(()) @@ -126,7 +134,26 @@ pub async fn init_at_current_version(db: &Database) -> Result<(), DatabaseOperat } use crate::models; -pub use v2 as current; +pub mod current { + use super::v2; + use super::v3; + pub use v2::TABLE_PROBLEMS; + pub use v2::TABLE_ROOT; + pub use v2::TABLE_WALLS; + pub use v3::TABLE_USER; + pub use v3::VERSION; +} + +pub mod v3 { + use crate::models; + use crate::server::db::bincode::Bincode; + use redb::TableDefinition; + + pub const VERSION: u64 = 3; + + pub const TABLE_USER: TableDefinition, Bincode> = + TableDefinition::new("user"); +} pub mod v2 { use crate::models; diff --git a/crates/ascend/src/server/migrations.rs b/crates/ascend/src/server/migrations.rs index b97926f..e8bb2d6 100644 --- a/crates/ascend/src/server/migrations.rs +++ b/crates/ascend/src/server/migrations.rs @@ -1,6 +1,22 @@ +use super::db; use super::db::Database; #[tracing::instrument(skip_all, err)] -pub async fn run_migrations(_db: &Database) -> Result<(), Box> { +pub async fn run_migrations(db: &Database) -> Result<(), Box> { + v3(db).await?; + Ok(()) +} + +#[tracing::instrument(skip_all, err)] +pub async fn v3(db: &Database) -> Result<(), Box> { + use redb::ReadableTableMetadata; + tracing::warn!("MIGRATING TO VERSION 3"); + db.write(|txn| { + // Opening the table creates the table + let table = txn.open_table(db::current::TABLE_USER)?; + assert!(table.is_empty()?); + Ok(()) + }) + .await?; Ok(()) }