transfer function
This commit is contained in:
parent
38c67e3c27
commit
121f3bb9cd
@ -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 {
|
||||
|
36
src/db.rs
36
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<Vec<Note>> {
|
||||
|
18
src/main.rs
18
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user