Skip to content

Commit

Permalink
Adjust query and LoadableAsset to include deleted events
Browse files Browse the repository at this point in the history
I forgot the fact that the table might include events that
have been marked as deleted. Unfortunately that means the
query and `LoadableAsset` trait are getting increasingly
complicated to the point where it's hard to understand and
quite easy to make these errors.
This is also in part due to the series table's need to be
sortable by event count, and I am starting to wonder if it
might be better to move the whole query to the trait, though
that brings its own complications and I doubt that would even
be possible without other major adjustments.
  • Loading branch information
owi92 committed Feb 5, 2025
1 parent 19ebb5d commit a9b6065
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion backend/src/api/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,11 @@ impl LoadableAsset for AuthorizedEvent {
}

fn table_name() -> &'static str {
"events"
"all_events"
}

fn alias() -> Option<&'static str> {
Some("events")
}

fn sort_clauses(_column: &str) -> (&str, &str) {
Expand Down
8 changes: 6 additions & 2 deletions backend/src/api/model/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,14 @@ impl LoadableAsset for Series {
"series"
}

fn alias() -> Option<&'static str> {
None
}

fn sort_clauses(column: &str) -> (&str, &str) {
match column {
"count(events.id)" => (
"left join events on events.series = series.id",
"count(all_events.id)" => (
"left join all_events on all_events.series = series.id",
"group by series.id",
),
_ => ("", ""),
Expand Down
14 changes: 11 additions & 3 deletions backend/src/api/model/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ define_sort_column_and_order!(
Title => "title",
Created => "created",
Updated => "updated",
EventCount => "count(events.id)",
EventCount => "count(all_events.id)",
};
pub struct SeriesSortOrder
);
Expand Down Expand Up @@ -164,6 +164,7 @@ pub type AssetMapping<ResourceMapping> = ResourceMapping;
pub trait LoadableAsset: FromDb<RowMapping: Copy> {
fn selection() -> (String, <Self as FromDb>::RowMapping);
fn table_name() -> &'static str;
fn alias() -> Option<&'static str>;
/// Returns custom `join` and `group by` clauses that might be required
/// for ordering by certain columns.
fn sort_clauses(column: &str) -> (&str, &str);
Expand Down Expand Up @@ -201,6 +202,13 @@ where
};

let table = T::table_name();
let alias = T::alias();

let table_alias = match alias {
Some(a) => format!("{table} as {a}"),
None => table.to_string(),
};
let table = alias.unwrap_or(table);

// We need to know the item count before querying the actual items,
// to check if the offset is too high. If it is, it's set to the maximum.
Expand All @@ -217,7 +225,7 @@ where
let (join_clause, group_by_clause) = T::sort_clauses(&sort_column);

let query = format!(
"select {selection} from {table} \
"select {selection} from {table_alias} \
{join_clause} \
{acl_filter} \
{group_by_clause} \
Expand All @@ -228,7 +236,7 @@ where

// Execute query
let items = context.db.query_mapped(&query, args, |row| {
// Retrieve actual event data
// Retrieve actual row data
T::from_row(&row, mapping)
}).await?;

Expand Down

0 comments on commit a9b6065

Please sign in to comment.