Added Function to create an Item

This commit is contained in:
Sebastian Moser
2022-09-20 23:53:13 +02:00
parent 87ebad4bce
commit ec990fd370
2 changed files with 53 additions and 2 deletions

View File

@@ -100,7 +100,6 @@ async fn warp_server(mize_folder: String) {
// get itemstore // get itemstore
let itemstore = crate::server::itemstore::itemstore::new(mize_folder + "/db").await; let itemstore = crate::server::itemstore::itemstore::new(mize_folder + "/db").await;
println!("haöasldjkfsdfi");
let mut clients: Arc<Mutex<Vec<Client>>> = Arc::new(Mutex::new(Vec::new())); let mut clients: Arc<Mutex<Vec<Client>>> = Arc::new(Mutex::new(Vec::new()));

View File

@@ -12,17 +12,69 @@ pub type Item = Vec<[String; 2]>;
impl itemstore { impl itemstore {
pub async fn new(path: String) -> itemstore { pub async fn new(path: String) -> itemstore {
let ds = Datastore::new(&(String::from("file://") + &path[..] + "/db")[..]).await.expect("Could not open database"); let ds = Datastore::new(&(String::from("file://") + &path[..] + "/db")[..]).await.expect("Could not open database");
println!("in new fn");
let mut tr = ds.transaction(true, false).await.expect("creation of transaction failed"); let mut tr = ds.transaction(true, false).await.expect("creation of transaction failed");
match tr.get(vec!('0' as u8)).await { match tr.get(vec!('0' as u8)).await {
Ok(Some(val)) => (), Ok(Some(val)) => println!("item 0 already created"),
Ok(None) => { Ok(None) => {
let init = vec!(String::from("num_of_items"), String::from("next_free_id"));
tr.set("0".to_string().into_bytes(), encode(init)).await;
tr.set("0:num_of_items".to_string().into_bytes(), vec!('1' as u8)).await;
tr.set("0:next_free_id".to_string().into_bytes(), vec!('1' as u8)).await;
tr.commit().await;
}, },
Err(e) => println!("Error while getting item index with id 0: {}", e) Err(e) => println!("Error while getting item index with id 0: {}", e)
}; };
return itemstore {db:ds}; return itemstore {db:ds};
} }
pub async fn create(&self, item: Item) -> u64 {
let mut tr = self.db.transaction(true, true).await.expect("creation of transaction failed");
let id = String::from_utf8(tr.get("0:next_free_id").await
.expect("no item with id 0 when there definetly should be one")
.expect("error reading item 0 from db"))
.expect("error converting from utf-8 to String");
let mut keys: Vec<String> = Vec::new();
for field in 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;
}
tr.set(id.clone().into_bytes(), encode(keys)).await;
//increment 0:number_of_items by 1
let num = tr.get("0:num_of_items".to_string().into_bytes()).await
.expect("error getting \"0:num_of_items\"")
.expect("error reading item 0 from db");
let mut num_u64 = u64::from_be_bytes([num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]]) +1;
tr.set("0:num_of_items".to_string().into_bytes(), num);
//imcrement next_free_id by 1
let num = tr.get("0:next_free_id".to_string().into_bytes()).await
.expect("error getting \"0:next_free_id\"")
.expect("error reading item 0 from db");
let mut num_u64 = u64::from_be_bytes([num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]]) +1;
tr.set("0:next_free_id".to_string().into_bytes(), num);
tr.commit().await;
let idvec = id.into_bytes();
return u64::from_be_bytes([idvec[0], idvec[1], idvec[2], idvec[3], idvec[4], idvec[5], idvec[6], idvec[7], ])
}
pub async fn update(&self, id: u64, new_item: Item){
}
pub async fn delete(&self, id: u64){
}
//pub async fn get(&self, id: u64) -> Item {
//let mut tr = self.db.transaction(false, false).await.expect("creation of transaction failed");
//}
} }
pub fn decode(bytes: Vec<u8>) -> Vec<String> { pub fn decode(bytes: Vec<u8>) -> Vec<String> {