Skip to content

Product

// RB2 Core Connect
using System.Text;
using System.Text.Json;
using CoreConnect.Commerce.Core;
using HotChocolate;
using HotChocolate.Types;
using Attribute = CoreConnect.Commerce.Core.Attribute;
namespace CoreConnect.Commerce.Catalog;
public class Product : Node
{
/// <summary>
/// The ID of the Product.
/// </summary>
public required string Id { get; set; }
/// <summary>
/// The Key of the Product.
/// </summary>
public string? Key { get; set; }
/// <summary>
/// The SKU of the Product.
/// </summary>
public required string Sku { get; set; }
/// <summary>
/// The Name of the Product.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// The Slug of the Product.
/// </summary>
public required string Slug { get; set; }
/// <summary>
/// The Description of the Product.
/// </summary>
public required string Description { get; set; }
/// <summary>
/// The Product version/revision.
/// </summary>
public long? Version { get; set; }
/// <summary>
/// The Price Include Tax of the Product.
/// </summary>
public Money? PriceInclTax { get; set; }
/// <summary>
/// The Price Exclude Tax of the Product.
/// </summary>
public Money? PriceExclTax { get; set; }
/// <summary>
/// The Sale Price Include Tax (Discounted Price Include Tax) of the Product.
/// </summary>
public Money? SalePriceInclTax { get; set; }
/// <summary>
/// The Sale Price Exclude Tax (Discounted Price Exclude Tax) of the Product.
/// </summary>
public Money? SalePriceExclTax { get; set; }
/// <summary>
/// The Default Image of the Product.
/// </summary>
public Image? DefaultImage { get; set; }
/// <summary>
/// The Images of the Product.
/// </summary>
public required IEnumerable<Image> Images { get; set; }
/// <summary>
/// Gets or sets the categories.
/// </summary>
public required IEnumerable<Category> Categories { get; set; }
/// <summary>
/// The Attributes of the Product.
/// </summary>
public required IEnumerable<Attribute> Attributes { get; set; }
/// <summary>
/// The Specifications of the Product.
/// </summary>
public required IEnumerable<Attribute>? Specifications { get; set; }
/// <summary>
/// The Variants of the Product.
/// </summary>
public required IEnumerable<ProductVariant> Variants { get; set; }
public IEnumerable<ProductVariant>? AllVariants { get; set; }
/// <summary>
/// The SEO of the Product.
/// </summary>
public required Seo Seo { get; set; }
public ProductsResponse? ProductsResponse { get; set; }
/// <summary>
/// Gets or sets the created at.
/// </summary>
public DateTime CreatedAt { get; set; }
/// <summary>
/// Gets or sets the last modified at.
/// </summary>
public DateTime LastModifiedAt { get; set; }
/// <summary>
/// Gets or sets the first live at.
/// </summary>
public DateTime? FirstLiveAt { get; set; }
/// <summary>
/// Gets or sets the breadcrumbs.
/// </summary>
/// <value>
/// The breadcrumbs.
/// </value>
public required IEnumerable<Breadcrumbs> Breadcrumbs { get; set; }
/// <summary>
/// List of related products
/// </summary>
public required List<Product> RelatedProducts { get; set; }
/// <summary>
/// The localized slugs
/// </summary>
[GraphQLType(typeof(AnyType))]
public Dictionary<string, object>? LocalizedSlugs { get; set; }
/// <summary>
/// Price Custom fields. Should be manipulated by the consumer to display the right values. Object is of type CentPrecisionMoney
/// </summary>
[GraphQLType(typeof(AnyType))]
public Dictionary<string, object>? CustomFields { get; set; }
/// <summary>
/// Discounted prices for a product based on quantity tiers
/// </summary>
public IEnumerable<TierPrices>? TierPrices { get; set; }
/// <summary>
/// Whether the product is published or not
/// </summary>
public bool? IsPublished { get; set; }
/// <summary>
/// Product discounts
/// </summary>
public List<string>? Discounts { get; set; }
/// <summary>
/// Product product type information.
/// </summary>
public ProductTypeInfo? ProductTypeInfo { get; set; }
}
/// <summary>
/// Breadcrumbs class
/// </summary>
public class Breadcrumbs
{
/// <summary>
/// Gets or sets the slug.
/// </summary>
/// <value>
/// The slug.
/// </value>
public required string Slug { get; set; }
/// <summary>
/// Gets or sets the key.
/// </summary>
/// <value>
/// The key.
/// </value>
public string? Key { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public required string Name { get; set; }
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>
/// The path.
/// </value>
public string? Path { get; set; }
public int depth { get; set; }
}
[ExtendObjectType<Product>]
public class ProductExtension
{
private const string _variantTotals = "variantTotals";
public ProductMetadata GetMetadata([Parent] Product product)
{
return SetMetadata(product);
}
private static ProductMetadata SetMetadata(Product product)
{
var attributes = product.Variants
.SelectMany(v => v.Attributes)
.GroupBy(a => a.Name)
.ToDictionary(
g => g.Key,
g => g.Select(a => AttributeValueHelper.GetAttributeValue(a.Value ?? new AttributeValue())).Distinct().ToList()
);
var jsonAttributes = JsonSerializer.Serialize(attributes);
var jsonBuilder = new StringBuilder();
jsonBuilder.Append(jsonAttributes);
return new ProductMetadata(_variantTotals, JsonDocument.Parse(jsonBuilder.ToString()).RootElement);
}
}
public class ProductMetadata
{
public ProductMetadata(string key, JsonElement value)
{
Key = key;
Value = value;
}
public string Key { get; set; }
public JsonElement Value { get; set; }
}
public class ProductsResponse
{
public ProductsResponse(List<Product> products, ProductsResponseTotals productsResponseTotals)
{
Products = products;
ProductsResponseTotals = productsResponseTotals;
}
public List<Product> Products { get; set; }
public ProductsResponseTotals ProductsResponseTotals { get; set; }
}
public class ProductsResponseTotals
{
public ProductsResponseTotals(long limit, long offset, long? total)
{
Limit = limit;
Offset = offset;
Total = total;
}
/// <summary>
/// The maximum number of records to retrieve.
/// </summary>
public long Limit { get; set; }
/// <summary>
/// By specifying offset, you retrieve a subset of records starting with the offset.
/// </summary>
public long Offset { get; set; }
public long? Total { get; set; }
}
public class ProductTypeInfo
{
public string? Name { get; set; }
public string? ProductTypeId { get; set; }
}