From b7ff847c3c44df876fcc21e946ffff1eb781a4d9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 25 Apr 2024 13:52:23 +0200 Subject: 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. --- cli/src/api/client.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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> { - 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(()) -- cgit v1.2.3