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 index 69ccf3617a..e1d79b89ab 100644 --- 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 @@ -133,13 +133,13 @@ impl ListWeightsQueryBuilder { self } - pub fn weights_type(mut self, weights_type: WeightsType) -> Self { - self.weights_type = Some(weights_type); + pub fn weights_type(mut self, weights_type: Option) -> Self { + self.weights_type = weights_type; self } - pub fn weights_category(mut self, weights_category: WeightsCategory) -> Self { - self.weights_category = Some(weights_category); + pub fn weights_category(mut self, weights_category: Option) -> Self { + self.weights_category = weights_category; self } diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/weights/list_availible_weights.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/weights/list_availible_weights.rs index fa64de0d58..26257fc60a 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/weights/list_availible_weights.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/weights/list_availible_weights.rs @@ -35,11 +35,17 @@ pub struct ListAvailibleWeightsSuccessResponse { pub cursor_previous: Option, } +#[derive(Deserialize)] +pub struct ListWeightsByPathInfo { + pub username: String, + pub weights_type: Option, + pub weights_category: Option, +} + #[derive(Serialize)] pub struct ModelWeightForList { pub weight_token: ModelWeightToken, - pub weights_type: WeightsType, pub weights_category: WeightsCategory, @@ -73,31 +79,31 @@ pub struct ModelWeightForList { #[derive(Debug)] pub enum ListWeightError { - NotAuthorized, - ServerError, + NotAuthorized, + ServerError, } impl std::fmt::Display for ListWeightError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) - } + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } } impl ResponseError for ListWeightError { - fn status_code(&self) -> StatusCode { - match *self { - ListWeightError::NotAuthorized => StatusCode::UNAUTHORIZED, - ListWeightError::ServerError => StatusCode::INTERNAL_SERVER_ERROR, + fn status_code(&self) -> StatusCode { + match *self { + ListWeightError::NotAuthorized => StatusCode::UNAUTHORIZED, + ListWeightError::ServerError => StatusCode::INTERNAL_SERVER_ERROR, + } } - } } pub async fn list_availible_weights_handler( http_request: HttpRequest, + path: web::Path, query: web::Query, server_state: web::Data> ) -> Result { - let maybe_user_session = server_state.session_checker .maybe_get_user_session(&http_request, &server_state.mysql_pool).await .map_err(|e| { @@ -123,103 +129,134 @@ pub async fn list_availible_weights_handler( let cursor_is_reversed = query.cursor_is_reversed.unwrap_or(false); let cursor = if let Some(cursor) = query.cursor.as_deref() { - let cursor = server_state.sort_key_crypto.decrypt_id(cursor) + let cursor = server_state.sort_key_crypto.decrypt_id(cursor) .map_err(|e| { warn!("crypto error: {:?}", e); ListWeightError::ServerError - })?; - Some(cursor) + })?; + Some(cursor) } else { - None + None }; - let include_user_hidden = is_mod; + let include_user_hidden = is_mod; - let mut query_builder = ListWeightsQueryBuilder::new() - .sort_ascending(sort_ascending) - .scope_creator_username(None) - .include_user_hidden(include_user_hidden) - .include_user_deleted_results(is_mod) - .include_mod_deleted_results(is_mod) - .limit(limit) - .cursor_is_reversed(cursor_is_reversed) - .offset(cursor); + let mut weights_category: Option = None; + let mut weights_type: Option = None; - let query_results = query_builder.perform_query_for_page(&server_state.mysql_pool).await; + if let Some(weights_category_string) = path.weights_category.as_ref() { + let result = WeightsCategory::from_str(&weights_category_string); + match result { + Ok(category) => { + weights_category = Some(category) + }, + Err(e) => { + warn!("invalid weights_category: {:?}", e); + weights_category = None + } + } + } + + if let Some(weights_type_string) = path.weights_type.as_ref() { + let result = WeightsType::from_str(&weights_type_string); + match result { + Ok(wtype) => { + weights_type = Some(wtype) + }, + Err(e) => { + warn!("invalid weights_type: {:?}", e); + weights_type = None + } + } + } + + let mut query_builder = ListWeightsQueryBuilder::new() + .sort_ascending(sort_ascending) + .weights_category(weights_category) + .weights_type(weights_type) + .scope_creator_username(None) + .include_user_hidden(include_user_hidden) + .include_user_deleted_results(is_mod) + .include_mod_deleted_results(is_mod) + .limit(limit) + .cursor_is_reversed(cursor_is_reversed) + .offset(cursor); + + let query_results = query_builder.perform_query_for_page(&server_state.mysql_pool).await; - let weights_page = match query_results { - Ok(results) => results, - Err(e) => { - warn!("Query error: {:?}", e); - return Err(ListWeightError::ServerError); - } - }; + let weights_page = match query_results { + Ok(results) => results, + Err(e) => { + warn!("Query error: {:?}", e); + return Err(ListWeightError::ServerError); + } + }; - let cursor_next = if let Some(id) = weights_page.last_id { - let cursor = server_state.sort_key_crypto.encrypt_id(id as u64) + let cursor_next = if let Some(id) = weights_page.last_id { + let cursor = server_state.sort_key_crypto.encrypt_id(id as u64) .map_err(|e| { - warn!("crypto error: {:?}", e); - ListWeightError::ServerError - })?; - Some(cursor) - } else { - None - }; + warn!("crypto error: {:?}", e); + ListWeightError::ServerError + })?; + Some(cursor) + } else { + None + }; - let cursor_previous = if let Some(id) = weights_page.first_id { - let cursor = server_state.sort_key_crypto.encrypt_id(id as u64) + let cursor_previous = if let Some(id) = weights_page.first_id { + let cursor = server_state.sort_key_crypto.encrypt_id(id as u64) .map_err(|e| { - warn!("crypto error: {:?}", e); - ListWeightError::ServerError - })?; - Some(cursor) - } else { - None - }; + warn!("crypto error: {:?}", e); + ListWeightError::ServerError + })?; + Some(cursor) + } else { + None + }; - // generate parse a response - let response = ListAvailibleWeightsSuccessResponse { - success: true, - weights: weights_page.weights.into_iter() - .map(|weights| ModelWeightForList { - weight_token: weights.token, - title: weights.title, - weights_type: weights.weights_type, - weights_category: weights.weights_category, + // generate parse a response + let response = ListAvailibleWeightsSuccessResponse { + success: true, + weights: weights_page.weights.into_iter() + .map(|weights| ModelWeightForList { + weight_token: weights.token, + title: weights.title, + weights_type: weights.weights_type, + weights_category: weights.weights_category, - maybe_thumbnail_token:weights.maybe_thumbnail_token, - description_markdown: weights.description_markdown, - description_rendered_html: weights.description_rendered_html, + maybe_thumbnail_token:weights.maybe_thumbnail_token, + description_markdown: weights.description_markdown, + description_rendered_html: weights.description_rendered_html, - creator_user_token: weights.creator_user_token, - creator_set_visibility: weights.creator_set_visibility, + creator_user_token: weights.creator_user_token, + creator_set_visibility: weights.creator_set_visibility, - file_size_bytes:weights.file_size_bytes, - file_checksum_sha2: weights.file_checksum_sha2, + file_size_bytes:weights.file_size_bytes, + file_checksum_sha2: weights.file_checksum_sha2, - cached_user_ratings_total_count: weights.cached_user_ratings_total_count, - cached_user_ratings_positive_count: weights.cached_user_ratings_positive_count, - cached_user_ratings_negative_count: weights.cached_user_ratings_negative_count, - maybe_cached_user_ratings_ratio: weights.maybe_cached_user_ratings_ratio, + cached_user_ratings_total_count: weights.cached_user_ratings_total_count, + cached_user_ratings_positive_count: weights.cached_user_ratings_positive_count, + cached_user_ratings_negative_count: weights.cached_user_ratings_negative_count, + maybe_cached_user_ratings_ratio: weights.maybe_cached_user_ratings_ratio, - version: weights.version, + version: weights.version, - created_at: weights.created_at, - updated_at: weights.updated_at, + created_at: weights.created_at, + updated_at: weights.updated_at, - creator_username: weights.creator_username, - creator_display_name: weights.creator_display_name, - creator_email_gravatar_hash: weights.creator_email_gravatar_hash - }).collect::>(), - cursor_next, - cursor_previous, - }; - - let body = serde_json::to_string(&response) + creator_username: weights.creator_username, + creator_display_name: weights.creator_display_name, + creator_email_gravatar_hash: weights.creator_email_gravatar_hash + }).collect::>(), + cursor_next, + cursor_previous, + }; + + let body = serde_json::to_string(&response) .map_err(|e| ListWeightError::ServerError)?;