diff --git a/src/crypt.rs b/src/crypt.rs index db6f592..1d40ae7 100644 --- a/src/crypt.rs +++ b/src/crypt.rs @@ -17,7 +17,6 @@ use rustc_serialize::base64; use rustc_serialize::base64::{FromBase64, ToBase64}; use std::convert::TryFrom; use std::convert::TryInto; -use std::error::Error; use std::io; pub struct EncryptedValue { diff --git a/src/db.rs b/src/db.rs index b6a5a10..d79c916 100644 --- a/src/db.rs +++ b/src/db.rs @@ -100,20 +100,34 @@ impl DbHandle { } pub fn write_note(&mut self, master_key: [u8; 32], note: Note) { + let existing_notes = self.read_notes(master_key).unwrap(); let namespace_id = self.get_namespace_id(¬e.namespace, master_key).unwrap(); + let tx = self.conn.transaction().unwrap(); + + for existing_note in existing_notes { + if existing_note.namespace == note.namespace + && existing_note.category == note.category + && existing_note.title == note.title + { + tx.execute("DELETE FROM notes WHERE id=$1;", params![existing_note.id]) + .unwrap(); + } + } + let encrypted_title = crypt::encrypt_value(¬e.title, master_key); let encrypted_value = crypt::encrypt_value(¬e.value, master_key); - self.conn - .execute( - "INSERT INTO notes (namespace, category, title, value) VALUES ($1, $2, $3, $4);", - params![ - namespace_id, - note.category, - encrypted_title, - encrypted_value - ], - ) - .unwrap(); + tx.execute( + "INSERT INTO notes (namespace, category, title, value) VALUES ($1, $2, $3, $4);", + params![ + namespace_id, + note.category, + encrypted_title, + encrypted_value + ], + ) + .unwrap(); + + let _ = tx.commit().unwrap(); } pub fn read_notes(&mut self, master_key: [u8; 32]) -> rusqlite::Result> { diff --git a/src/main.rs b/src/main.rs index 1d7d411..e38f94d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,12 +143,18 @@ fn set(mut db_conn: db::DbHandle, master_key: [u8; 32]) { } fn transfer(mut db_conn: db::DbHandle, master_key: [u8; 32]) { - for host in db_conn - .list_accounts(master_key) - .into_iter() - .map(|account: db::Account| account.host) - { - println!("{}", host); + for account in db_conn.list_accounts(master_key).into_iter() { + let new_note = db::Note { + id: 0, + namespace: "main".to_owned(), + category: "account".to_owned(), + title: account.host, + value: format!( + "username: {}\npassword:{}\n", + account.user, account.password + ), + }; + db_conn.write_note(master_key, new_note); } }