diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-04-25 13:52:23 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-04-25 13:52:23 +0200 |
commit | b7ff847c3c44df876fcc21e946ffff1eb781a4d9 (patch) | |
tree | 1e642543c2c36f6b9269a5601dbc13e4552d817f | |
parent | a4dbf815cc418514a268ca54464c2d1b9c413055 (diff) | |
download | faktura-b7ff847c3c44df876fcc21e946ffff1eb781a4d9.tar.gz faktura-b7ff847c3c44df876fcc21e946ffff1eb781a4d9.tar.bz2 faktura-b7ff847c3c44df876fcc21e946ffff1eb781a4d9.zip |
Strip `id` column when saving clients.
Inserting multiple clients with the same id does not work, so we remove
it and let the db backend assign the id instead.
-rw-r--r-- | cli/src/api/client.rs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/cli/src/api/client.rs b/cli/src/api/client.rs index 9c46db3..c77063b 100644 --- a/cli/src/api/client.rs +++ b/cli/src/api/client.rs @@ -4,6 +4,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use serde::{Deserialize, Serialize}; +use serde_json::json; use std::error::Error; use super::Connection; @@ -28,7 +29,20 @@ impl Client { } pub fn save(&self, conn: Connection) -> Result<(), Box<dyn Error>> { - let resp = conn.post("clients", &self)?; + // By default the `id` of a newly created client will be 0, + // which will cause the API to add the client with id=0. This works once + // but then fails for the next clients added. + // + // To fix this, we remove the `id` from the json payload before + // we pass it to the API. + let mut json = json!(&self) + .as_object() + .ok_or("not an object")? + .clone(); + + json.remove("id"); + + let resp = conn.post("clients", &json)?; println!("{}", resp); Ok(()) |