From 2a623bd8eb986460eaba3a64f1157044e85adc5b Mon Sep 17 00:00:00 2001 From: Michael Chung Date: Thu, 23 Nov 2023 22:46:28 -0500 Subject: [PATCH] Ok just mapped this by hand, super dissapointed that I can't use the the proper way to do it for some unknown reason. --- .../queries/model_weights/delete_weights.rs | 2 - .../src/queries/model_weights/get_weight.rs | 43 +++++-- .../model_weights/list_weights_by_user.rs | 114 ++++++++++++------ .../list_weights_query_builder.rs | 0 .../src/queries/model_weights/mod.rs | 1 + 5 files changed, 112 insertions(+), 48 deletions(-) create mode 100644 crates/lib/mysql_queries/src/queries/model_weights/list_weights_query_builder.rs diff --git a/crates/lib/mysql_queries/src/queries/model_weights/delete_weights.rs b/crates/lib/mysql_queries/src/queries/model_weights/delete_weights.rs index d4eb55a97a..628cb2b7ee 100644 --- a/crates/lib/mysql_queries/src/queries/model_weights/delete_weights.rs +++ b/crates/lib/mysql_queries/src/queries/model_weights/delete_weights.rs @@ -1,8 +1,6 @@ use sqlx::MySqlPool; use errors::AnyhowResult; - - pub async fn delete_weights_as_user( weight_token: &str, mysql_pool: &MySqlPool diff --git a/crates/lib/mysql_queries/src/queries/model_weights/get_weight.rs b/crates/lib/mysql_queries/src/queries/model_weights/get_weight.rs index 6c05df7e27..8aedfb3c49 100644 --- a/crates/lib/mysql_queries/src/queries/model_weights/get_weight.rs +++ b/crates/lib/mysql_queries/src/queries/model_weights/get_weight.rs @@ -5,7 +5,7 @@ use sqlx::pool::PoolConnection; use anyhow::anyhow; use log::{ error }; -use enums::by_table::model_weights::weights_types::WeightsType; +use enums::by_table::model_weights::weights_types::{WeightsType, self}; use enums::by_table::model_weights::weights_category::WeightsCategory; use enums::common::visibility::Visibility; @@ -78,12 +78,35 @@ pub async fn get_weights_by_token_with_connection( } }; + // unwrap the result + + let weights_category = WeightsCategory::from_str(record.weights_category.as_ref()); + let weights_category = match weights_category { + Ok(weights_category) => weights_category, + Err(err) => { + error!("Error fetching weights by token: {:?}", err); + return Err(anyhow!("Error fetching weights by token: {:?}", err)); + } + }; + + + + let weights_types = WeightsType::from_str(record.weights_type.as_ref()); + + let weights_types = match weights_types { + Ok(weights_types) => weights_types, + Err(err) => { + error!("Error fetching weights by token: {:?}", err); + return Err(anyhow!("Error fetching weights by token: {:?}", err)); + } + }; + Ok( Some(RetrivedModelWeight { token: record.token, title: record.title, - weights_type: record.weights_type, - weights_category: record.weights_category, + weights_type: weights_types, + weights_category: weights_category, maybe_thumbnail_token: record.maybe_thumbnail_token, description_markdown: record.description_markdown, description_rendered_html: record.description_rendered_html, @@ -123,8 +146,8 @@ async fn select_include_deleted( SELECT wt.token as `token: tokens::tokens::model_weights::ModelWeightToken`, wt.title, - wt.weights_type as `weights_type: enums::by_table::model_weights::weights_types::WeightsType`, - wt.weights_category as `weights_category: enums::by_table::model_weights::weights_category::WeightsCategory`, + wt.weights_type, + wt.weights_category, wt.maybe_thumbnail_token, wt.description_markdown, wt.description_rendered_html, @@ -162,6 +185,8 @@ async fn select_without_deleted( weight_token: &ModelWeightToken, mysql_connection: &mut PoolConnection ) -> Result { + //as `weights_type: enums::by_table::model_weights::weights_types::WeightsType`, + //as `weights_category: enums::by_table::model_weights::weights_category::WeightsCategory` sqlx ::query_as!( RawWeight, @@ -169,8 +194,8 @@ async fn select_without_deleted( SELECT wt.token as `token: tokens::tokens::model_weights::ModelWeightToken`, wt.title, - wt.weights_type as `weights_type: enums::by_table::model_weights::weights_types::WeightsType`, - wt.weights_category as `weights_category: enums::by_table::model_weights::weights_category::WeightsCategory`, + wt.weights_type, + wt.weights_category, wt.maybe_thumbnail_token, wt.description_markdown, wt.description_rendered_html, @@ -211,8 +236,8 @@ async fn select_without_deleted( pub struct RawWeight { pub token: ModelWeightToken, pub title: String, - pub weights_type: WeightsType, - pub weights_category: WeightsCategory, + pub weights_type: String, + pub weights_category: String, pub maybe_thumbnail_token: Option, pub description_markdown: String, pub description_rendered_html: String, diff --git a/crates/lib/mysql_queries/src/queries/model_weights/list_weights_by_user.rs b/crates/lib/mysql_queries/src/queries/model_weights/list_weights_by_user.rs index 976fa1feb2..609b5d7c56 100644 --- a/crates/lib/mysql_queries/src/queries/model_weights/list_weights_by_user.rs +++ b/crates/lib/mysql_queries/src/queries/model_weights/list_weights_by_user.rs @@ -12,38 +12,45 @@ use enums::by_table::model_weights::{ weights_types::WeightsType, weights_category::WeightsCategory, }; +use tokens::tokens::users::UserToken; #[derive(Serialize)] -pub struct WeightsRecord { - pub weights_token: ModelWeightToken, +pub struct WeightsJoinUserRecord { + pub token: ModelWeightToken, pub title: String, - - pub creator_user_token: UserToken, - pub creator_username: String, - pub creator_ip_address: String, - pub weights_type: WeightsType, pub weights_category: WeightsCategory, - + pub maybe_thumbnail_token: Option, pub description_markdown: String, pub description_rendered_html: String, - - pub cached_user_ratings_negative_count: i32, - pub cached_user_ratings_positive_count: i32, - pub cached_user_ratings_total_count: i32, - + pub creator_user_token: UserToken, + pub creator_ip_address: String, + pub creator_set_visibility: Visibility, + pub maybe_last_update_user_token: Option, + pub original_download_url: Option, + pub original_filename: Option, + pub file_size_bytes: i32, + pub file_checksum_sha2: String, + pub private_bucket_hash: String, + pub maybe_private_bucket_prefix: Option, + pub maybe_private_bucket_extension: Option, + pub cached_user_ratings_negative_count: u32, + pub cached_user_ratings_positive_count: u32, + pub cached_user_ratings_total_count: u32, pub maybe_cached_user_ratings_ratio: Option, - pub cached_user_ratings_total_count: i32, - + pub cached_user_ratings_last_updated_at: DateTime, + pub version: i32, pub created_at: DateTime, pub updated_at: DateTime, + pub user_deleted_at: Option>, + pub mod_deleted_at: Option> } pub async fn list_weights_by_username( mysql_pool: &MySqlPool, username: &str, can_see_deleted: bool, -) -> AnyhowResult> { +) -> AnyhowResult> { let mut connection = mysql_pool.acquire().await?; list_weights_by_username_with_connection(&mut connection, username, can_see_deleted).await } @@ -52,13 +59,13 @@ pub async fn list_weights_by_username_with_connection( mysql_connection: &mut PoolConnection, creator_username: &str, can_see_deleted: bool, -) -> AnyhowResult> { +) -> AnyhowResult> { let datasets = list_datasets_by_creator_username(mysql_connection, creator_username, can_see_deleted) .await; - let datasets : Vec = match datasets { + let datasets : Vec = match datasets { Ok(datasets) => datasets, Err(err) => { match err { @@ -74,24 +81,24 @@ pub async fn list_weights_by_username_with_connection( }; Ok(datasets.into_iter() - .map(|dataset| { - WeightsRecord { - weights_token: ModelWeightToken(dataset.token), - title: dataset.title, - creator_user_token: dataset.creator_user_token, - creator_username: dataset.creator_username, - creator_ip_address: dataset.creator_ip_address, - weights_type: dataset.weights_type, - weights_category: dataset.weights_category, - description_markdown: dataset.description_markdown, - description_rendered_html: dataset.description_rendered_html, - cached_user_ratings_negative_count: dataset.cached_user_ratings_negative_count, - cached_user_ratings_positive_count: dataset.cached_user_ratings_positive_count, - cached_user_ratings_total_count: dataset.cached_user_ratings_total_count, - maybe_cached_user_ratings_ratio: dataset.maybe_cached_user_ratings_ratio, - cached_user_ratings_total_count: dataset.cached_user_ratings_total_count, - created_at: dataset.created_at, - updated_at: dataset.updated_at, + .map(|dataset: WeightsJoinUserRecord| { + WeightsJoinUserRecord { + // weights_token: ModelWeightToken(dataset.token), + // title: dataset.title, + // creator_user_token: dataset.creator_user_token, + // creator_username: dataset.creator_username, + // creator_ip_address: dataset.creator_ip_address, + // weights_type: dataset.weights_type, + // weights_category: dataset.weights_category, + // description_markdown: dataset.description_markdown, + // description_rendered_html: dataset.description_rendered_html, + // cached_user_ratings_negative_count: dataset.cached_user_ratings_negative_count, + // cached_user_ratings_positive_count: dataset.cached_user_ratings_positive_count, + // cached_user_ratings_total_count: dataset.cached_user_ratings_total_count, + // maybe_cached_user_ratings_ratio: dataset.maybe_cached_user_ratings_ratio, + // cached_user_ratings_total_count: dataset.cached_user_ratings_total_count, + // created_at: dataset.created_at, + // updated_at: dataset.updated_at, } }) .filter(|dataset| { @@ -101,7 +108,7 @@ pub async fn list_weights_by_username_with_connection( } -async fn list_datasets_by_creator_username( +async fn list_weights_by_creator_username( mysql_connection: &mut PoolConnection, creator_username: &str, can_see_deleted: bool, @@ -168,6 +175,39 @@ async fn list_datasets_by_creator_username( Ok(maybe_datasets) } + +#[derive(Serialize)] +pub struct RawWeightJoinUser { + pub token: ModelWeightToken, + pub title: String, + pub weights_type: WeightsType, + pub weights_category: WeightsCategory, + pub maybe_thumbnail_token: Option, + pub description_markdown: String, + pub description_rendered_html: String, + pub creator_user_token: UserToken, + pub creator_ip_address: String, + pub creator_set_visibility: Visibility, + pub maybe_last_update_user_token: Option, + pub original_download_url: Option, + pub original_filename: Option, + pub file_size_bytes: i32, + pub file_checksum_sha2: String, + pub private_bucket_hash: String, + pub maybe_private_bucket_prefix: Option, + pub maybe_private_bucket_extension: Option, + pub cached_user_ratings_negative_count: u32, + pub cached_user_ratings_positive_count: u32, + pub cached_user_ratings_total_count: u32, + pub maybe_cached_user_ratings_ratio: Option, + pub cached_user_ratings_last_updated_at: DateTime, + pub version: i32, + pub created_at: DateTime, + pub updated_at: DateTime, + pub user_deleted_at: Option>, + pub mod_deleted_at: Option>, +} + #[cfg(test)] mod tests { diff --git a/crates/lib/mysql_queries/src/queries/model_weights/list_weights_query_builder.rs b/crates/lib/mysql_queries/src/queries/model_weights/list_weights_query_builder.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/crates/lib/mysql_queries/src/queries/model_weights/mod.rs b/crates/lib/mysql_queries/src/queries/model_weights/mod.rs index c429d93968..f082e5ce0b 100644 --- a/crates/lib/mysql_queries/src/queries/model_weights/mod.rs +++ b/crates/lib/mysql_queries/src/queries/model_weights/mod.rs @@ -3,4 +3,5 @@ pub mod delete_weights; pub mod get_weight; pub mod update_weight; // pub mod list_weights_by_user; +// pub mod list_weights_query_builder; pub mod tests; \ No newline at end of file