Move database queries intothe storage struct
This commit is contained in:
parent
e7601b7100
commit
0971df3cbe
@ -21,9 +21,6 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use flake_tracker::{
|
use flake_tracker::{
|
||||||
storage::{
|
storage::{
|
||||||
FlakeRevision,
|
|
||||||
FlakeUri,
|
|
||||||
InputModel,
|
|
||||||
Storage,
|
Storage,
|
||||||
},
|
},
|
||||||
templates::{
|
templates::{
|
||||||
@ -118,19 +115,8 @@ async fn route_index(
|
|||||||
async fn route_flakes(
|
async fn route_flakes(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
let flake_revisions: Vec<FlakeUri> = sqlx::query_as("
|
|
||||||
SELECT
|
|
||||||
uri
|
|
||||||
FROM flake_revisions
|
|
||||||
GROUP BY uri
|
|
||||||
ORDER BY uri
|
|
||||||
")
|
|
||||||
.fetch_all(&state.storage.db)
|
|
||||||
.await
|
|
||||||
.context("Failed to fetch data from database")?;
|
|
||||||
|
|
||||||
Ok(render_template(&FlakesTemplate {
|
Ok(render_template(&FlakesTemplate {
|
||||||
flakes: flake_revisions,
|
flakes: state.storage.flakes().await?,
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,23 +124,9 @@ async fn route_flake_info(
|
|||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(uri): Path<String>,
|
Path(uri): Path<String>,
|
||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
let flake_revisions: Vec<FlakeRevision> = sqlx::query_as("
|
|
||||||
SELECT
|
|
||||||
revision_uri,
|
|
||||||
revision,
|
|
||||||
last_modified
|
|
||||||
FROM flake_revisions
|
|
||||||
WHERE uri = ?
|
|
||||||
ORDER BY last_modified
|
|
||||||
")
|
|
||||||
.bind(&uri)
|
|
||||||
.fetch_all(&state.storage.db)
|
|
||||||
.await
|
|
||||||
.context("Failed to fetch data from database")?;
|
|
||||||
|
|
||||||
Ok(render_template(&FlakeInfoTemplate {
|
Ok(render_template(&FlakeInfoTemplate {
|
||||||
uri: uri,
|
uri: uri.clone(),
|
||||||
flake_revisions: flake_revisions,
|
flake_revisions: state.storage.revisions_from_flake(&uri).await?,
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,25 +134,9 @@ async fn route_revision(
|
|||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(revision_uri): Path<String>,
|
Path(revision_uri): Path<String>,
|
||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
let inputs: Vec<InputModel> = sqlx::query_as("
|
|
||||||
SELECT
|
|
||||||
flake_revision_uri,
|
|
||||||
input_name,
|
|
||||||
revision_uri,
|
|
||||||
uri,
|
|
||||||
nar_hash,
|
|
||||||
last_modified
|
|
||||||
FROM flake_revisions_inputs
|
|
||||||
WHERE flake_revision_uri = ?
|
|
||||||
ORDER BY input_name
|
|
||||||
")
|
|
||||||
.bind(&revision_uri)
|
|
||||||
.fetch_all(&state.storage.db)
|
|
||||||
.await
|
|
||||||
.context("Failed to fetch data from database")?;
|
|
||||||
|
|
||||||
Ok(render_template(&RevisionTemplate {
|
Ok(render_template(&RevisionTemplate {
|
||||||
revision_uri: revision_uri,
|
revision_uri: revision_uri.clone(),
|
||||||
inputs: inputs,
|
inputs: state.storage.inputs_for_revision(&revision_uri).await?,
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,54 @@ impl Storage {
|
|||||||
db,
|
db,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn flakes(&self) -> Result<Vec<FlakeUri>> {
|
||||||
|
sqlx::query_as("
|
||||||
|
SELECT
|
||||||
|
uri
|
||||||
|
FROM flake_revisions
|
||||||
|
GROUP BY uri
|
||||||
|
ORDER BY uri
|
||||||
|
")
|
||||||
|
.fetch_all(&self.db)
|
||||||
|
.await
|
||||||
|
.context("Failed to fetch data from database")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn revisions_from_flake(&self, uri: &str) -> Result<Vec<FlakeRevision>> {
|
||||||
|
sqlx::query_as("
|
||||||
|
SELECT
|
||||||
|
revision_uri,
|
||||||
|
revision,
|
||||||
|
last_modified
|
||||||
|
FROM flake_revisions
|
||||||
|
WHERE uri = ?
|
||||||
|
ORDER BY last_modified
|
||||||
|
")
|
||||||
|
.bind(&uri)
|
||||||
|
.fetch_all(&self.db)
|
||||||
|
.await
|
||||||
|
.context("Failed to fetch data from database")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn inputs_for_revision(&self, revision_uri: &str) -> Result<Vec<InputModel>> {
|
||||||
|
sqlx::query_as("
|
||||||
|
SELECT
|
||||||
|
flake_revision_uri,
|
||||||
|
input_name,
|
||||||
|
revision_uri,
|
||||||
|
uri,
|
||||||
|
nar_hash,
|
||||||
|
last_modified
|
||||||
|
FROM flake_revisions_inputs
|
||||||
|
WHERE flake_revision_uri = ?
|
||||||
|
ORDER BY input_name
|
||||||
|
")
|
||||||
|
.bind(&revision_uri)
|
||||||
|
.fetch_all(&self.db)
|
||||||
|
.await
|
||||||
|
.context("Failed to fetch data from database")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromRow)]
|
#[derive(FromRow)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user