loco straucture
This commit is contained in:
56
src/views/shop.rs
Normal file
56
src/views/shop.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
//! JSON shaping for storefront and catalog-admin templates.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::models::_entities::{categories, products};
|
||||
use crate::shared::money::format_price;
|
||||
|
||||
/// Card/list shape for a product: model fields plus a formatted price, its
|
||||
/// optional primary image filename and category name.
|
||||
pub fn product_card(
|
||||
product: &products::Model,
|
||||
image: Option<String>,
|
||||
category_name: Option<String>,
|
||||
) -> Value {
|
||||
json!({
|
||||
"id": product.id,
|
||||
"name": product.name,
|
||||
"slug": product.slug,
|
||||
"description": product.description,
|
||||
"price": format_price(product.price_cents),
|
||||
"currency": product.currency,
|
||||
"sku": product.sku,
|
||||
"stock": product.stock,
|
||||
"published": product.published,
|
||||
"image": image,
|
||||
"category_name": category_name,
|
||||
})
|
||||
}
|
||||
|
||||
/// Shape used to pre-fill the admin product form (exposes `category_id` rather
|
||||
/// than a resolved name, and the current primary image).
|
||||
pub fn product_form(product: &products::Model, image: Option<String>) -> Value {
|
||||
json!({
|
||||
"id": product.id,
|
||||
"name": product.name,
|
||||
"slug": product.slug,
|
||||
"description": product.description,
|
||||
"price": format_price(product.price_cents),
|
||||
"currency": product.currency,
|
||||
"sku": product.sku,
|
||||
"stock": product.stock,
|
||||
"published": product.published,
|
||||
"category_id": product.category_id,
|
||||
"image": image,
|
||||
})
|
||||
}
|
||||
|
||||
/// Depth-ordered `{ name, slug, depth }` rows for the storefront sidebar,
|
||||
/// rendered as an indented flat list.
|
||||
pub fn sidebar_rows(tree: &[(categories::Model, usize)]) -> Vec<Value> {
|
||||
tree.iter()
|
||||
.map(|(category, depth)| {
|
||||
json!({ "name": category.name, "slug": category.slug, "depth": depth })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user