public void Set(FieldContainer entity, IFieldDefinition field, File file) { var options = field.Option as Litium.FieldFramework.FieldTypes.PointerOption; var pointerType = GetTypeFor(options.EntityType); if (!pointerType.IsInstanceOfType(file)) { throw new Exception($"An object of type {file.GetType()} can not be set to a pointer ({field.Id}) of type {options.EntityType}"); } if (options.MultiSelect) { var files = entity.GetValue <List <PointerItem> >(field.Id)? .Select(r => _mediaArchive.GetFile(r.EntitySystemId)) .Where(r => r != null) .ToList() ?? new List <File>(); files.RemoveAll(r => string.Equals(r.Name, file.Name, StringComparison.OrdinalIgnoreCase)); files.Add(_mediaArchive.GetFile(file.SystemId)); Sort?.Invoke(entity, field, files); entity.AddOrUpdateValue(field.Id, files.Select(r => new PointerItem() { EntitySystemId = r.SystemId }).ToList()); } else { entity.AddOrUpdateValue(field.Id, new PointerItem() { EntitySystemId = file.SystemId }); } }
public void Set(FieldContainer entity, IFieldDefinition field, File file) { var images = entity.GetValue <List <Guid> >(field.Id)? .Select(r => _mediaArchive.GetFile(r)) .Where(r => r != null) .ToList() ?? new List <File>(); if (images.Any(r => r.SystemId == file.SystemId)) { // Field already contain this file. Don't do anything. return; } images.RemoveAll(r => string.Equals(r.Name, file.Name, StringComparison.OrdinalIgnoreCase)); images.Add(_mediaArchive.GetFile(file.SystemId)); Sort?.Invoke(entity, field, images); entity.AddOrUpdateValue(field.Id, images.Select(r => r.SystemId).ToList()); }