REFACTOR: Dry'ing out the insert_new_note feature.
Since both `insert_page` and `insert_note` need to insert a note, having that code twice in the same block was annoying, especially since discovering that my oh-so-clever use of `include_str!` precludes me from using the `query!` macros, which want the strings included *before* doing analysis. All that wrestling with the Transaction type turned out to be much simpler when I was able to just devolve it into an Executor.
This commit is contained in:
parent
75809d821d
commit
1565fef001
|
@ -1,9 +1,10 @@
|
|||
use crate::errors::NoteStoreError;
|
||||
use crate::structs::{RawNote, RawPage};
|
||||
use friendly_id;
|
||||
use chrono;
|
||||
use friendly_id;
|
||||
use sqlx;
|
||||
use sqlx::sqlite::SqlitePool;
|
||||
use sqlx::{sqlite::Sqlite, Executor};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A handle to our Sqlite database.
|
||||
|
@ -13,6 +14,24 @@ pub struct NoteStore(Arc<SqlitePool>);
|
|||
type NoteResult<T> = core::result::Result<T, NoteStoreError>;
|
||||
type SqlResult<T> = sqlx::Result<T>;
|
||||
|
||||
async fn insert_note<'e, E>(executor: E, id: &str, content: &str, notetype: &str) -> SqlResult<i64>
|
||||
where
|
||||
E: 'e + Executor<'e, Database = Sqlite>,
|
||||
{
|
||||
let insert_one_note_sql = include_str!("sql/insert_one_note.sql");
|
||||
let now = chrono::Utc::now();
|
||||
Ok(sqlx::query(insert_one_note_sql)
|
||||
.bind(&id)
|
||||
.bind(&content)
|
||||
.bind(¬etype)
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.execute(executor)
|
||||
.await?
|
||||
.last_insert_rowid())
|
||||
}
|
||||
|
||||
impl NoteStore {
|
||||
pub async fn new(url: &str) -> NoteResult<Self> {
|
||||
let pool = SqlitePool::connect(url).await?;
|
||||
|
@ -38,46 +57,33 @@ impl NoteStore {
|
|||
}
|
||||
|
||||
pub async fn insert_note(&self, id: &str, content: &str, notetype: &str) -> SqlResult<i64> {
|
||||
let insert_one_note_sql = include_str!("sql/insert_one_note.sql");
|
||||
let now = chrono::Utc::now();
|
||||
Ok(sqlx::query(insert_one_note_sql)
|
||||
.bind(&id)
|
||||
.bind(&content)
|
||||
.bind(¬etype)
|
||||
.bind(&now).bind(&now).bind(&now)
|
||||
.execute(&*self.0).await?
|
||||
.last_insert_rowid())
|
||||
insert_note(&*self.0, id, content, notetype).await
|
||||
}
|
||||
|
||||
// TODO: We're returning the raw page with the raw note id, note
|
||||
// the friendly ID. Is there a disconnect there? It's making me
|
||||
// furiously to think.
|
||||
pub async fn insert_page(&self, id: &str, title: &str) -> SqlResult<i64> {
|
||||
let insert_one_note_sql = include_str!("sql/insert_one_note.sql");
|
||||
let insert_one_page_sql = include_str!("sql/insert_one_page.sql");
|
||||
let new_note_id = friendly_id::create();
|
||||
let now = chrono::Utc::now();
|
||||
|
||||
let mut tx = self.0.begin().await?;
|
||||
|
||||
let note_id = sqlx::query(insert_one_note_sql)
|
||||
.bind(&new_note_id)
|
||||
.bind(&"")
|
||||
.bind(&"page")
|
||||
.bind(&now).bind(&now).bind(&now)
|
||||
.execute(&mut tx).await?
|
||||
.last_insert_rowid();
|
||||
let note_id = insert_note(&mut tx, &new_note_id, &"", &"page").await?;
|
||||
|
||||
let page_id = sqlx::query(insert_one_page_sql)
|
||||
.bind(&id)
|
||||
.bind(&title)
|
||||
.bind(¬e_id)
|
||||
.bind(&now).bind(&now).bind(&now)
|
||||
.execute(&mut tx).await?
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.bind(&now)
|
||||
.execute(&mut tx)
|
||||
.await?
|
||||
.last_insert_rowid();
|
||||
|
||||
tx.commit().await?;
|
||||
Ok(page_id)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue