Function to get an item implemented

This commit is contained in:
Sebastian Moser
2022-09-21 12:31:17 +02:00
parent 5630e7ec8e
commit ccb53f8abb

View File

@@ -1,5 +1,5 @@
use std::io; use std::{io, fmt::format};
use surrealdb::Datastore; use surrealdb::Datastore;
use itertools::Itertools; use itertools::Itertools;
@@ -72,8 +72,8 @@ impl itemstore {
let mut keys: Vec<String> = match tr.get(format!("{}", id).into_bytes()).await { let mut keys: Vec<String> = match tr.get(format!("{}", id).into_bytes()).await {
Ok(Some(val)) => decode(val), Ok(Some(val)) => decode(val),
Ok(None) => { Ok(None) => {
self.create(new_item);
tr.cancel(); tr.cancel();
self.create(new_item);
return; return;
}, },
Err(e) => panic!("Error while querying the db: {}", e), Err(e) => panic!("Error while querying the db: {}", e),
@@ -95,10 +95,31 @@ impl itemstore {
pub async fn delete(&self, id: u64){ pub async fn delete(&self, id: u64){
} }
//pub async fn get(&self, id: u64) -> Item { pub async fn get(&self, id: u64) -> Item {
//let mut tr = self.db.transaction(false, false).await.expect("creation of transaction failed"); let mut tr = self.db.transaction(false, false).await.expect("creation of transaction failed");
//} let mut item: Item = Vec::new();
let mut keys: Vec<String> = match tr.get(format!("{}", id).into_bytes()).await {
Ok(Some(val)) => decode(val),
Ok(None) => {
panic!("index of item {} not found", id);
},
Err(e) => panic!("Error while querying the db: {}", e),
};
for key in keys {
let mut field: [String; 2] = [String::new(), String::new()];
field[0] = key.clone();
field[1] = match tr.get(format!("{}:{}", id, key)).await {
Ok(Some(val)) => String::from_utf8(val)
.expect("error converting value to utf-8 String"),
Ok(None) => panic!("a field that should be there wasn't"),
Err(e) => panic!("Error while querying the db: {}", e),
};
item.push(field);
}
return item;
}
} }
pub fn decode(bytes: Vec<u8>) -> Vec<String> { pub fn decode(bytes: Vec<u8>) -> Vec<String> {