diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/mod.rs | 7 | ||||
| -rw-r--r-- | src/api/store.rs | 27 |
2 files changed, 29 insertions, 5 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs index 8ce855e..dfefcc8 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -201,9 +201,10 @@ impl IntoResponse for ApiError { /// client's; others are logged with `context`, and are a bare `500`. pub(crate) fn store_status(error: StoreError, context: &str) -> ApiError { let status = match error { - StoreError::Conflict | StoreError::NotAList | StoreError::PastTheEnd { .. } => { - StatusCode::CONFLICT - } + StoreError::Conflict + | StoreError::NotAList + | StoreError::PastTheEnd { .. } + | StoreError::NotACounter => StatusCode::CONFLICT, StoreError::NotFound | StoreError::UnknownHeads => StatusCode::NOT_FOUND, StoreError::Rejoining => StatusCode::SERVICE_UNAVAILABLE, other => { diff --git a/src/api/store.rs b/src/api/store.rs index 8b09487..7039008 100644 --- a/src/api/store.rs +++ b/src/api/store.rs @@ -78,6 +78,13 @@ macro_rules! read_at { } #[derive(Debug, Deserialize)] +pub(crate) struct PostQuery { + /// Adds this to the counter at the path instead of inserting the body + /// (`store::increment_path`). + increment: Option<i64>, +} + +#[derive(Debug, Deserialize)] pub(crate) struct DeleteQuery { #[serde(default)] prune: bool, @@ -355,11 +362,14 @@ fn body_value(headers: &HeaderMap, body: &Bytes) -> Result<JsonValue, StatusCode /// `POST /store/{key}`: inserts the body into a list, `key` ending in the index it /// goes to (`store::insert_path`: the items from there on move up; the length, or `-1`, /// appends); `201` with where it went (`{"index": n}`). Not a list there, or an index -/// past its end: `409`, saying which. +/// past its end: `409`, saying which. With `?increment=n`, adds `n` to the counter at +/// `key` instead (`store::increment_path`), answering with its value then; not a +/// counter there: `409`. pub(crate) async fn post_store_entry( State(state): State<AppState>, Extension(user): Extension<AuthenticatedUser>, Path(key): Path<String>, + Query(query): Query<PostQuery>, RawQuery(raw_query): RawQuery, headers: HeaderMap, body: Bytes, @@ -368,9 +378,22 @@ pub(crate) async fn post_store_entry( return Err(StatusCode::SERVICE_UNAVAILABLE.into()); } let path = parse_write_path(&state, &user, &key, "POST")?; - if let Some(redirect) = to_positive(&state, &path, true, None, raw_query.as_deref()).await? { + // An increment names an item (`-1`: the last); an insert where it goes (`-1`: the end). + let insertion = query.increment.is_none(); + if let Some(redirect) = + to_positive(&state, &path, insertion, None, raw_query.as_deref()).await? + { return Ok(redirect); } + if let Some(by) = query.increment { + let value = state + .mutate_doc_for(Some(user.0.clone()), move |doc| { + store::increment_path(doc, &path, by) + }) + .await + .map_err(|e| store_status(e, "Failed to increment a counter"))?; + return response_for_value(&JsonValue::from(value), headers.get(ACCEPT)); + } let index = path.last().and_then(|leaf| store::list_index(leaf)); let value = body_value(&headers, &body)?; let inserted = state |
