Initial HerbAPI implementation
Rust/Axum REST API (herbapi-api) with PostgreSQL, S3/Garage, OIDC auth. Dioxus 0.7 WASM frontend (herbapi-ui) with sidebar layout and botanical reference style. 9 SQL migrations covering families, species, cultivars, suppliers, companions, images, users, API tokens.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
use gloo_net::http::Request;
|
||||
use serde::de::DeserializeOwned;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::types::*;
|
||||
|
||||
const API_BASE: &str = "/api/v1";
|
||||
|
||||
async fn get_json<T: DeserializeOwned>(path: &str) -> Result<T, String> {
|
||||
let resp = Request::get(path)
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Request failed: {e}"))?;
|
||||
|
||||
if !resp.ok() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(format!("HTTP {}: {}", resp.status(), body));
|
||||
}
|
||||
|
||||
resp.json().await.map_err(|e| format!("JSON parse error: {e}"))
|
||||
}
|
||||
|
||||
async fn post_json<B: serde::Serialize, T: DeserializeOwned>(
|
||||
path: &str,
|
||||
body: &B,
|
||||
) -> Result<T, String> {
|
||||
let resp = Request::post(path)
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(serde_json::to_string(body).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| format!("Request build error: {e}"))?
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Request failed: {e}"))?;
|
||||
|
||||
if !resp.ok() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(format!("HTTP {}: {}", resp.status(), body));
|
||||
}
|
||||
|
||||
resp.json().await.map_err(|e| format!("JSON parse error: {e}"))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn put_json<B: serde::Serialize, T: DeserializeOwned>(
|
||||
path: &str,
|
||||
body: &B,
|
||||
) -> Result<T, String> {
|
||||
let resp = Request::put(path)
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(serde_json::to_string(body).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| format!("Request build error: {e}"))?
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Request failed: {e}"))?;
|
||||
|
||||
if !resp.ok() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(format!("HTTP {}: {}", resp.status(), body));
|
||||
}
|
||||
|
||||
resp.json().await.map_err(|e| format!("JSON parse error: {e}"))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn delete_req(path: &str) -> Result<(), String> {
|
||||
let resp = Request::delete(path)
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Request failed: {e}"))?;
|
||||
|
||||
if !resp.ok() {
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
return Err(format!("HTTP {}: {}", resp.status(), body));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --- Auth ---
|
||||
pub async fn get_current_user() -> Result<MeResponse, String> {
|
||||
get_json("/auth/me").await
|
||||
}
|
||||
|
||||
// --- Families ---
|
||||
pub async fn list_families(page: i64, search: Option<&str>) -> Result<PaginatedResponse<Family>, String> {
|
||||
let mut url = format!("{API_BASE}/families?page={page}&per_page=25");
|
||||
if let Some(q) = search {
|
||||
url.push_str(&format!("&search={q}"));
|
||||
}
|
||||
get_json(&url).await
|
||||
}
|
||||
|
||||
pub async fn get_family(slug: &str) -> Result<Family, String> {
|
||||
get_json(&format!("{API_BASE}/families/{slug}")).await
|
||||
}
|
||||
|
||||
// --- Species ---
|
||||
pub async fn list_species(page: i64, family: Option<&str>, search: Option<&str>) -> Result<PaginatedResponse<Species>, String> {
|
||||
let mut url = format!("{API_BASE}/species?page={page}&per_page=25");
|
||||
if let Some(f) = family {
|
||||
url.push_str(&format!("&family={f}"));
|
||||
}
|
||||
if let Some(q) = search {
|
||||
url.push_str(&format!("&search={q}"));
|
||||
}
|
||||
get_json(&url).await
|
||||
}
|
||||
|
||||
pub async fn get_species(slug: &str) -> Result<Species, String> {
|
||||
get_json(&format!("{API_BASE}/species/{slug}")).await
|
||||
}
|
||||
|
||||
// --- Cultivars ---
|
||||
pub async fn list_cultivars(page: i64, species: Option<&str>, search: Option<&str>) -> Result<PaginatedResponse<Cultivar>, String> {
|
||||
let mut url = format!("{API_BASE}/cultivars?page={page}&per_page=25");
|
||||
if let Some(s) = species {
|
||||
url.push_str(&format!("&species={s}"));
|
||||
}
|
||||
if let Some(q) = search {
|
||||
url.push_str(&format!("&search={q}"));
|
||||
}
|
||||
get_json(&url).await
|
||||
}
|
||||
|
||||
pub async fn get_cultivar(slug: &str) -> Result<Cultivar, String> {
|
||||
get_json(&format!("{API_BASE}/cultivars/{slug}")).await
|
||||
}
|
||||
|
||||
// --- Suppliers ---
|
||||
pub async fn list_suppliers() -> Result<Vec<Supplier>, String> {
|
||||
get_json(&format!("{API_BASE}/suppliers")).await
|
||||
}
|
||||
|
||||
pub async fn get_supplier(slug: &str) -> Result<Supplier, String> {
|
||||
get_json(&format!("{API_BASE}/suppliers/{slug}")).await
|
||||
}
|
||||
|
||||
pub async fn get_cultivar_suppliers(id: Uuid) -> Result<Vec<CultivarSupplier>, String> {
|
||||
get_json(&format!("{API_BASE}/cultivars/{id}/suppliers")).await
|
||||
}
|
||||
|
||||
// --- Companions ---
|
||||
pub async fn get_companions(species_id: Uuid) -> Result<Vec<CompanionRelationship>, String> {
|
||||
get_json(&format!("{API_BASE}/species/{species_id}/companions")).await
|
||||
}
|
||||
|
||||
// --- Images ---
|
||||
pub async fn get_images(entity_type: &str, entity_id: Uuid) -> Result<Vec<Image>, String> {
|
||||
get_json(&format!("{API_BASE}/images/{entity_type}/{entity_id}")).await
|
||||
}
|
||||
|
||||
// --- Search ---
|
||||
pub async fn search(query: &str, limit: i64) -> Result<Vec<SearchResult>, String> {
|
||||
get_json(&format!("{API_BASE}/search?q={query}&limit={limit}")).await
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::types::MeResponse;
|
||||
|
||||
#[derive(Routable, Clone, Debug, PartialEq)]
|
||||
#[rustfmt::skip]
|
||||
pub enum Route {
|
||||
#[layout(Layout)]
|
||||
#[route("/")]
|
||||
Home {},
|
||||
#[route("/families")]
|
||||
FamilyList {},
|
||||
#[route("/families/:slug")]
|
||||
FamilyDetail { slug: String },
|
||||
#[route("/species")]
|
||||
SpeciesList {},
|
||||
#[route("/species/:slug")]
|
||||
SpeciesDetail { slug: String },
|
||||
#[route("/cultivars")]
|
||||
CultivarList {},
|
||||
#[route("/cultivars/:slug")]
|
||||
CultivarDetail { slug: String },
|
||||
#[route("/suppliers")]
|
||||
SupplierList {},
|
||||
#[route("/suppliers/:slug")]
|
||||
SupplierDetail { slug: String },
|
||||
#[route("/search")]
|
||||
SearchPage {},
|
||||
#[end_layout]
|
||||
#[route("/:..segments")]
|
||||
NotFound { segments: Vec<String> },
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn App() -> Element {
|
||||
rsx! {
|
||||
Router::<Route> {}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Layout() -> Element {
|
||||
// Try to get current user (may be None for public access)
|
||||
let auth = use_resource(|| async { api::get_current_user().await.ok() });
|
||||
let user: Option<MeResponse> = auth.read().as_ref().and_then(|r| r.clone());
|
||||
|
||||
rsx! {
|
||||
div { class: "app-layout",
|
||||
nav { class: "sidebar",
|
||||
div { class: "sidebar-brand",
|
||||
span { class: "brand-icon", "\u{1F33F}" }
|
||||
div { class: "brand-text-group",
|
||||
span { class: "brand-text", "HerbAPI" }
|
||||
span { class: "brand-sub", "Plant Database" }
|
||||
}
|
||||
}
|
||||
div { class: "sidebar-nav",
|
||||
NavLink { to: Route::Home {}, label: "Home" }
|
||||
NavLink { to: Route::FamilyList {}, label: "Families" }
|
||||
NavLink { to: Route::SpeciesList {}, label: "Species" }
|
||||
NavLink { to: Route::CultivarList {}, label: "Cultivars" }
|
||||
NavLink { to: Route::SupplierList {}, label: "Suppliers" }
|
||||
NavLink { to: Route::SearchPage {}, label: "Search" }
|
||||
}
|
||||
div { class: "sidebar-user",
|
||||
if let Some(ref u) = user {
|
||||
span { class: "user-name", "{u.nickname.as_deref().or(u.name.as_deref()).unwrap_or(&u.email)}" }
|
||||
a { class: "logout-link", href: "/auth/oidc/logout", "Logout" }
|
||||
} else {
|
||||
a { class: "login-link", href: "/auth/oidc/login", "Login" }
|
||||
}
|
||||
}
|
||||
}
|
||||
main { class: "content",
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn NavLink(to: Route, label: &'static str) -> Element {
|
||||
rsx! {
|
||||
Link { to: to, class: "nav-link",
|
||||
span { class: "nav-label", "{label}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn NotFound(segments: Vec<String>) -> Element {
|
||||
rsx! {
|
||||
div { class: "not-found",
|
||||
h1 { "404" }
|
||||
p { "Page not found: /{segments.join(\"/\")}" }
|
||||
Link { to: Route::Home {}, "Back to Home" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Re-export page components for the router
|
||||
pub use crate::pages::cultivars::{CultivarDetail, CultivarList};
|
||||
pub use crate::pages::families::{FamilyDetail, FamilyList};
|
||||
pub use crate::pages::home::Home;
|
||||
pub use crate::pages::search::SearchPage;
|
||||
pub use crate::pages::species::{SpeciesDetail, SpeciesList};
|
||||
pub use crate::pages::suppliers::{SupplierDetail, SupplierList};
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod plant_card;
|
||||
pub mod planting_calendar;
|
||||
@@ -0,0 +1,24 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::app::Route;
|
||||
|
||||
#[component]
|
||||
pub fn PlantCard(slug: String, name: String, name_common: Option<String>, entity_type: String) -> Element {
|
||||
let route = match entity_type.as_str() {
|
||||
"species" => Route::SpeciesDetail { slug: slug.clone() },
|
||||
"cultivar" => Route::CultivarDetail { slug: slug.clone() },
|
||||
"family" => Route::FamilyDetail { slug: slug.clone() },
|
||||
_ => Route::Home {},
|
||||
};
|
||||
|
||||
rsx! {
|
||||
div { class: "plant-card",
|
||||
Link { to: route,
|
||||
em { class: "card-scientific", "{name}" }
|
||||
}
|
||||
if let Some(ref common) = name_common {
|
||||
p { class: "card-common", "{common}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
const MONTH_LABELS: [&str; 12] = ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"];
|
||||
|
||||
#[component]
|
||||
pub fn PlantingCalendar(
|
||||
indoor_sowing: Option<Vec<i32>>,
|
||||
direct_sowing: Option<Vec<i32>>,
|
||||
transplanting: Option<Vec<i32>>,
|
||||
glasshouse: Option<Vec<i32>>,
|
||||
harvesting: Option<Vec<i32>>,
|
||||
) -> Element {
|
||||
let rows: Vec<(&str, &str, &Option<Vec<i32>>)> = vec![
|
||||
("Indoor Sowing", "cal-indoor", &indoor_sowing),
|
||||
("Direct Sowing", "cal-direct", &direct_sowing),
|
||||
("Transplanting", "cal-transplant", &transplanting),
|
||||
("Glasshouse", "cal-glass", &glasshouse),
|
||||
("Harvesting", "cal-harvest", &harvesting),
|
||||
];
|
||||
|
||||
// Check if any data exists
|
||||
let has_data = rows.iter().any(|(_, _, months)| months.is_some());
|
||||
if !has_data {
|
||||
return rsx! { p { class: "empty", "No planting calendar data." } };
|
||||
}
|
||||
|
||||
rsx! {
|
||||
div { class: "planting-calendar",
|
||||
// Header row with month labels
|
||||
div { class: "cal-row cal-header",
|
||||
div { class: "cal-label" }
|
||||
for label in MONTH_LABELS.iter() {
|
||||
div { class: "cal-cell", "{label}" }
|
||||
}
|
||||
}
|
||||
// Data rows
|
||||
for (name, class, months) in rows.iter() {
|
||||
if months.is_some() {
|
||||
div { class: "cal-row",
|
||||
div { class: "cal-label", "{name}" }
|
||||
for month in 1..=12i32 {
|
||||
{
|
||||
let active = months.as_ref()
|
||||
.map(|m| m.contains(&month))
|
||||
.unwrap_or(false);
|
||||
rsx! {
|
||||
div {
|
||||
class: if active { format!("cal-cell {class} active") } else { "cal-cell".to_string() },
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
mod api;
|
||||
mod app;
|
||||
mod components;
|
||||
mod pages;
|
||||
mod types;
|
||||
|
||||
fn main() {
|
||||
console_error_panic_hook::set_once();
|
||||
dioxus::launch(app::App);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
use crate::components::planting_calendar::PlantingCalendar;
|
||||
|
||||
#[component]
|
||||
pub fn CultivarList() -> Element {
|
||||
let mut page = use_signal(|| 1i64);
|
||||
let mut search = use_signal(|| String::new());
|
||||
let current_page = *page.read();
|
||||
let search_str = search.read().clone();
|
||||
|
||||
let cultivars = use_resource(move || {
|
||||
let s = search_str.clone();
|
||||
async move {
|
||||
let q = if s.is_empty() { None } else { Some(s.as_str()) };
|
||||
api::list_cultivars(current_page, None, q).await
|
||||
}
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
h1 { "Cultivars" }
|
||||
|
||||
div { class: "search-bar",
|
||||
input {
|
||||
r#type: "text",
|
||||
placeholder: "Search cultivars...",
|
||||
value: "{search}",
|
||||
oninput: move |e| {
|
||||
search.set(e.value());
|
||||
page.set(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
match &*cultivars.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "table-wrap",
|
||||
table {
|
||||
thead {
|
||||
tr {
|
||||
th { "Name" }
|
||||
th { "Organic" }
|
||||
th { "Perennial" }
|
||||
th { "Frost Tolerance" }
|
||||
}
|
||||
}
|
||||
tbody {
|
||||
for c in data.data.iter() {
|
||||
tr {
|
||||
td {
|
||||
Link { to: Route::CultivarDetail { slug: c.slug.clone() },
|
||||
strong { "{c.name}" }
|
||||
}
|
||||
}
|
||||
td { if c.is_organic { "Yes" } else { "-" } }
|
||||
td { if c.perennial { "Yes" } else { "Annual" } }
|
||||
td { "{c.frost_tolerance.as_deref().unwrap_or(\"-\")}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if data.total > data.per_page {
|
||||
div { class: "pagination",
|
||||
button {
|
||||
disabled: current_page <= 1,
|
||||
onclick: move |_| page.set(current_page - 1),
|
||||
"Previous"
|
||||
}
|
||||
span { "Page {current_page}" }
|
||||
button {
|
||||
disabled: current_page * data.per_page >= data.total,
|
||||
onclick: move |_| page.set(current_page + 1),
|
||||
"Next"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn CultivarDetail(slug: String) -> Element {
|
||||
let slug_clone = slug.clone();
|
||||
let cultivar = use_resource(move || {
|
||||
let s = slug_clone.clone();
|
||||
async move { api::get_cultivar(&s).await }
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page cultivar-detail",
|
||||
match &*cultivar.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(c)) => rsx! {
|
||||
h1 { "{c.name}" }
|
||||
if let Some(ref en) = c.name_en {
|
||||
p { class: "name-common", "{en}" }
|
||||
}
|
||||
if let Some(ref desc) = c.description {
|
||||
div { class: "description", "{desc}" }
|
||||
}
|
||||
|
||||
div { class: "badges",
|
||||
if c.is_organic {
|
||||
span { class: "badge organic", "Organic" }
|
||||
}
|
||||
if c.perennial {
|
||||
span { class: "badge", "Perennial" }
|
||||
}
|
||||
if let Some(ref ft) = c.frost_tolerance {
|
||||
span { class: "badge", "Frost: {ft}" }
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref dtg) = c.days_to_germination {
|
||||
p { "Days to germination: {dtg}" }
|
||||
}
|
||||
if let Some(ref gtd) = c.growing_time_days {
|
||||
p { "Growing time: {gtd} days" }
|
||||
}
|
||||
|
||||
// Planting calendar
|
||||
h2 { "Planting Calendar" }
|
||||
PlantingCalendar {
|
||||
indoor_sowing: c.indoor_sowing_months.clone(),
|
||||
direct_sowing: c.direct_sowing_months.clone(),
|
||||
transplanting: c.transplanting_months.clone(),
|
||||
glasshouse: c.glasshouse_months.clone(),
|
||||
harvesting: c.harvesting_months.clone(),
|
||||
}
|
||||
|
||||
if let Some(ref pg) = c.pollination_group {
|
||||
p { "Pollination group: {pg}" }
|
||||
}
|
||||
if let Some(sf) = c.self_fertile {
|
||||
if sf {
|
||||
p { "Self-fertile: Yes" }
|
||||
} else {
|
||||
p { "Self-fertile: No" }
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
|
||||
#[component]
|
||||
pub fn FamilyList() -> Element {
|
||||
let mut page = use_signal(|| 1i64);
|
||||
let mut search = use_signal(|| String::new());
|
||||
let current_page = *page.read();
|
||||
let search_str = search.read().clone();
|
||||
|
||||
let families = use_resource(move || {
|
||||
let s = search_str.clone();
|
||||
async move {
|
||||
let q = if s.is_empty() { None } else { Some(s.as_str()) };
|
||||
api::list_families(current_page, q).await
|
||||
}
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
h1 { "Plant Families" }
|
||||
|
||||
div { class: "search-bar",
|
||||
input {
|
||||
r#type: "text",
|
||||
placeholder: "Search families...",
|
||||
value: "{search}",
|
||||
oninput: move |e| {
|
||||
search.set(e.value());
|
||||
page.set(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
match &*families.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "table-wrap",
|
||||
table {
|
||||
thead {
|
||||
tr {
|
||||
th { "Scientific Name" }
|
||||
th { "English" }
|
||||
th { "German" }
|
||||
}
|
||||
}
|
||||
tbody {
|
||||
for f in data.data.iter() {
|
||||
tr {
|
||||
td {
|
||||
Link { to: Route::FamilyDetail { slug: f.slug.clone() },
|
||||
em { "{f.name_scientific}" }
|
||||
}
|
||||
}
|
||||
td { "{f.name_en.as_deref().unwrap_or(\"-\")}" }
|
||||
td { "{f.name_de.as_deref().unwrap_or(\"-\")}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if data.total > data.per_page {
|
||||
div { class: "pagination",
|
||||
button {
|
||||
disabled: current_page <= 1,
|
||||
onclick: move |_| page.set(current_page - 1),
|
||||
"Previous"
|
||||
}
|
||||
span { "Page {current_page} of {(data.total + data.per_page - 1) / data.per_page}" }
|
||||
button {
|
||||
disabled: current_page * data.per_page >= data.total,
|
||||
onclick: move |_| page.set(current_page + 1),
|
||||
"Next"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn FamilyDetail(slug: String) -> Element {
|
||||
let slug_clone = slug.clone();
|
||||
let family = use_resource(move || {
|
||||
let s = slug_clone.clone();
|
||||
async move { api::get_family(&s).await }
|
||||
});
|
||||
|
||||
let slug_for_species = slug.clone();
|
||||
let species = use_resource(move || {
|
||||
let s = slug_for_species.clone();
|
||||
async move { api::list_species(1, Some(&s), None).await }
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
match &*family.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(f)) => rsx! {
|
||||
h1 { em { "{f.name_scientific}" } }
|
||||
if let Some(ref en) = f.name_en {
|
||||
p { class: "name-common", "{en}" }
|
||||
}
|
||||
if let Some(ref de) = f.name_de {
|
||||
p { class: "name-common", "{de}" }
|
||||
}
|
||||
if let Some(ref desc) = f.description {
|
||||
p { "{desc}" }
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
h2 { "Species in this Family" }
|
||||
match &*species.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "card-grid",
|
||||
for s in data.data.iter() {
|
||||
div { class: "plant-card",
|
||||
Link { to: Route::SpeciesDetail { slug: s.slug.clone() },
|
||||
em { "{s.name_scientific}" }
|
||||
}
|
||||
if let Some(ref en) = s.name_en {
|
||||
p { class: "card-common", "{en}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
use crate::components::plant_card::PlantCard;
|
||||
|
||||
#[component]
|
||||
pub fn Home() -> Element {
|
||||
let mut search_query = use_signal(|| String::new());
|
||||
let species = use_resource(|| async { api::list_species(1, None, None).await });
|
||||
|
||||
rsx! {
|
||||
div { class: "page home-page",
|
||||
h1 { "HerbAPI" }
|
||||
p { class: "subtitle", "Trilingual plant reference database" }
|
||||
|
||||
div { class: "search-bar",
|
||||
input {
|
||||
r#type: "text",
|
||||
placeholder: "Search plants...",
|
||||
value: "{search_query}",
|
||||
oninput: move |e| search_query.set(e.value()),
|
||||
onkeydown: move |e| {
|
||||
if e.key() == Key::Enter {
|
||||
let nav = navigator();
|
||||
nav.push(Route::SearchPage {});
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
h2 { "Recent Species" }
|
||||
match &*species.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "card-grid",
|
||||
for s in data.data.iter().take(12) {
|
||||
PlantCard {
|
||||
key: "{s.id}",
|
||||
slug: s.slug.clone(),
|
||||
name: s.name_scientific.clone(),
|
||||
name_common: s.name_en.clone(),
|
||||
entity_type: "species".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod cultivars;
|
||||
pub mod families;
|
||||
pub mod home;
|
||||
pub mod search;
|
||||
pub mod species;
|
||||
pub mod suppliers;
|
||||
@@ -0,0 +1,77 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
|
||||
#[component]
|
||||
pub fn SearchPage() -> Element {
|
||||
let mut query = use_signal(|| String::new());
|
||||
let mut results = use_signal(|| None::<Result<Vec<crate::types::SearchResult>, String>>);
|
||||
|
||||
let trigger_search = move || {
|
||||
let q = query.read().clone();
|
||||
if !q.is_empty() {
|
||||
spawn(async move {
|
||||
let res = api::search(&q, 50).await;
|
||||
results.set(Some(res));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
rsx! {
|
||||
div { class: "page search-page",
|
||||
h1 { "Search" }
|
||||
|
||||
div { class: "search-bar",
|
||||
input {
|
||||
r#type: "text",
|
||||
placeholder: "Search plants, families, cultivars...",
|
||||
value: "{query}",
|
||||
oninput: move |e| query.set(e.value()),
|
||||
onkeydown: move |e| {
|
||||
if e.key() == Key::Enter {
|
||||
trigger_search();
|
||||
}
|
||||
},
|
||||
}
|
||||
button { onclick: move |_| trigger_search(), "Search" }
|
||||
}
|
||||
|
||||
match &*results.read() {
|
||||
None => rsx! {},
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
p { class: "result-count", "{data.len()} results" }
|
||||
div { class: "search-results",
|
||||
for r in data.iter() {
|
||||
div { class: "search-result",
|
||||
span { class: "result-type badge", "{r.entity_type}" }
|
||||
match r.entity_type.as_str() {
|
||||
"family" => rsx! {
|
||||
Link { to: Route::FamilyDetail { slug: r.slug.clone() },
|
||||
em { "{r.name}" }
|
||||
}
|
||||
},
|
||||
"species" => rsx! {
|
||||
Link { to: Route::SpeciesDetail { slug: r.slug.clone() },
|
||||
em { "{r.name}" }
|
||||
}
|
||||
},
|
||||
"cultivar" => rsx! {
|
||||
Link { to: Route::CultivarDetail { slug: r.slug.clone() },
|
||||
strong { "{r.name}" }
|
||||
}
|
||||
},
|
||||
_ => rsx! { span { "{r.name}" } },
|
||||
}
|
||||
if let Some(ref desc) = r.description {
|
||||
p { class: "result-desc", "{desc}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
use crate::components::plant_card::PlantCard;
|
||||
|
||||
#[component]
|
||||
pub fn SpeciesList() -> Element {
|
||||
let mut page = use_signal(|| 1i64);
|
||||
let mut search = use_signal(|| String::new());
|
||||
let current_page = *page.read();
|
||||
let search_str = search.read().clone();
|
||||
|
||||
let species = use_resource(move || {
|
||||
let s = search_str.clone();
|
||||
async move {
|
||||
let q = if s.is_empty() { None } else { Some(s.as_str()) };
|
||||
api::list_species(current_page, None, q).await
|
||||
}
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
h1 { "Species" }
|
||||
|
||||
div { class: "search-bar",
|
||||
input {
|
||||
r#type: "text",
|
||||
placeholder: "Search species...",
|
||||
value: "{search}",
|
||||
oninput: move |e| {
|
||||
search.set(e.value());
|
||||
page.set(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
match &*species.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "card-grid",
|
||||
for s in data.data.iter() {
|
||||
PlantCard {
|
||||
key: "{s.id}",
|
||||
slug: s.slug.clone(),
|
||||
name: s.name_scientific.clone(),
|
||||
name_common: s.name_en.clone(),
|
||||
entity_type: "species".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
if data.total > data.per_page {
|
||||
div { class: "pagination",
|
||||
button {
|
||||
disabled: current_page <= 1,
|
||||
onclick: move |_| page.set(current_page - 1),
|
||||
"Previous"
|
||||
}
|
||||
span { "Page {current_page} of {(data.total + data.per_page - 1) / data.per_page}" }
|
||||
button {
|
||||
disabled: current_page * data.per_page >= data.total,
|
||||
onclick: move |_| page.set(current_page + 1),
|
||||
"Next"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn SpeciesDetail(slug: String) -> Element {
|
||||
let slug_clone = slug.clone();
|
||||
let species = use_resource(move || {
|
||||
let s = slug_clone.clone();
|
||||
async move { api::get_species(&s).await }
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page species-detail",
|
||||
match &*species.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(s)) => {
|
||||
let species_slug = s.slug.clone();
|
||||
rsx! {
|
||||
h1 { em { "{s.name_scientific}" } }
|
||||
if let Some(ref en) = s.name_en {
|
||||
p { class: "name-common", "{en}" }
|
||||
}
|
||||
if let Some(ref de) = s.name_de {
|
||||
p { class: "name-common", "{de}" }
|
||||
}
|
||||
if let Some(ref desc) = s.description {
|
||||
div { class: "description", "{desc}" }
|
||||
}
|
||||
|
||||
// Info grid
|
||||
div { class: "info-grid",
|
||||
if let Some(ref layer) = s.plant_layer {
|
||||
div { class: "info-item",
|
||||
span { class: "info-label", "Layer" }
|
||||
span { class: "info-value", "{layer}" }
|
||||
}
|
||||
}
|
||||
if let Some(ref dt) = s.drought_tolerance {
|
||||
div { class: "info-item",
|
||||
span { class: "info-label", "Drought Tolerance" }
|
||||
span { class: "info-value", "{dt}" }
|
||||
}
|
||||
}
|
||||
if let Some(ref hz) = s.hardiness_zone_usda {
|
||||
div { class: "info-item",
|
||||
span { class: "info-label", "USDA Zone" }
|
||||
span { class: "info-value", "{hz}" }
|
||||
}
|
||||
}
|
||||
if let Some(rating) = s.edibility_rating {
|
||||
div { class: "info-item",
|
||||
span { class: "info-label", "Edibility" }
|
||||
span { class: "info-value", "{rating}/5" }
|
||||
}
|
||||
}
|
||||
if let Some(nf) = s.nitrogen_fixer {
|
||||
if nf {
|
||||
div { class: "info-item badge",
|
||||
"Nitrogen Fixer"
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(da) = s.dynamic_accumulator {
|
||||
if da {
|
||||
div { class: "info-item badge",
|
||||
"Dynamic Accumulator"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cultivars for this species
|
||||
h2 { "Cultivars" }
|
||||
CultivarListForSpecies { species_slug: species_slug }
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn CultivarListForSpecies(species_slug: String) -> Element {
|
||||
let slug = species_slug.clone();
|
||||
let cultivars = use_resource(move || {
|
||||
let s = slug.clone();
|
||||
async move { api::list_cultivars(1, Some(&s), None).await }
|
||||
});
|
||||
|
||||
rsx! {
|
||||
match &*cultivars.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => {
|
||||
if data.data.is_empty() {
|
||||
rsx! { p { class: "empty", "No cultivars yet." } }
|
||||
} else {
|
||||
rsx! {
|
||||
div { class: "card-grid",
|
||||
for c in data.data.iter() {
|
||||
div { class: "plant-card",
|
||||
Link { to: Route::CultivarDetail { slug: c.slug.clone() },
|
||||
strong { "{c.name}" }
|
||||
}
|
||||
if let Some(ref en) = c.name_en {
|
||||
p { class: "card-common", "{en}" }
|
||||
}
|
||||
if c.is_organic {
|
||||
span { class: "badge organic", "Organic" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::api;
|
||||
use crate::app::Route;
|
||||
|
||||
#[component]
|
||||
pub fn SupplierList() -> Element {
|
||||
let suppliers = use_resource(|| async { api::list_suppliers().await });
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
h1 { "Suppliers" }
|
||||
|
||||
match &*suppliers.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(data)) => rsx! {
|
||||
div { class: "table-wrap",
|
||||
table {
|
||||
thead {
|
||||
tr {
|
||||
th { "Name" }
|
||||
th { "Country" }
|
||||
th { "Organic" }
|
||||
th { "Demeter" }
|
||||
}
|
||||
}
|
||||
tbody {
|
||||
for s in data.iter() {
|
||||
tr {
|
||||
td {
|
||||
Link { to: Route::SupplierDetail { slug: s.slug.clone() },
|
||||
strong { "{s.name}" }
|
||||
}
|
||||
}
|
||||
td { "{s.country.as_deref().unwrap_or(\"-\")}" }
|
||||
td { if s.is_organic { "Yes" } else { "-" } }
|
||||
td { if s.is_demeter { "Yes" } else { "-" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn SupplierDetail(slug: String) -> Element {
|
||||
let slug_clone = slug.clone();
|
||||
let supplier = use_resource(move || {
|
||||
let s = slug_clone.clone();
|
||||
async move { api::get_supplier(&s).await }
|
||||
});
|
||||
|
||||
rsx! {
|
||||
div { class: "page",
|
||||
match &*supplier.read() {
|
||||
None => rsx! { p { "Loading..." } },
|
||||
Some(Err(e)) => rsx! { p { class: "error", "Error: {e}" } },
|
||||
Some(Ok(s)) => rsx! {
|
||||
h1 { "{s.name}" }
|
||||
if let Some(ref url) = s.url {
|
||||
p { a { href: "{url}", target: "_blank", "{url}" } }
|
||||
}
|
||||
div { class: "badges",
|
||||
if s.is_organic {
|
||||
span { class: "badge organic", "Organic" }
|
||||
}
|
||||
if s.is_demeter {
|
||||
span { class: "badge demeter", "Demeter" }
|
||||
}
|
||||
if let Some(ref country) = s.country {
|
||||
span { class: "badge", "{country}" }
|
||||
}
|
||||
}
|
||||
if let Some(ref notes) = s.notes {
|
||||
p { "{notes}" }
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct PaginatedResponse<T> {
|
||||
pub data: Vec<T>,
|
||||
pub total: i64,
|
||||
pub page: i64,
|
||||
pub per_page: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Family {
|
||||
pub id: Uuid,
|
||||
pub slug: String,
|
||||
pub name_scientific: String,
|
||||
pub name_en: Option<String>,
|
||||
pub name_de: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Species {
|
||||
pub id: Uuid,
|
||||
pub slug: String,
|
||||
pub family_id: Uuid,
|
||||
pub name_scientific: String,
|
||||
pub name_en: Option<String>,
|
||||
pub name_de: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub soil_moisture: Option<String>,
|
||||
pub ph_min: Option<f64>,
|
||||
pub ph_max: Option<f64>,
|
||||
pub hardiness_zone_usda: Option<String>,
|
||||
pub hardiness_zone_at: Option<String>,
|
||||
pub drought_tolerance: Option<String>,
|
||||
pub edibility_rating: Option<i16>,
|
||||
pub food_uses: Option<String>,
|
||||
pub medicinal_uses: Option<String>,
|
||||
pub other_uses: Option<String>,
|
||||
pub native_range: Option<String>,
|
||||
pub plant_layer: Option<String>,
|
||||
pub nitrogen_fixer: Option<bool>,
|
||||
pub dynamic_accumulator: Option<bool>,
|
||||
pub wikidata_qid: Option<String>,
|
||||
pub primary_image_key: Option<String>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Cultivar {
|
||||
pub id: Uuid,
|
||||
pub slug: String,
|
||||
pub species_id: Uuid,
|
||||
pub name: String,
|
||||
pub name_en: Option<String>,
|
||||
pub name_de: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub is_organic: bool,
|
||||
pub perennial: bool,
|
||||
pub growing_time_days: Option<i32>,
|
||||
pub days_to_germination: Option<i32>,
|
||||
pub frost_tolerance: Option<String>,
|
||||
pub indoor_sowing_months: Option<Vec<i32>>,
|
||||
pub direct_sowing_months: Option<Vec<i32>>,
|
||||
pub transplanting_months: Option<Vec<i32>>,
|
||||
pub glasshouse_months: Option<Vec<i32>>,
|
||||
pub harvesting_months: Option<Vec<i32>>,
|
||||
pub pollination_group: Option<String>,
|
||||
pub self_fertile: Option<bool>,
|
||||
pub primary_image_key: Option<String>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Supplier {
|
||||
pub id: Uuid,
|
||||
pub slug: String,
|
||||
pub name: String,
|
||||
pub url: Option<String>,
|
||||
pub is_organic: bool,
|
||||
pub is_demeter: bool,
|
||||
pub country: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct CultivarSupplier {
|
||||
pub id: Uuid,
|
||||
pub cultivar_id: Uuid,
|
||||
pub supplier_id: Uuid,
|
||||
pub article_number: Option<String>,
|
||||
pub product_url: Option<String>,
|
||||
pub price_eur: Option<f64>,
|
||||
pub pack_size: Option<f64>,
|
||||
pub pack_unit: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct CompanionRelationship {
|
||||
pub id: Uuid,
|
||||
pub species_a_id: Uuid,
|
||||
pub species_b_id: Uuid,
|
||||
pub relationship: String,
|
||||
pub mechanism: Option<String>,
|
||||
pub source_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Image {
|
||||
pub id: Uuid,
|
||||
pub entity_type: String,
|
||||
pub entity_id: Uuid,
|
||||
pub s3_key: String,
|
||||
pub caption: Option<String>,
|
||||
pub is_primary: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct SearchResult {
|
||||
pub entity_type: String,
|
||||
pub id: Uuid,
|
||||
pub slug: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub rank: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct MeResponse {
|
||||
pub id: Uuid,
|
||||
pub email: String,
|
||||
pub name: Option<String>,
|
||||
pub nickname: Option<String>,
|
||||
pub admin: bool,
|
||||
}
|
||||
Reference in New Issue
Block a user