namespace zero.Api.Abstractions; public class PickerProvider : IPickerProvider where T : ZeroIdEntity, new() { protected IStoreOperations Operations { get; set; } public PickerProvider(IStoreOperations operations) { Operations = operations; } /// public virtual async Task> GetPreviews(IEnumerable ids) { Dictionary result = await Operations.Load(ids); return result.Select(x => { if (x.Value == null) { return GenerateNotFoundPreview(x.Key); } return ConvertToPreview(x.Value); }).ToList(); } /// public virtual async Task> PickFrom(int pageNumber, int pageSize = 50, ListQuery query = default) { Paged result = await Operations.Load(pageNumber, pageSize, q => q.Filter(query)); return result.MapTo(ConvertToModel); } /// /// Converts a source item to picker model which is output in the list picker /// protected virtual PickerModel ConvertToModel(T source) { PickerModel model = new() { Id = source.Id }; if (source is ZeroEntity) { model.Name = (source as ZeroEntity).Name; model.IsActive = (source as ZeroEntity).IsActive; } return model; } /// /// Converts a source item to picker model which is output in the list picker. /// Renders an error when the source is null. /// protected virtual PickerPreviewModel ConvertToPreview(T source) { PickerPreviewModel model = new() { Id = source.Id, Icon = "fth-box" }; if (source is ZeroEntity) { model.Name = (source as ZeroEntity).Name; } else { model.Name = "[object #" + source.Id + "]"; } return model; } /// /// Generates a preview which displays a not-found error /// protected virtual PickerPreviewModel GenerateNotFoundPreview(string id) { return new() { //HasError = true, Icon = "fth-alert-circle color-red", Id = id, Name = "@errors.preview.notfound", Text = "@errors.preview.notfound_text" }; } } public interface IPickerProvider where T : ZeroIdEntity, new() { /// /// Get previews which are displayed in the editor field /// Task> GetPreviews(IEnumerable ids); /// /// Items to choose from when the picker is open. /// The picker supports paging as well as filters. /// Task> PickFrom(int pageNumber, int pageSize = 50, ListQuery query = null); }