public virtual async Task <ActionResult> GenerateNavbar()
        {
            var idMap = new Dictionary <int, int>();

            foreach (var category in _postCategoriesService.GetAsEnumerable())
            {
                var id = await _navBarService.AddAsync(new TblNavBarItems()
                {
                    Index     = category.DisplayOrder,
                    InnerHtml = category.CategoryName,
                    Name      = category.CategoryName,
                    Url       = "/Categories/" + category.Slug
                });

                var catLocals = await _localizedEntityService.GetLocalizedPropertiesAsync(category.Id, "TblPostCategories", "CategoryName");

                foreach (var local in catLocals)
                {
                    await _localizedEntityService.AddAsync(new TblLocalizedProperty()
                    {
                        LanguageId     = local.LanguageId,
                        EntityId       = id,
                        LocaleKeyGroup = "TblNavBarItems",
                        LocaleKey      = "InnerHtml",
                        LocaleValue    = local.LocaleValue
                    });
                }

                idMap.Add(category.Id, id);
            }

            foreach (var category in _postCategoriesService.GetAsEnumerable().Where(p => p.ParentCategoryId != null))
            {
                var navbarItem = await _navBarService.FindByIdAsync(idMap[category.Id]);

                navbarItem.ParentItemId = idMap[category.ParentCategoryId.Value];
                await _navBarService.UpdateAsync(navbarItem);
            }

            SuccessNotification(_localizationService.GetResource("OperationCompletedSuccessfully"));

            return(RedirectToAction("Index"));
        }
Example #2
0
        public virtual async Task <T> LoadSettingAsync <T>() where T : ISettings, new()
        {
            // Try get result from cache
            if (_memoryCache.Contains(CacheTags.Setting, typeof(T).FullName))
            {
                return(_memoryCache.GetObject <T>(CacheTags.Setting, typeof(T).FullName));
            }

            var settings = Activator.CreateInstance <T>();

            foreach (var prop in typeof(T).GetProperties())
            {
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }

                if (prop.PropertyType == typeof(LocalizedString))
                {
                    string key     = typeof(T).Name + "." + prop.Name;
                    var    setting = FindByKey <string>(key);
                    if (setting == null)
                    {
                        continue;
                    }

                    var value = new LocalizedString(setting);
                    foreach (var localizedProperty in await _localizedEntityService.GetLocalizedPropertiesAsync(0,
                                                                                                                typeof(T).Name, prop.Name))
                    {
                        value.Add(localizedProperty.LanguageId, localizedProperty.LocaleValue);
                    }

                    prop.SetValue(settings, value, null);
                }
                else
                {
                    var key     = typeof(T).Name + "." + prop.Name;
                    var setting = FindByKey <string>(key);
                    if (setting == null)
                    {
                        continue;
                    }

                    if (TypeDescriptor.GetConverter(prop.PropertyType).CanConvertFrom(typeof(string)))
                    {
                        if (!TypeDescriptor.GetConverter(prop.PropertyType).IsValid(setting))
                        {
                            continue;
                        }

                        object value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromInvariantString(setting);

                        prop.SetValue(settings, value, null);
                    }
                    else if (prop.PropertyType.IsClass && prop.PropertyType.IsSerializable)
                    {
                        try
                        {
                            var obj = setting.JsonToObject(prop.PropertyType);
                            prop.SetValue(settings, obj, null);
                        }
                        catch
                        {
                        }
                    }
                }
            }

            _memoryCache.AddObject(CacheTags.Setting, settings, TimeSpan.FromDays(30), typeof(T).FullName);

            return(settings);
        }