Implement "since" query for fetch_latest_documents
This commit is contained in:
parent
70471f5142
commit
65a42a18bf
1 changed files with 32 additions and 10 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use axum::{
|
||||
extract::{Path, State},
|
||||
extract::{Path, Query, State},
|
||||
Json,
|
||||
};
|
||||
use axum_extra::{
|
||||
|
|
@ -9,10 +9,10 @@ use axum_extra::{
|
|||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::auth::auth;
|
||||
use super::{auth::auth, responses::FetchLatestDocumentsResponse};
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
database::models::{DocumentVersionWithoutContent, VaultId},
|
||||
database::models::{VaultId, VaultUpdateId},
|
||||
errors::{server_error, SyncServerError},
|
||||
};
|
||||
|
||||
|
|
@ -22,19 +22,41 @@ pub struct PathParams {
|
|||
vault_id: VaultId,
|
||||
}
|
||||
|
||||
// This is required for aide to infer the path parameter types and names
|
||||
#[derive(Deserialize, JsonSchema)]
|
||||
pub struct QueryParams {
|
||||
since_update_id: Option<VaultUpdateId>,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn fetch_latest_documents(
|
||||
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||
Path(PathParams { vault_id }): Path<PathParams>,
|
||||
Query(QueryParams { since_update_id }): Query<QueryParams>,
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<DocumentVersionWithoutContent>>, SyncServerError> {
|
||||
) -> Result<Json<FetchLatestDocumentsResponse>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
let latest_version = state
|
||||
.database
|
||||
.get_latest_documents(&vault_id, None)
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
let documents = if let Some(since_update_id) = since_update_id {
|
||||
state
|
||||
.database
|
||||
.get_latest_documents_since(&vault_id, since_update_id, None)
|
||||
.await
|
||||
.map_err(server_error)
|
||||
} else {
|
||||
state
|
||||
.database
|
||||
.get_latest_documents(&vault_id, None)
|
||||
.await
|
||||
.map_err(server_error)
|
||||
}?;
|
||||
|
||||
Ok(Json(latest_version))
|
||||
Ok(Json(FetchLatestDocumentsResponse {
|
||||
last_update_id: documents
|
||||
.iter()
|
||||
.map(|doc| doc.vault_update_id)
|
||||
.max()
|
||||
.unwrap_or(since_update_id.unwrap_or(0)),
|
||||
latest_documents: documents,
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue