Adding components and assets.

Commiting to try the library with the target dioxus project.
This commit is contained in:
2025-09-06 15:05:58 -04:00
parent e6f3ff3c1e
commit 9679578977
10 changed files with 743 additions and 38 deletions

View File

@ -1,27 +1,70 @@
use dioxus::prelude::*;
// Remember that Server functions parameters must not be references.
// They are coming from potentially other computers.
#[cfg(feature = "server")]
use std::sync::Arc;
use crate::{Database, Lore, Tale};
use dioxus::prelude::*;
#[cfg(feature = "server")]
use tokio::sync::OnceCell;
use tavern::{Adventurer, Lore, Tale};
#[cfg(feature = "server")]
use tavern::Database;
#[cfg(feature = "server")]
static BLOG_DATABASE: OnceCell<Arc<Database>> = OnceCell::const_new();
#[cfg(feature = "server")]
async fn get_database_instance() -> &'static Arc<Database>
{
BLOG_DATABASE.get_or_init(|| async {
let db = Database::open("tavern.db")
.await
.expect("Failed to open database");
Arc::new(db)
}).await
}
#[server]
pub async fn get_blog_list() -> Result<Vec<Lore>, ServerFnError> {
let db = server_context()
.get::<Arc<Database>>()
.ok_or_else(|| ServerFnError::ServerError("Database context not available".to_string()))?;
pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerFnError>
{
let db = get_database_instance().await;
let summaries = db.get_tales_summary(&[]).await
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
let summaries = db.get_tales_summary(&categories).await
.map_err(|e| ServerFnError::new(e))?;
Ok(summaries)
}
#[server]
pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError> {
let db = server_context()
.get::<Arc<Database>>()
.ok_or_else(|| ServerFnError::ServerError("Database context not available".to_string()))?;
pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError>
{
let db = get_database_instance().await;
let tale = db.get_tale_by_slug(&slug).await
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
.map_err(|e| ServerFnError::new(e))?;
tale.ok_or(ServerFnError::ServerError("Post not found".into()))
tale.ok_or(ServerFnError::new(format!("Post {} not found", slug)))
}
#[server]
pub async fn get_author(handle: String) -> Result<Adventurer, ServerFnError>
{
let db = get_database_instance().await;
let author = db.get_adventurer(&handle).await
.map_err(|e| ServerFnError::new(e))?;
author.ok_or(ServerFnError::new(format!("Author {} not found.", handle)))
}