Add pagination to search results.

Move to shared definition of json requests between client/server.
This commit is contained in:
2023-03-09 18:04:55 -08:00
parent f16860dd09
commit eba362a7f2
10 changed files with 162 additions and 34 deletions

View File

@@ -9,6 +9,7 @@ edition = "2021"
rocket = { version = "0.5.0-rc.2", features = [ "json" ] }
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
notmuch = { path = "../notmuch" }
shared = { path = "../shared" }
serde_json = "1.0.87"
thiserror = "1.0.37"
serde = { version = "1.0.147", features = ["derive"] }

View File

@@ -22,17 +22,30 @@ fn hello() -> &'static str {
async fn refresh(nm: &State<Notmuch>) -> Result<Json<String>, Debug<NotmuchError>> {
Ok(Json(String::from_utf8_lossy(&nm.new()?).to_string()))
}
#[get("/search")]
async fn search_all(nm: &State<Notmuch>) -> Result<Json<SearchSummary>, Debug<NotmuchError>> {
search(nm, "*").await
async fn search_all(
nm: &State<Notmuch>,
) -> Result<Json<shared::SearchResult>, Debug<NotmuchError>> {
search(nm, "*", None, None).await
}
#[get("/search/<query>")]
#[get("/search/<query>?<page>&<results_per_page>")]
async fn search(
nm: &State<Notmuch>,
query: &str,
) -> Result<Json<SearchSummary>, Debug<NotmuchError>> {
let res = nm.search(query)?;
page: Option<usize>,
results_per_page: Option<usize>,
) -> Result<Json<shared::SearchResult>, Debug<NotmuchError>> {
let page = page.unwrap_or(0);
let results_per_page = results_per_page.unwrap_or(10);
let res = shared::SearchResult {
summary: nm.search(query, page * results_per_page, results_per_page)?,
query: query.to_string(),
page,
results_per_page,
total: nm.count(query)?,
};
Ok(Json(res))
}