Add type filtering in there and category filtering

This commit is contained in:
Michael Chung committed 2023-12-04 03:38:51 -05:00
1 parent e4756a4f2c
commit 241f648baf
2 files changed
+125 -88

No files matched your search

@@ -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<WeightsType>) -> 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<WeightsCategory>) -> Self {
self.weights_category = weights_category;
self
}
@@ -35,11 +35,17 @@ pub struct ListAvailibleWeightsSuccessResponse {
pub cursor_previous: Option<String>,
}
#[derive(Deserialize)]
pub struct ListWeightsByPathInfo {
pub username: String,
pub weights_type: Option<String>,
pub weights_category: Option<String>,
}
#[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<ListWeightsByPathInfo>,
query: web::Query<ListAvailibleWeightsQuery>,
server_state: web::Data<Arc<ServerState>>
) -> Result<HttpResponse, impl ResponseError> {
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<WeightsCategory> = None;
let mut weights_type: Option<WeightsType> = 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::<Vec<_>>(),
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::<Vec<_>>(),
cursor_next,
cursor_previous,
};
let body = serde_json::to_string(&response)
.map_err(|e| ListWeightError::ServerError)?;