Advertisement
HenX

Untitled

Jan 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1.    public virtual void SaveAttribute<TPropType>(BaseEntity entity, string key, TPropType value, int storeId = 0)
  2.         {
  3.             if (entity == null)
  4.                 throw new ArgumentNullException("entity");
  5.  
  6.             if (key == null)
  7.                 throw new ArgumentNullException("key");
  8.  
  9.             string keyGroup = entity.GetUnproxiedEntityType().Name;
  10.  
  11.             var props = GetAttributesForEntity(entity.Id, keyGroup)
  12.                 .Where(x => x.StoreId == storeId)
  13.                 .ToList();
  14.             var prop = props.FirstOrDefault(ga =>
  15.                 ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant
  16.  
  17.             var valueStr = CommonHelper.To<string>(value);
  18.  
  19.             if (prop != null)
  20.             {
  21.                 if (string.IsNullOrWhiteSpace(valueStr))
  22.                 {
  23.                     //delete
  24.                     DeleteAttribute(prop);
  25.                 }
  26.                 else
  27.                 {
  28.                     //update
  29.                     prop.Value = valueStr;
  30.                     UpdateAttribute(prop);
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 if (!string.IsNullOrWhiteSpace(valueStr))
  36.                 {
  37.                     //insert
  38.                     prop = new GenericAttribute
  39.                     {
  40.                         EntityId = entity.Id,
  41.                         Key = key,
  42.                         KeyGroup = keyGroup,
  43.                         Value = valueStr,
  44.                         StoreId = storeId,
  45.                        
  46.                     };
  47.                     InsertAttribute(prop);
  48.                 }
  49.             }
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement