Port index page
This commit is contained in:
parent
3bfae9c0ad
commit
a36b188938
4 changed files with 18 additions and 11 deletions
69
src/server/web/pages/index.rs
Normal file
69
src/server/web/pages/index.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use askama::Template;
|
||||
use axum::{extract::State, response::IntoResponse};
|
||||
use futures::TryStreamExt;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
server::web::{
|
||||
base::{Base, Tab},
|
||||
link::LinkCommit,
|
||||
paths::PathIndex,
|
||||
},
|
||||
somehow,
|
||||
};
|
||||
|
||||
struct Ref {
|
||||
name: String,
|
||||
commit: LinkCommit,
|
||||
tracked: bool,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/index.html")]
|
||||
struct IndexTemplate {
|
||||
base: Base,
|
||||
tracked_refs: Vec<Ref>,
|
||||
untracked_refs: Vec<Ref>,
|
||||
}
|
||||
|
||||
pub async fn get_index(
|
||||
_path: PathIndex,
|
||||
State(config): State<&'static Config>,
|
||||
State(db): State<SqlitePool>,
|
||||
) -> somehow::Result<impl IntoResponse> {
|
||||
let base = Base::new(config, Tab::Index);
|
||||
|
||||
let refs = sqlx::query!(
|
||||
"\
|
||||
SELECT name, hash, message, reachable, tracked \
|
||||
FROM refs \
|
||||
JOIN commits USING (hash) \
|
||||
ORDER BY name ASC \
|
||||
"
|
||||
)
|
||||
.fetch(&db)
|
||||
.map_ok(|r| Ref {
|
||||
name: r.name.clone(),
|
||||
commit: LinkCommit::new(&base, r.hash, &r.message, r.reachable),
|
||||
tracked: r.tracked != 0,
|
||||
})
|
||||
.try_collect::<Vec<_>>()
|
||||
.await?;
|
||||
|
||||
let mut tracked_refs = vec![];
|
||||
let mut untracked_refs = vec![];
|
||||
for reference in refs {
|
||||
if reference.tracked {
|
||||
tracked_refs.push(reference);
|
||||
} else {
|
||||
untracked_refs.push(reference);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(IndexTemplate {
|
||||
base: Base::new(config, Tab::Index),
|
||||
tracked_refs,
|
||||
untracked_refs,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue