|
- using System;
- using System.Collections.Generic;
-
- namespace CIG
- {
- public class StoreProduct : IComparable<StoreProduct>
- {
- public StoreProduct(Dictionary<string, object> props)
- {
- this.Update(props);
- }
-
- public StoreProduct(StoreProduct storeProduct) : this(storeProduct.Serialize())
- {
- }
-
- public virtual void Update(Dictionary<string, object> props)
- {
- this.Identifier = (string)props["ProductIdentifier"];
- this.Title = this.GetValue<string>(props, "ProductTitle", string.Empty);
- this.Description = this.GetValue<string>(props, "ProductDescription", string.Empty);
- this.CurrencyCode = this.GetValue<string>(props, "ProductCurrencyCode", string.Empty);
- this.FormattedPrice = this.GetValue<string>(props, "ProductFormattedPrice", string.Empty);
- this.Price = this.GetValue<decimal>(props, "ProductPrice", 0m);
- this.DollarPrice = this.GetValue<decimal>(props, "ProductDollarPrice", 0m);
- this.EuroPrice = this.GetValue<decimal>(props, "ProductEuroPrice", 0m);
- this.Available = this.GetValue<bool>(props, "Available", false);
- if (this.Price == 0m)
- {
- if (this.DollarPrice != 0m)
- {
- this.Price = this.DollarPrice;
- }
- else if (this.EuroPrice != 0m)
- {
- this.Price = this.EuroPrice;
- }
- }
- object obj;
- if (props.TryGetValue("CustomProperties", out obj))
- {
- Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
- if (dictionary != null)
- {
- this.CustomProperties = new Dictionary<string, string>();
- foreach (KeyValuePair<string, object> keyValuePair in dictionary)
- {
- this.CustomProperties.Add(keyValuePair.Key, (string)keyValuePair.Value);
- }
- }
- else
- {
- this.CustomProperties = (Dictionary<string, string>)obj;
- }
- }
- else
- {
- this.CustomProperties = new Dictionary<string, string>();
- }
- }
-
- public int CompareTo(StoreProduct other)
- {
- int num = this.Price.CompareTo(other.Price);
- if (num == 0)
- {
- num = this.EuroPrice.CompareTo(other.EuroPrice);
- if (num == 0)
- {
- num = this.DollarPrice.CompareTo(other.DollarPrice);
- }
- }
- return num;
- }
-
- public override string ToString()
- {
- return string.Format("[StoreProduct: {0}; Available: {1}]", this.Identifier, this.Available);
- }
-
- public bool Available { get; set; }
-
- public string Identifier { get; private set; }
-
- public int StorePosition { get; set; }
-
- public string Title { get; private set; }
-
- public string Description { get; private set; }
-
- public decimal Price { get; private set; }
-
- public string CurrencyCode { get; private set; }
-
- public string FormattedPrice { get; private set; }
-
- public decimal DollarPrice { get; private set; }
-
- public decimal EuroPrice { get; private set; }
-
- public Dictionary<string, string> CustomProperties { get; private set; }
-
- private T GetValue<T>(Dictionary<string, object> dict, string key, T defaultValue)
- {
- object obj;
- if (dict.TryGetValue(key, out obj) && obj != null)
- {
- return (T)((object)obj);
- }
- return defaultValue;
- }
-
- public Dictionary<string, object> Serialize()
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- dictionary["ProductIdentifier"] = this.Identifier;
- dictionary["ProductTitle"] = this.Title;
- dictionary["ProductDescription"] = this.Description;
- dictionary["ProductPrice"] = this.Price;
- dictionary["ProductCurrencyCode"] = this.CurrencyCode;
- dictionary["ProductFormattedPrice"] = this.FormattedPrice;
- dictionary["ProductEuroPrice"] = this.EuroPrice;
- dictionary["ProductDollarPrice"] = this.DollarPrice;
- Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
- foreach (KeyValuePair<string, string> keyValuePair in this.CustomProperties)
- {
- dictionary2.Add(keyValuePair.Key, keyValuePair.Value);
- }
- dictionary["CustomProperties"] = dictionary2;
- dictionary["Available"] = this.Available;
- return dictionary;
- }
-
- public const string IdentifierKey = "ProductIdentifier";
-
- public const string TitleKey = "ProductTitle";
-
- public const string DescriptionKey = "ProductDescription";
-
- public const string PriceKey = "ProductPrice";
-
- public const string CurrencyCodeKey = "ProductCurrencyCode";
-
- public const string FormattedPriceKey = "ProductFormattedPrice";
-
- public const string EuroPriceKey = "ProductEuroPrice";
-
- public const string DollarPriceKey = "ProductDollarPrice";
-
- public const string CustomPropertiesKey = "CustomProperties";
-
- public const string AvailableKey = "Available";
- }
- }
|