Пример #1
0
        public async Task <(IReadOnlyList <CmsItem> results, int total)> List(CmsType cmsType, string?sortField, string?sortOrder, int pageSize = 20, int pageIndex = 0, string?searchQuery = null)
        {
            List <CmsItem>?indexFile = await GetIndexFile(cmsType).ConfigureAwait(false);

            var returnItems = indexFile.AsQueryable();

            if (!string.IsNullOrEmpty(searchQuery))
            {
                returnItems = returnItems
                              .Where(x => string.Join(" ", x.AdditionalProperties.Values.Select(x => x.ToString())
                                                      .Where(x => x != null)
                                                      .Select(x => x !.ToLowerInvariant())
                                                      ).Contains(searchQuery, StringComparison.InvariantCultureIgnoreCase));
            }

            if (sortField != null)
            {
                sortOrder = sortOrder ?? "Asc";
                if (sortOrder == "Asc")
                {
                    returnItems = returnItems.OrderBy(x => x.AdditionalProperties[sortField].ToString());
                }
                else
                {
                    returnItems = returnItems.OrderByDescending(x => x.AdditionalProperties[sortField].ToString());
                }
            }
            else
            {
                //Default sort by CreatedDate
                returnItems = returnItems.OrderBy(x => x.CreatedDate);
            }

            return(returnItems.Skip(pageSize * pageIndex).Take(pageSize).ToList(), indexFile.Count);
        }
Пример #2
0
        public async Task <(IReadOnlyList <CmsItem> results, int total)> List(CmsType cmsType, string?sortField, string?sortOrder, int pageSize = 20, int pageIndex = 0, string?searchQuery = null)
        {
            IQueryable <Model> returnItems = dbContext.Set <Model>();

            if (sortField != null)
            {
                sortOrder = sortOrder ?? "ASC";
                if (sortOrder == "Asc")
                {
                    returnItems = returnItems.OrderBy(sortField);
                }
                else
                {
                    returnItems = returnItems.OrderBy(sortField + " DESC");
                }
            }

            var count = await returnItems.CountAsync().ConfigureAwait(false);

            returnItems = returnItems.Skip(pageIndex).Take(pageSize);

            var result = await returnItems.ToListAsync().ConfigureAwait(false);

            return(result.Select(x => {
                var item = x.ToCmsItem();
                if (item != null)
                {
                    item.CmsType = cmsType;
                }
                return item;
            }).Where(x => x != null).Select(x => x !).ToList(), count);
        }
Пример #3
0
        public async Task <string> WriteFile(CmsFile file, CmsType cmsType, Guid id, string fieldName, string?lang, string?currentUser)
        {
            string fileName = GenerateFileName(cmsType, id, fieldName, lang);

            var blob = await azureStorageService.StoreFileAsync(file.Bytes, fileName, file.ContentType).ConfigureAwait(false);

            return(fileName);
        }
Пример #4
0
        public async Task Write <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            if (azureStorageConfig.StorageLocation == AzureStorageLocation.Tables ||
                azureStorageConfig.StorageLocation == AzureStorageLocation.Both)
            {
                await tableService.InsertOrMergeEntityAsync <T>(item, lang).ConfigureAwait(false);
            }
            else
            {
                var fileName = GenerateFileName(cmsType, id, lang);
                await azureStorageService.WriteFileAsJson(item, fileName).ConfigureAwait(false);
            }

            //Write index file for paging and sorting
            var indexFileName = GenerateFileName(cmsType, "_index", lang);
            var typeInfo      = cmsConfiguration.MenuItems.Where(x => x.Key == cmsType.Value).FirstOrDefault();

            if (typeInfo == null)
            {
                return;
            }

            //Get current index file
            (var indexFile, _) = await azureStorageService.ReadFileAsJsonAsync <List <CmsItem> >(indexFileName).ConfigureAwait(false);

            indexFile = indexFile ?? new List <CmsItem>();

            //Remove existing item
            CmsItem?oldItem = indexFile.Where(x => x.Id == item.Id).FirstOrDefault();

            if (oldItem != null)
            {
                indexFile.Remove(oldItem);
            }

            var indexItem = new CmsItem {
                Id               = id,
                CmsType          = cmsType,
                LastModifiedBy   = item.LastModifiedBy,
                LastModifiedDate = item.LastModifiedDate
            };

            if (oldItem != null)
            {
                indexItem.CreatedDate = oldItem.CreatedDate;
            }

            foreach (var prop in typeInfo.ListViewProperties)
            {
                var value = item.AdditionalProperties[prop.Key];
                indexItem.AdditionalProperties[prop.Key] = value;
            }

            indexFile.Add(indexItem);

            await azureStorageService.WriteFileAsJson(indexFile, indexFileName).ConfigureAwait(false);
        }
Пример #5
0
 public ModuleDefinition(string name, string displayName, string description, CmsType cmsType, List<ModuleView> views)
 {
     Name = name;
     DisplayName = displayName;
     Description = description;
     CmsType = cmsType;
     Views = views;
     Instances = new List<ModuleInstance>();
 }
Пример #6
0
        public async Task <string> WriteFile(CmsFile file, CmsType cmsType, Guid id, string fieldName, string?lang, string?currentUser)
        {
            using (Stream stream = new MemoryStream(file.Bytes))
            {
                var response = await _client.FileSystem.AddAsync(stream, fieldName);

                return(response.ToLink().Id.ToString());
            }
        }
Пример #7
0
        public async Task <string> WriteFile(CmsFile file, CmsType cmsType, Guid id, string fieldName, string?lang, string?currentUser)
        {
            using (Stream stream = new MemoryStream(file.Bytes))
            {
                var response = await _client.UploadFileAsync($"{fieldName}.{file.ContentType}", stream);

                return(response.Skylink);
            }
        }
Пример #8
0
        private IReadCmsItem GetReadProvider(CmsType cmsType)
        {
            var readProvider = readCmsItemProviders.Where(x => x.HandlesType(cmsType)).FirstOrDefault();

            if (readProvider == null)
            {
                throw new Exception($"No read provider found that can handle type {cmsType}");
            }
            return(readProvider);
        }
Пример #9
0
        public static string GenerateFileName(CmsType cmsType, string id, string?lang)
        {
            string fileName = $"{cmsType}/{id}.json";

            if (!string.IsNullOrEmpty(lang))
            {
                fileName = $"{cmsType}/{id}/{lang}.json";
            }
            return(fileName);
        }
Пример #10
0
        private async Task <CmsItemTableEntity?> GetCmsItemTableEntity(CmsType cmsType, Guid id, string?lang)
        {
            var table = await GetCloudTableClient(cmsType.ToString());

            TableOperation retreiveOperation = TableOperation.Retrieve <CmsItemTableEntity>(cmsType.ToString(), GetIdForItem(id, lang));
            TableResult    result            = await table.ExecuteAsync(retreiveOperation);

            CmsItemTableEntity?resultEntity = result.Result as CmsItemTableEntity;

            return(resultEntity);
        }
Пример #11
0
        public async Task <T?> Read <T>(CmsType cmsType, Guid id, string?lang) where T : CmsItem
        {
            var item = await dbContext.Set <Model>().FindAsync(id).ConfigureAwait(false);

            if (item != null)
            {
                dbContext.Entry(item).State = EntityState.Detached;
            }

            return(item?.ToObject <T>());
        }
Пример #12
0
 public IDataProvider CreateDataProvider(CmsType cmsType)
 {
     switch(cmsType)
     {
         case CmsType.Convert:
             return new ConvertDataProvider();
         case CmsType.ConvertOnDemand:
             return new ConvertOnDemandDataProvider();
         default:
             throw new Exception("Unsupported CMS Type specified");
     }
 }
Пример #13
0
        public async Task Delete(CmsType cmsType, Guid id, string?lang, string?currentUser)
        {
            var dbObj = await dbContext.Set <Model>().FindAsync(id).ConfigureAwait(false);

            if (dbObj == null)
            {
                throw new Exception($"Obj with ID {id} not found");
            }

            dbContext.Set <Model>().Remove(dbObj);
            await dbContext.SaveChangesAsync();
        }
Пример #14
0
        public async Task Write <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            var    fileName = GenerateFileName(cmsType, id, lang);
            string itemJson = JsonSerializer.Serialize(item);
            await _client.SkyDbSetAsString(privateKey, publicKey, new RegistryKey(fileName), itemJson).ConfigureAwait(false);

            //Write index file for paging and sorting
            var indexFileName = GenerateFileName(cmsType, "_index", lang);
            var typeInfo      = cmsConfiguration.MenuItems.Where(x => x.Key == cmsType.Value).FirstOrDefault();

            if (typeInfo == null)
            {
                return;
            }

            //Get current index file
            List <CmsItem>?indexFile = await GetIndexFile(cmsType).ConfigureAwait(false);

            //Remove existing item
            CmsItem?oldItem = indexFile.Where(x => x.Id == item.Id).FirstOrDefault();

            if (oldItem != null)
            {
                indexFile.Remove(oldItem);
            }

            var indexItem = new CmsItem
            {
                Id               = id,
                CmsType          = cmsType,
                LastModifiedBy   = item.LastModifiedBy,
                LastModifiedDate = item.LastModifiedDate
            };

            if (oldItem != null)
            {
                indexItem.CreatedDate = oldItem.CreatedDate;
            }

            foreach (var prop in typeInfo.ListViewProperties)
            {
                var value = item.AdditionalProperties[prop.Key];
                indexItem.AdditionalProperties[prop.Key] = value;
            }

            indexFile.Add(indexItem);

            string indexFileJson = JsonSerializer.Serialize(indexFile);
            await _client.SkyDbSetAsString(privateKey, publicKey, new RegistryKey(indexFileName), indexFileJson).ConfigureAwait(false);
        }
Пример #15
0
        public async Task Write <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            //Run interceptors before saving
            foreach (var interceptor in writeCmsItemInterceptors.Where(x => x.HandlesType(cmsType)))
            {
                item = await interceptor.InterceptAsync(item, cmsType, id, lang, currentUser).ConfigureAwait(false);
            }
            ;

            //Set id and type property, they might get lost during intercept
            item.Id             = id;
            item.CmsType        = cmsType;
            item.LastModifiedBy = currentUser;

            await Task.WhenAll(writeCmsItemProviders.Where(x => x.HandlesType(cmsType)).Select(x => x.Write(item, cmsType, id, lang, currentUser))).ConfigureAwait(false);
        }
Пример #16
0
        private string GenerateFileName(CmsType cmsType, Guid id, string fieldName, string?lang)
        {
            string fileName = $"{cmsType}/{id}/{lang}/{fieldName}";

            if (string.IsNullOrEmpty(lang))
            {
                fileName = $"{cmsType}/{id}/{fieldName}";
            }

            if (storageConfig.GenerateUniqueFileName)
            {
                fileName = $"{fileName}/{Guid.NewGuid()}";
            }

            return(fileName);
        }
Пример #17
0
        public async Task <T?> Read <T>(CmsType cmsType, Guid id, string?lang) where T : CmsItem
        {
            var fileName = GenerateFileName(cmsType, id, lang);

            var json = await _client.SkyDbGetAsString(publicKey, new RegistryKey(fileName)).ConfigureAwait(false);

            T?file = null;

            if (json != null)
            {
                file = JsonSerializer.Deserialize <T>(json);
            }

            //TODO: Something with creation date?

            return(file);
        }
Пример #18
0
        public async Task Delete(CmsType cmsType, Guid id, string?lang, string?currentUser)
        {
            if (azureStorageConfig.StorageLocation == AzureStorageLocation.Tables ||
                azureStorageConfig.StorageLocation == AzureStorageLocation.Both)
            {
                await tableService.DeleteEntityAsync(cmsType, id, lang).ConfigureAwait(false);

                //Delete all translations
                foreach (var cmsLang in cmsConfiguration.Languages)
                {
                    await tableService.DeleteEntityAsync(cmsType, id, cmsLang).ConfigureAwait(false);
                }
            }
            else
            {
                var fileName = GenerateFileName(cmsType, id, null);

                await azureStorageService.DeleteFileAsync(fileName).ConfigureAwait(false);

                //Get translations
                var files = await azureStorageService.GetFilesFromDirectory($"{cmsType}/{id}").ConfigureAwait(false);

                foreach (var file in files)
                {
                    await azureStorageService.DeleteFileAsync(file.Name).ConfigureAwait(false);
                }
            }

            //Write index file for paging and sorting
            var indexFileName = GenerateFileName(cmsType, "_index", lang);

            //Get current index file
            (var indexFile, _) = await azureStorageService.ReadFileAsJsonAsync <List <CmsItem> >(indexFileName).ConfigureAwait(false);

            indexFile = indexFile ?? new List <CmsItem>();

            //Remove existing item
            var existing = indexFile.Where(x => x.Id == id).FirstOrDefault();

            if (existing != null)
            {
                indexFile.Remove(existing);

                await azureStorageService.WriteFileAsJson(indexFile, indexFileName).ConfigureAwait(false);
            }
        }
Пример #19
0
        private async Task <List <CmsItem> > GetIndexFile(CmsType cmsType)
        {
            //Get index file
            var indexFileName = GenerateFileName(cmsType, "_index", null);

            //Get current index file
            var json = await _client.SkyDbGetAsString(publicKey, new RegistryKey(indexFileName)).ConfigureAwait(false);

            var indexFile = new List <CmsItem>();

            if (json != null)
            {
                indexFile = JsonSerializer.Deserialize <List <CmsItem> >(json);
                indexFile = indexFile ?? new List <CmsItem>();
            }

            return(indexFile);
        }
Пример #20
0
        public async Task <T?> GetEntityAsync <T>(CmsType cmsType, Guid id, string?lang) where T : CmsItem
        {
            try
            {
                CmsItemTableEntity?resultEntity = await GetCmsItemTableEntity(cmsType, id, lang);

                if (resultEntity != null && resultEntity.JsonData != null)
                {
                    return(JsonSerializer.Deserialize <T>(resultEntity.JsonData));
                }

                return(null);
            }
            catch (StorageException e)
            {
                throw;
            }
        }
Пример #21
0
        public async Task DeleteEntityAsync(CmsType cmsType, Guid id, string?lang)
        {
            try
            {
                CmsItemTableEntity?resultEntity = await GetCmsItemTableEntity(cmsType, id, lang);

                if (resultEntity != null)
                {
                    var table = await GetCloudTableClient(cmsType.ToString());

                    TableOperation deleteOperation = TableOperation.Delete(resultEntity);
                    TableResult    result          = await table.ExecuteAsync(deleteOperation);
                }
            }
            catch (StorageException e)
            {
                throw;
            }
        }
Пример #22
0
        public async Task Delete(CmsType cmsType, Guid id, string?lang, string?currentUser)
        {
            //Unable to delete actual files from Skynet

            //Get current index file
            List <CmsItem>?indexFile = await GetIndexFile(cmsType).ConfigureAwait(false);

            //Remove existing item
            var existing = indexFile.Where(x => x.Id == id).FirstOrDefault();

            if (existing != null)
            {
                indexFile.Remove(existing);

                string indexFileJson = JsonSerializer.Serialize(indexFile);
                var    indexFileName = GenerateFileName(cmsType, "_index", lang);
                await _client.SkyDbSetAsString(privateKey, publicKey, new RegistryKey(indexFileName), indexFileJson).ConfigureAwait(false);
            }
        }
Пример #23
0
        public async Task Write <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            var dbObj = item.ToObject <Model>();

            if (dbObj == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var existing = await this.Read <T>(cmsType, id, lang);

            if (existing == null)
            {
                dbContext.Set <Model>().Add(dbObj);
            }
            else
            {
                dbContext.Set <Model>().Update(dbObj);
            }

            await dbContext.SaveChangesAsync();
        }
        public Task <T> InterceptAsync <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            var cmsUser = item.ToObject <CmsUser>();

            if (cmsUser == null)
            {
                throw new Exception($"Unable to convert item to {cmsType}");
            }

            //Encrypt password
            cmsUser.PasswordEncrypted = BCrypt.Net.BCrypt.HashPassword(cmsUser.Password);
            cmsUser.Password          = string.Empty; //Do not store original password

            var modifiedItem = cmsUser.ToObject <T>();

            if (modifiedItem == null)
            {
                throw new Exception($"Item was null after converting ({cmsType}).");
            }

            return(Task.FromResult(modifiedItem));
        }
Пример #25
0
        public async Task <T?> Read <T>(CmsType cmsType, Guid id, string?lang) where T : CmsItem
        {
            if (azureStorageConfig.StorageLocation == AzureStorageLocation.Tables ||
                azureStorageConfig.StorageLocation == AzureStorageLocation.Both)
            {
                return(await tableService.GetEntityAsync <T>(cmsType, id, lang));
            }
            else
            {
                var fileName = GenerateFileName(cmsType, id, lang);

                (var file, DateTimeOffset? createdDate) = await azureStorageService.ReadFileAsJsonAsync <T>(fileName);

                //Set the CreatedDate
                if (file != null && createdDate.HasValue)
                {
                    file.CreatedDate = createdDate.Value;
                }

                return(file);
            }
        }
Пример #26
0
        public Task <T> InterceptAsync <T>(T item, CmsType cmsType, Guid id, string?lang, string?currentUser) where T : CmsItem
        {
            return(Task.FromResult(item));
            //using (var stream = new MemoryStream())
            //{
            //    using (var writer = new Utf8JsonWriter(stream))
            //    {
            //        writer.WriteStartObject();
            //        writer.WriteString("date", DateTimeOffset.UtcNow);
            //        writer.WriteNumber("temp", 42);
            //        writer.WriteEndObject();
            //        writer.Flush();

            //        JsonDocument doc = await JsonDocument.ParseAsync(stream);

            //        var el = doc.RootElement;

            //        if (item.AdditionalProperties.ContainsKey("test"))
            //            item.AdditionalProperties["test"] = el;
            //        else
            //            item.AdditionalProperties.Add("test", el);
            //    }
            //}
        }
Пример #27
0
        private void LoadModuleDataFromSource(CmsType cmsType)
        {
            // Add new module registry data for specified CMS type
            var data = ServiceContext.DataProviderFactory.CreateDataProvider(cmsType).GetModuleRegistryData().Data;
            var viewModel = JsonConvert.DeserializeObject<ModuleRegistryViewModel>(data);

            if (viewModel != null)
            {
                foreach (var module in viewModel.Modules)
                {
                    //module.DisplayName += " (" + cmsType.ToString() + ")";
                    Modules.Add(module.Name, module);

                    foreach (var instance in module.Instances)
                    {
                        if (!ModuleInstances.ContainsKey(instance.Id))
                            ModuleInstances.Add(instance.Id, instance);
                    }
                }
            }
        }
Пример #28
0
 public bool HandlesType(CmsType cmsType) => !azureStorageConfig.ExcludedTypes.Contains(cmsType);
Пример #29
0
 public bool CanSort(CmsType cmsType) => true;
Пример #30
0
 public DigitalAssetManager(DataProxyBase dataProxy)
 {
     _dataProxy = dataProxy;
     _settings = dataProxy.ProcessRequest<ReadCmsIntegrationSettingsResponse>(new ReadCmsIntegrationSettingsRequest()).Settings;
     _cmsType = ResolveAssetManagerType();
 }
Пример #31
0
 public static string GenerateFileName(CmsType cmsType, Guid id, string?lang)
 {
     return(GenerateFileName(cmsType, id.ToString(), lang));
 }
Пример #32
0
 public Task Delete(CmsType cmsType, Guid id, string?lang, string?currentUser)
 {
     return(Task.WhenAll(writeCmsItemProviders.Where(x => x.HandlesType(cmsType)).Select(x => x.Delete(cmsType, id, lang, currentUser))));
 }
Пример #33
0
 public CmsItemTableEntity(string id, CmsType cmsType)
 {
     PartitionKey = cmsType.ToString();
     RowKey       = id.ToString();
 }
Пример #34
0
        public Task <T?> Read <T>(CmsType cmsType, Guid id, string?lang) where T : CmsItem
        {
            IReadCmsItem firstReadProdiver = GetReadProvider(cmsType);

            return(firstReadProdiver.Read <T>(cmsType, id, lang));
        }