Exemple #1
0
        public virtual IEnumerable <BaseEntityViewModel> ToViewModels(IEnumerable <TEntity> entities, string[] moreFields = null)
        {
            foreach (var entity in entities)
            {
                var details   = EntityHelper.GetDetails(entity);
                var viewModel = new BaseEntityViewModel()
                {
                    Title = details.FirstOrDefault(o => o.Field == "title")?.Value,
                };
                viewModel.SetId(entity.Id);
                viewModel.SetName(entity.Name);
                var thumnailUrl = details.FirstOrDefault(o => o.Field == "thumbnail" && o.Suffix == AppKey.FileUrlPropertyName)?.Value;


                if (thumnailUrl != null && !thumnailUrl.StartsWith("/") && (!thumnailUrl.StartsWith("https://") || !thumnailUrl.StartsWith("http://")))
                {
                    thumnailUrl = "/" + thumnailUrl;
                }

                viewModel.SetThumbnail(thumnailUrl);

                if (moreFields != null)
                {
                    foreach (var field in moreFields)
                    {
                        var objectDetailValue = details.FirstOrDefault(o => o.Field == field)?.Value;
                        if (objectDetailValue != null)
                        {
                            viewModel.SetMoreDetail(field, details.FirstOrDefault(o => o.Field == field).Value);
                        }
                    }
                }

                yield return(viewModel);
            }
        }