Skip to content

Commit

Permalink
fix: order
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar committed Nov 12, 2024
1 parent 204298b commit 0a72450
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 14 additions & 12 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ impl DB {
) -> anyhow::Result<(Vec<ProposalWithLatestSnapshotView>, 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());
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -473,20 +475,20 @@ impl DB {
) -> anyhow::Result<(Vec<RfpWithLatestSnapshotView>, 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<String> = 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,
});

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/entrypoints/proposal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn search(
}

#[utoipa::path(get, path = "/proposals?<order>&<limit>&<offset>&<filters>", 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)"),
))]
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/entrypoints/rfp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async fn search(
}

#[utoipa::path(get, path = "/rfps?<order>&<limit>&<offset>&<filters>", 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)"),
))]
Expand All @@ -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
Expand Down

0 comments on commit 0a72450

Please sign in to comment.