From 0a7245053b274632564473f29a117f64d2c88051 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 12 Nov 2024 18:21:48 +0900 Subject: [PATCH] fix: order --- .github/workflows/ci.yml | 3 +++ src/db/mod.rs | 26 ++++++++++++++------------ src/entrypoints/proposal/mod.rs | 8 ++++---- src/entrypoints/rfp/mod.rs | 8 ++++---- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f07faf3..c64ab3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,9 @@ jobs: psql -c \"ALTER ROLE devhub_cache_api_rs CREATEDB;\" psql -c \"GRANT ALL PRIVILEGES ON DATABASE devhub_cache_api_rs TO devhub_cache_api_rs;\" " + - name: Install SQlx + run: cargo install sqlx-cli + - name: Create database tables run: cargo sqlx database create diff --git a/src/db/mod.rs b/src/db/mod.rs index ec80021..c2f8996 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -170,9 +170,11 @@ impl DB { ) -> anyhow::Result<(Vec, i64)> { // Validate the order clause to prevent SQL injection let order_clause = match order.to_lowercase().as_str() { - "asc" => "ASC", - "desc" => "DESC", - _ => "DESC", // Default to DESC if the order is not recognized + "ts_asc" => "ps.ts ASC", + "ts_desc" => "ps.ts DESC", + "id_asc" => "ps.proposal_id ASC", + "id_desc" => "ps.proposal_id DESC", + _ => "ps.proposal_id DESC", // Default to DESC if the order is not recognized }; let stage = filters.as_ref().and_then(|f| f.stage.as_ref()); @@ -234,7 +236,7 @@ impl DB { AND ($5 IS NULL OR ps.timeline::text ~ $5) AND ($6 IS NULL OR ps.category = $6) AND ($7 IS NULL OR ps.labels::jsonb ?| $7) - ORDER BY ps.ts {} + ORDER BY {} LIMIT $1 OFFSET $2 "#, order_clause, @@ -473,20 +475,20 @@ impl DB { ) -> anyhow::Result<(Vec, i64)> { // Validate the order clause to prevent SQL injection let order_clause = match order.to_lowercase().as_str() { - "asc" => "ASC", - "desc" => "DESC", - _ => "DESC", // Default to DESC if the order is not recognized + "ts_asc" => "ps.ts ASC", + "ts_desc" => "ps.ts DESC", + "id_asc" => "ps.rfp_id ASC", + "id_desc" => "ps.rfp_id DESC", + _ => "ps.rfp_id DESC", // Default to DESC if the order is not recognized }; // Extract and validate the stage filter let stage = filters.as_ref().and_then(|f| f.stage.as_ref()); let stage_clause: Option = stage.and_then(|s| match s.to_uppercase().as_str() { - // AcceptingSubmissions, - // Evaluation, - // "ACCEPTING_SUBMISSIONS" => Some(), + "ACCEPTING_SUBMISSIONS" => Some("ACCEPTING_SUBMISSIONS".to_string()), + "EVALUATION" => Some("EVALUATION".to_string()), "PROPOSAL_SELECTED" => Some("PROPOSAL_SELECTED".to_string()), "CANCELLED" => Some("CANCELLED".to_string()), - _ => None, }); @@ -530,7 +532,7 @@ impl DB { AND ($5 IS NULL OR ps.timeline::text ~ $5) AND ($6 IS NULL OR ps.category = $6) AND ($7 IS NULL OR ps.labels::jsonb ?| $7) - ORDER BY ps.ts {order} + ORDER BY {order} LIMIT $1 OFFSET $2 "#, order = order_clause, diff --git a/src/entrypoints/proposal/mod.rs b/src/entrypoints/proposal/mod.rs index ad6448f..c18c099 100644 --- a/src/entrypoints/proposal/mod.rs +++ b/src/entrypoints/proposal/mod.rs @@ -44,8 +44,8 @@ async fn search( } #[utoipa::path(get, path = "/proposals?&&&", params( - ("order"= &str, Path, description ="order"), - ("limit"= i64, Path, description = "limit"), + ("order"= &str, Path, description ="default order id_desc (ts_asc)"), + ("limit"= i64, Path, description = "default limit 10"), ("offset"= i64, Path, description = "offset"), ("filters"= GetProposalFilters, Path, description = "filters struct that contains stuff like category, labels (vec), author_id, stage, block_timestamp (i64)"), ))] @@ -61,8 +61,8 @@ async fn get_proposals( let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap(); let last_updated_timestamp = db.get_last_updated_timestamp().await.unwrap(); - let order = order.unwrap_or("desc"); - let limit = limit.unwrap_or(25); + let order = order.unwrap_or("id_desc"); + let limit = limit.unwrap_or(10); let offset = offset.unwrap_or(0); if current_timestamp_nano - last_updated_timestamp diff --git a/src/entrypoints/rfp/mod.rs b/src/entrypoints/rfp/mod.rs index 6161ee5..1255089 100644 --- a/src/entrypoints/rfp/mod.rs +++ b/src/entrypoints/rfp/mod.rs @@ -42,8 +42,8 @@ async fn search( } #[utoipa::path(get, path = "/rfps?&&&", params( - ("order"= &str, Path, description ="order"), - ("limit"= i64, Path, description = "limit"), + ("order"= &str, Path, description ="default order id_desc"), + ("limit"= i64, Path, description = "default limit 10"), ("offset"= i64, Path, description = "offset"), ("filters"= GetRfpFilters, Path, description = "filters struct that contains stuff like category, labels (vec), author_id, stage, block_timestamp (i64)"), ))] @@ -59,8 +59,8 @@ async fn get_rfps( let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap(); let last_updated_timestamp = db.get_last_updated_timestamp().await.unwrap(); - let order = order.unwrap_or("desc"); - let limit = limit.unwrap_or(25); + let order = order.unwrap_or("id_desc"); + let limit = limit.unwrap_or(10); let offset = offset.unwrap_or(0); if current_timestamp_nano - last_updated_timestamp