now products have different options, like different parameters
Some checks failed
CI / Check Style (push) Has been cancelled
CI / Run Clippy (push) Has been cancelled
CI / Run Tests (push) Has been cancelled

This commit is contained in:
Priec
2026-06-22 15:44:02 +02:00
parent 29854a972b
commit 3f798432a0
52 changed files with 1281 additions and 628 deletions

View File

@@ -2,23 +2,28 @@
use serde_json::{json, Value};
use crate::models::_entities::{categories, products};
use crate::models::_entities::{categories, product_variants, products};
use crate::shared::money::format_price;
use crate::shared::pricing::PricedProduct;
/// Card/list shape for a product: model fields plus the viewer's resolved price
/// (from [`crate::shared::pricing`]), its optional primary image and category.
/// `on_sale` means "render the price as reduced" — driven by the resolved price,
/// so it covers both public sales and business deals; `is_business` flags the
/// latter.
/// for its representative (first) variant, the variant count (so the template can
/// render "from {price}" for multi-variant products), its optional primary image
/// and category. `on_sale` means "render the price as reduced" — driven by the
/// resolved price, so it covers both public sales and business deals;
/// `is_business` flags the latter. `representative` is the variant whose price is
/// shown and whose stock/sku the card exposes.
pub fn product_card(
product: &products::Model,
representative: &product_variants::Model,
priced: &PricedProduct,
variant_count: usize,
image: Option<String>,
category_name: Option<String>,
) -> Value {
json!({
"id": product.id,
"variant_id": representative.id,
"name": product.name,
"slug": product.slug,
"description": product.description,
@@ -27,26 +32,41 @@ pub fn product_card(
"is_business": priced.is_business,
"regular_price": format_price(priced.regular_cents),
"currency": product.currency,
"sku": product.sku,
"stock": product.stock,
"sku": representative.sku,
"stock": representative.stock,
"variant_count": variant_count,
"has_options": variant_count > 1,
"published": product.published,
"image": image,
"category_name": category_name,
})
}
/// One priced variant row for the product detail page's option picker.
pub fn variant_option(variant: &product_variants::Model, priced: &PricedProduct) -> Value {
json!({
"id": variant.id,
"label": variant.label,
"sku": variant.sku,
"stock": variant.stock,
"in_stock": variant.stock > 0,
"price": format_price(priced.price_cents),
"on_sale": priced.is_reduced(),
"regular_price": format_price(priced.regular_cents),
"is_business": priced.is_business,
})
}
/// Shape used to pre-fill the admin product form (exposes `category_id` rather
/// than a resolved name, and the current primary image).
/// than a resolved name, and the current primary image). Variants are supplied
/// separately by the controller.
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,