using Microsoft.AspNetCore.Mvc.Rendering; namespace Nop.Services.ExportImport.Help; /// /// A helper class to access the property by name /// /// Object type /// Language public partial class PropertyByName { protected object _propertyValue; /// /// Ctor /// /// Property name /// Feature property access /// Specifies whether the property should be exported /// A task that represents the asynchronous operation public PropertyByName(string propertyName, Func> func, bool ignore = false) { PropertyName = propertyName; GetProperty = func; PropertyOrderPosition = 1; Ignore = ignore; } /// /// Ctor /// /// Property name /// Feature property access /// Specifies whether the property should be exported public PropertyByName(string propertyName, Func func = null, bool ignore = false) { PropertyName = propertyName; if (func != null) GetProperty = (obj, lang) => Task.FromResult(func(obj, lang)); PropertyOrderPosition = 1; Ignore = ignore; } /// /// Property order position /// public int PropertyOrderPosition { get; set; } /// /// Feature property access /// /// A task that represents the asynchronous operation public Func> GetProperty { get; } /// /// Property name /// public string PropertyName { get; } /// /// Property value /// public object PropertyValue { get => IsDropDownCell ? GetItemId(_propertyValue) : _propertyValue; set => _propertyValue = value; } /// /// Converted property value to Int32 /// public int IntValue { get { if (PropertyValue == null || !int.TryParse(PropertyValue.ToString(), out var rez)) return default; return rez; } } /// /// Converted property value to Int32 /// public int? IntValueNullable { get { if (PropertyValue == null || !int.TryParse(PropertyValue.ToString(), out var rez)) return null; return rez; } } /// /// Converted property value to boolean /// public bool BooleanValue { get { if (PropertyValue == null || !bool.TryParse(PropertyValue.ToString(), out var rez)) return default; return rez; } } /// /// Converted property value to string /// public string StringValue => PropertyValue == null ? string.Empty : Convert.ToString(PropertyValue); /// /// Converted property value to decimal /// public decimal DecimalValue { get { if (PropertyValue == null || !decimal.TryParse(PropertyValue.ToString(), out var rez)) return default; return rez; } } /// /// Converted property value to decimal? /// public decimal? DecimalValueNullable { get { if (PropertyValue == null || !decimal.TryParse(PropertyValue.ToString(), out var rez)) return null; return rez; } } /// /// Converted property value to double /// public double DoubleValue { get { if (PropertyValue == null || !double.TryParse(PropertyValue.ToString(), out var rez)) return default; return rez; } } /// /// Converted property value to DateTime? /// public DateTime? DateTimeNullable => string.IsNullOrWhiteSpace(StringValue) ? null : PropertyValue as DateTime?; /// /// Converted property value to guid /// public Guid GuidValue { get { if (PropertyValue == null || !Guid.TryParse(PropertyValue.ToString(), out var rez)) return default; return rez; } } /// /// To string /// /// String public override string ToString() { return PropertyName; } /// /// Specifies whether the property should be exported /// public bool Ignore { get; set; } /// /// Is drop down cell /// public bool IsDropDownCell => DropDownElements != null; /// /// Get DropDown elements /// /// Result public string[] GetDropDownElements() { return IsDropDownCell ? DropDownElements.Select(ev => ev.Text).ToArray() : Array.Empty(); } /// /// Get item text /// /// Identifier /// Text public string GetItemText(object id) { return DropDownElements.FirstOrDefault(ev => ev.Value == id.ToString())?.Text ?? string.Empty; } /// /// Get item identifier /// /// Name /// Identifier public int GetItemId(object name) { if (string.IsNullOrEmpty(name?.ToString())) return 0; if (!int.TryParse(name.ToString(), out var id)) id = 0; return Convert.ToInt32(DropDownElements.FirstOrDefault(ev => ev.Text.Trim() == name.ToString().Trim())?.Value ?? id.ToString()); } /// /// Elements for a drop-down cell /// public SelectList DropDownElements { get; set; } /// /// Indicates whether the cell can contain an empty value. Makes sense only for a drop-down cells /// public bool AllowBlank { get; set; } /// /// Is caption /// public bool IsCaption => PropertyName == StringValue || PropertyName == _propertyValue.ToString(); }