Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub fn select_by_id(id: i64, conn: &mut Connection) -> Result<User> {

Full database schema is always available in schema.sql, use it as reference and don't try to make up non-existing tables and fields.

**All SQL queries must go through the blocking_queries/queries layer. Never embed raw SQL in REST or RPC handlers.** If you need a new query, add it to the appropriate `blocking_queries.rs` file and create an async wrapper in `queries.rs`. Each table has its own subfolder under `src/db/main/` (e.g., `element_event/`, `element_comment/`, `area_element/`).

### REST Handler Patterns
Handlers return `RestResult<T>` which is `Result<Json<T>, RestApiError>`:

Expand Down
38 changes: 38 additions & 0 deletions src/db/main/element_comment/blocking_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,44 @@ pub fn select_created_between(
.map_err(Into::into)
}

pub fn select_created_between_for_area(
area_id: i64,
period_start: &OffsetDateTime,
period_end: &OffsetDateTime,
conn: &Connection,
) -> Result<Vec<ElementComment>> {
let sql = format!(
r#"
SELECT {projection}
FROM {table}
WHERE {element_id} IN (
SELECT element_id FROM {area_element_table}
WHERE area_id = ?1 AND deleted_at IS NULL
)
AND {deleted_at} IS NULL
AND {created_at} > ?2 AND {created_at} < ?3
ORDER BY {created_at} DESC
"#,
projection = ElementComment::projection(),
table = schema::TABLE_NAME,
element_id = Columns::ElementId.as_str(),
created_at = Columns::CreatedAt.as_str(),
deleted_at = Columns::DeletedAt.as_str(),
area_element_table = crate::db::main::area_element::schema::TABLE_NAME,
);
conn.prepare(&sql)?
.query_map(
params![
area_id,
period_start.format(&Rfc3339)?,
period_end.format(&Rfc3339)?,
],
ElementComment::mapper(),
)?
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
}

pub fn select_by_element_id(
element_id: i64,
include_deleted: bool,
Expand Down
19 changes: 19 additions & 0 deletions src/db/main/element_comment/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ pub async fn select_created_between(
.await?
}

pub async fn select_created_between_for_area(
area_id: i64,
period_start: OffsetDateTime,
period_end: OffsetDateTime,
pool: &Pool,
) -> Result<Vec<ElementComment>> {
pool.get()
.await?
.interact(move |conn| {
blocking_queries::select_created_between_for_area(
area_id,
&period_start,
&period_end,
conn,
)
})
.await?
}

pub async fn select_by_element_id(
element_id: i64,
include_deleted: bool,
Expand Down
36 changes: 36 additions & 0 deletions src/db/main/element_event/blocking_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ pub fn select_created_between(
Ok(res)
}

pub fn select_created_between_for_area(
area_id: i64,
period_start: &OffsetDateTime,
period_end: &OffsetDateTime,
conn: &Connection,
) -> Result<Vec<ElementEvent>> {
let sql = format!(
r#"
SELECT {projection}
FROM {table}
WHERE {element_id} IN (
SELECT element_id FROM {area_element_table}
WHERE area_id = ?1 AND deleted_at IS NULL
)
AND {created_at} > ?2 AND {created_at} < ?3
ORDER BY {created_at} DESC
"#,
projection = ElementEvent::projection(),
table = schema::TABLE_NAME,
element_id = Columns::ElementId.as_str(),
created_at = Columns::CreatedAt.as_str(),
area_element_table = crate::db::main::area_element::schema::TABLE_NAME,
);
conn.prepare(&sql)?
.query_map(
params![
area_id,
period_start.format(&Rfc3339)?,
period_end.format(&Rfc3339)?,
],
ElementEvent::mapper(),
)?
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
Comment thread
escapedcat marked this conversation as resolved.
}

pub fn select_by_user(id: i64, limit: i64, conn: &Connection) -> Result<Vec<ElementEvent>> {
let sql = format!(
r#"
Expand Down
19 changes: 19 additions & 0 deletions src/db/main/element_event/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ pub async fn select_created_between(
.await?
}

pub async fn select_created_between_for_area(
area_id: i64,
period_start: OffsetDateTime,
period_end: OffsetDateTime,
pool: &Pool,
) -> Result<Vec<ElementEvent>> {
pool.get()
.await?
.interact(move |conn| {
blocking_queries::select_created_between_for_area(
area_id,
&period_start,
&period_end,
conn,
)
})
.await?
}

pub async fn select_all(
sort_order: Option<String>,
limit: Option<i64>,
Expand Down
Loading
Loading