Adjusting to make the database better tested.
This commit is contained in:
@ -4,20 +4,20 @@ use std::str::FromStr;
|
||||
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
|
||||
use sqlx::{Error, Result};
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
use sqlx::Row;
|
||||
|
||||
use crate::adventurer::Adventurer;
|
||||
use crate::tale::Tale;
|
||||
use crate::tale::{Story, Tale};
|
||||
|
||||
#[cfg(feature = "publisher")]
|
||||
use crate::converter::Converter;
|
||||
#[cfg(feature = "publisher")]
|
||||
use crate::tavern::Tavern;
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
use crate::tale::Lore;
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
use crate::adventurer::Legend;
|
||||
|
||||
|
||||
@ -140,13 +140,24 @@ impl Database
|
||||
-> Result<()>
|
||||
{
|
||||
// Convert the tales content from Markdown to HTML.
|
||||
let markdown_content = std::fs::read_to_string(&tale.story)?;
|
||||
let html_content: String = Converter::markdown_to_html(&markdown_content);
|
||||
let html_content: std::borrow::Cow<'_, str> = match &tale.story
|
||||
{
|
||||
Story::Html(story) => { std::borrow::Cow::Borrowed(story) }
|
||||
|
||||
Story::File(path) =>
|
||||
{
|
||||
let markdown = std::fs::read_to_string(path)?;
|
||||
std::borrow::Cow::Owned(Converter::markdown_to_html(&markdown))
|
||||
}
|
||||
};
|
||||
|
||||
// Start a transaction.
|
||||
let mut tx = self.pool.begin().await?;
|
||||
|
||||
// Store the tale.
|
||||
// Pull the HTML content out first so that the str will last long enough.
|
||||
// This get's around the macro lifetime issue.
|
||||
let html_str = html_content.as_ref();
|
||||
sqlx::query!(
|
||||
"INSERT OR REPLACE INTO tales (
|
||||
slug, title, author, summary, publish_date, content
|
||||
@ -156,7 +167,7 @@ impl Database
|
||||
tale.lore.author,
|
||||
tale.lore.summary,
|
||||
tale.lore.publish_date,
|
||||
html_content
|
||||
html_str
|
||||
).execute(&mut *tx) // Pass mutable reference to the transaction
|
||||
.await?;
|
||||
|
||||
@ -263,7 +274,7 @@ impl Database
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
pub async fn get_tales_summary(&self, categories: &[String])
|
||||
-> Result<Vec<Lore>>
|
||||
{
|
||||
@ -312,7 +323,7 @@ impl Database
|
||||
.unwrap_or_default();
|
||||
|
||||
let date_str: String = row.try_get("publish_date")?;
|
||||
let publish_date = chrono::NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%dT%H:%M:%S")
|
||||
let publish_date = chrono::NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S")
|
||||
.map_err(|e| sqlx::Error::Decode(e.into()))?;
|
||||
|
||||
tales.push(Lore { title: row.try_get("title")?,
|
||||
@ -328,7 +339,7 @@ impl Database
|
||||
Ok(tales)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
pub async fn get_tale_by_slug(&self, slug: &str) -> Result<Option<Tale>>
|
||||
{
|
||||
let mut tx = self.pool.begin().await?;
|
||||
@ -356,7 +367,7 @@ impl Database
|
||||
.unwrap_or_default();
|
||||
|
||||
let date_str: String = row.try_get("publish_date")?;
|
||||
let publish_date = chrono::NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%dT%H:%M:%S")
|
||||
let publish_date = chrono::NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S")
|
||||
.map_err(|e| Error::Decode(e.into()))?;
|
||||
|
||||
let lore = Lore { title: row.try_get("title")?,
|
||||
@ -367,7 +378,7 @@ impl Database
|
||||
tags };
|
||||
|
||||
Ok(Some(Tale { lore,
|
||||
story: row.try_get("content")? }))
|
||||
story: Story::Html(row.try_get("content")?) }))
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -375,7 +386,7 @@ impl Database
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "publisher"))]
|
||||
#[cfg(any(not(feature = "publisher"), feature = "tester"))]
|
||||
pub async fn get_adventurer(&self, handle: &str)
|
||||
-> Result<Option<Adventurer>>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user