Implemented function to update an item
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@@ -755,6 +755,12 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal"
|
||||
version = "0.2.7"
|
||||
@@ -1361,6 +1367,15 @@ dependencies = [
|
||||
"waker-fn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.3"
|
||||
@@ -1554,6 +1569,7 @@ name = "mize"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"itertools",
|
||||
"surrealdb",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
|
||||
@@ -15,6 +15,7 @@ path = "src/client-main.rs"
|
||||
|
||||
[dependencies]
|
||||
futures-util = "0.3.24"
|
||||
itertools = "0.10.5"
|
||||
surrealdb = "1.0.0-beta.7"
|
||||
tokio = {version = "1.21.0", features = ["full"]}
|
||||
tokio-stream = "0.1.9"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
use std::io;
|
||||
use surrealdb::Datastore;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub struct itemstore {
|
||||
db: surrealdb::Datastore,
|
||||
@@ -66,6 +67,29 @@ impl itemstore {
|
||||
}
|
||||
|
||||
pub async fn update(&self, id: u64, new_item: Item){
|
||||
let mut tr = self.db.transaction(true, true).await.expect("creation of transaction failed");
|
||||
|
||||
let mut keys: Vec<String> = match tr.get(format!("{}", id).into_bytes()).await {
|
||||
Ok(Some(val)) => decode(val),
|
||||
Ok(None) => {
|
||||
self.create(new_item);
|
||||
tr.cancel();
|
||||
return;
|
||||
},
|
||||
Err(e) => panic!("Error while querying the db: {}", e),
|
||||
};
|
||||
|
||||
for field in new_item {
|
||||
let key = format!("{}:{}", id, field[0]);
|
||||
let val = field[1].clone();
|
||||
keys.push(field[0].clone());
|
||||
tr.set(key.into_bytes(), val.into_bytes()).await;
|
||||
}
|
||||
|
||||
keys = keys.into_iter().unique().collect::<Vec<String>>();
|
||||
tr.set(format!("{}", id).into_bytes(), encode(keys)).await;
|
||||
|
||||
tr.commit().await;
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: u64){
|
||||
|
||||
Reference in New Issue
Block a user