This commit is contained in:
Hamcha 2025-01-25 19:14:49 +01:00
parent 2a78c4429b
commit ab9a460d0b
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
2 changed files with 96 additions and 103 deletions
src
main.rs
outbound/repository/adapters

View file

@ -15,93 +15,93 @@ fn main() {
let mut builder = dioxus::LaunchBuilder::new(); let mut builder = dioxus::LaunchBuilder::new();
server_only! { server_only! {
use outbound::repository::adapters::memory::InMemoryStore;
use outbound::services::site::SiteService; use outbound::services::site::SiteService;
let store = tokio::runtime::Runtime::new().unwrap().block_on(setup_inmem_store());
builder = builder.with_context_provider(|| { builder = builder.with_context_provider(move || {
Box::new(SiteService::new(store.clone()))
let store = tokio::runtime::Builder::new_multi_thread() })
.enable_all()
.build()
.unwrap()
.block_on(async move {
let store = InMemoryStore::new();
store
.add_site(outbound::repository::site::SiteMetadata {
domain: "localhost".to_string(),
title: "Test site".to_string(),
})
.await;
store
.add_page(
"localhost",
domain::entities::site::Page {
info: domain::entities::site::PageInfo {
title: "Home".to_string(),
name: "/".to_string(),
order: 0,
},
content: domain::entities::site::PageContent::Single {
content: domain::entities::site::Post {
blocks: vec![domain::entities::site::Block::Text {
text: "Hello, world!".to_string(),
}],
},
},
},
)
.await;
store
.add_page(
"localhost",
domain::entities::site::Page {
info: domain::entities::site::PageInfo {
title: "Cool page".to_string(),
name: "cool".to_string(),
order: 10,
},
content: domain::entities::site::PageContent::Collection {
kind: domain::entities::site::CollectionKind::Blog,
collection_id: "cool-posts".to_string(),
},
},
)
.await;
store
.add_post(
"localhost",
"cool-posts",
"test_id",
domain::entities::site::Post {
blocks: vec![domain::entities::site::Block::Text {
text: "This is a cool post!".to_string(),
}],
},
)
.await;
store
.add_post(
"localhost",
"cool-posts",
"test_id_2",
domain::entities::site::Post {
blocks: vec![
domain::entities::site::Block::Text {
text: "This is another cool post!".to_string(),
},
domain::entities::site::Block::Text {
text: "With two blocks!".to_string(),
},
],
},
)
.await;
store
});
Box::new(SiteService::new(store))
});
} }
builder.launch(inbound::renderer::App); builder.launch(inbound::renderer::App);
} }
#[cfg(feature = "server")]
async fn setup_inmem_store() -> outbound::repository::adapters::memory::InMemoryStore {
use outbound::repository::site::SiteRepository;
let store = outbound::repository::adapters::memory::InMemoryStore::new();
store
.add_site(outbound::repository::site::SiteMetadata {
domain: "localhost".to_string(),
title: "Test site".to_string(),
})
.await;
store
.set_page(
"localhost",
domain::entities::site::Page {
info: domain::entities::site::PageInfo {
title: "Home".to_string(),
name: "/".to_string(),
order: 0,
},
content: domain::entities::site::PageContent::Single {
content: domain::entities::site::Post {
blocks: vec![domain::entities::site::Block::Text {
text: "Hello, world!".to_string(),
}],
},
},
},
)
.await
.unwrap();
store
.set_page(
"localhost",
domain::entities::site::Page {
info: domain::entities::site::PageInfo {
title: "Cool page".to_string(),
name: "cool".to_string(),
order: 10,
},
content: domain::entities::site::PageContent::Collection {
kind: domain::entities::site::CollectionKind::Blog,
collection_id: "cool-posts".to_string(),
},
},
)
.await
.unwrap();
store
.add_post(
"localhost",
"cool-posts",
"test_id",
domain::entities::site::Post {
blocks: vec![domain::entities::site::Block::Text {
text: "This is a cool post!".to_string(),
}],
},
)
.await;
store
.add_post(
"localhost",
"cool-posts",
"test_id_2",
domain::entities::site::Post {
blocks: vec![
domain::entities::site::Block::Text {
text: "This is another cool post!".to_string(),
},
domain::entities::site::Block::Text {
text: "With two blocks!".to_string(),
},
],
},
)
.await;
store
}

View file

@ -20,6 +20,7 @@ struct InMemoryStoreData {
posts: HashMap<(String, String), HashMap<String, Post>>, posts: HashMap<(String, String), HashMap<String, Post>>,
} }
#[derive(Clone)]
pub struct InMemoryStore { pub struct InMemoryStore {
data: Arc<Mutex<InMemoryStoreData>>, data: Arc<Mutex<InMemoryStoreData>>,
} }
@ -43,14 +44,6 @@ impl InMemoryStore {
.insert(site.domain.clone(), site); .insert(site.domain.clone(), site);
} }
pub async fn add_page(&self, site: &str, page: Page) {
self.data
.lock()
.await
.pages
.insert((site.to_string(), page.info.name.clone()), page);
}
pub async fn add_post(&self, site: &str, collection_id: &str, post_id: &str, post: Post) { pub async fn add_post(&self, site: &str, collection_id: &str, post_id: &str, post: Post) {
self.data self.data
.lock() .lock()
@ -73,8 +66,9 @@ impl InMemoryStore {
title: "Test site".to_string(), title: "Test site".to_string(),
}) })
.await; .await;
store store
.add_page( .set_page(
"example.com", "example.com",
Page { Page {
info: PageInfo { info: PageInfo {
@ -91,10 +85,11 @@ impl InMemoryStore {
}, },
}, },
) )
.await; .await
.unwrap();
store store
.add_page( .set_page(
"example.com", "example.com",
Page { Page {
info: PageInfo { info: PageInfo {
@ -111,7 +106,8 @@ impl InMemoryStore {
}, },
}, },
) )
.await; .await
.unwrap();
store store
.add_post( .add_post(
@ -186,7 +182,7 @@ impl SiteRepository for InMemoryStore {
.lock() .lock()
.await .await
.pages .pages
.insert((domain.to_string(), page.info.name.clone()), page); .insert((domain.to_string(), page.info.name.clone()), page.clone());
Ok(()) Ok(())
} }
@ -263,10 +259,7 @@ mod tests {
cursor::CursorOptions, cursor::CursorOptions,
site::{Block, Page, PageContent, PageInfo, Post}, site::{Block, Page, PageContent, PageInfo, Post},
}, },
outbound::repository::{ outbound::repository::{self, site::SiteRepository},
self,
site::{SiteMetadata, SiteRepository},
},
}; };
use super::InMemoryStore; use super::InMemoryStore;