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::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, Query, State},
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
use axum_extra::{
|
use axum_extra::{
|
||||||
|
|
@ -9,10 +9,10 @@ use axum_extra::{
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::auth::auth;
|
use super::{auth::auth, responses::FetchLatestDocumentsResponse};
|
||||||
use crate::{
|
use crate::{
|
||||||
app_state::AppState,
|
app_state::AppState,
|
||||||
database::models::{DocumentVersionWithoutContent, VaultId},
|
database::models::{VaultId, VaultUpdateId},
|
||||||
errors::{server_error, SyncServerError},
|
errors::{server_error, SyncServerError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -22,19 +22,41 @@ pub struct PathParams {
|
||||||
vault_id: VaultId,
|
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]
|
#[axum::debug_handler]
|
||||||
pub async fn fetch_latest_documents(
|
pub async fn fetch_latest_documents(
|
||||||
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path(PathParams { vault_id }): Path<PathParams>,
|
Path(PathParams { vault_id }): Path<PathParams>,
|
||||||
|
Query(QueryParams { since_update_id }): Query<QueryParams>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
) -> Result<Json<Vec<DocumentVersionWithoutContent>>, SyncServerError> {
|
) -> Result<Json<FetchLatestDocumentsResponse>, SyncServerError> {
|
||||||
auth(&state, auth_header.token())?;
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let latest_version = state
|
let documents = if let Some(since_update_id) = since_update_id {
|
||||||
.database
|
state
|
||||||
.get_latest_documents(&vault_id, None)
|
.database
|
||||||
.await
|
.get_latest_documents_since(&vault_id, since_update_id, None)
|
||||||
.map_err(server_error)?;
|
.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