コード例 #1
0
        public static async System.Threading.Tasks.Task <ViewModels.MixModules.ReadMvcViewModel> GetModuleAsync(string name, string culture, IUrlHelper url = null)
        {
            var cacheKey = $"vm_{culture}_module_{name}_mvc";
            var module   = new Domain.Core.ViewModels.RepositoryResponse <ViewModels.MixModules.ReadMvcViewModel>();

            // Load From Cache
            if (MixService.GetConfig <bool>("IsCache"))
            {
                module = await MixCacheService.GetAsync <Mix.Domain.Core.ViewModels.RepositoryResponse <ViewModels.MixModules.ReadMvcViewModel> >(cacheKey);
            }

            // If not cached yet => load from db
            if (module == null || !module.IsSucceed)
            {
                module = ViewModels.MixModules.ReadMvcViewModel.GetBy(m => m.Name == name && m.Specificulture == culture);
            }

            // If load successful => load details
            if (module.IsSucceed)
            {
                if (url != null && module.Data.Posts != null)
                {
                    module.Data.Posts.Items.ForEach(a => { a.Post.DetailsUrl = url.RouteUrl("Post", new { id = a.PostId, seoName = a.Post.SeoName }); });
                }
                await MixCacheService.SetAsync(cacheKey, module);
            }

            return(module.Data);
        }
コード例 #2
0
        protected async Task <RepositoryResponse <TModel> > SaveAsync <TView>(JObject obj, Expression <Func <TModel, bool> > predicate)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            if (obj != null)
            {
                List <EntityField> fields = new List <EntityField>();
                Type type = typeof(TModel);
                foreach (var item in obj.Properties())
                {
                    var          propName     = item.Name.ToTitleCase();
                    PropertyInfo propertyInfo = type.GetProperty(propName);
                    if (propertyInfo != null)
                    {
                        object val   = Convert.ChangeType(item.Value, propertyInfo.PropertyType);
                        var    field = new EntityField()
                        {
                            PropertyName  = propName,
                            PropertyValue = val
                        };
                        fields.Add(field);
                    }
                }
                var result = await DefaultRepository <TDbContext, TModel, TView> .Instance.UpdateFieldsAsync(predicate, fields);

                await MixCacheService.RemoveCacheAsync();

                return(result);
            }
            return(new RepositoryResponse <TModel>());
        }
コード例 #3
0
        public static async System.Threading.Tasks.Task <ViewModels.MixTemplates.ReadListItemViewModel> GetTemplateByPath(string templatePath)
        {
            var cacheKey = $"vm_template_{templatePath}_listitem";
            RepositoryResponse <ViewModels.MixTemplates.ReadListItemViewModel> getData = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getData = await MixCacheService.GetAsync <RepositoryResponse <ViewModels.MixTemplates.ReadListItemViewModel> >(cacheKey);
            }
            if (getData == null)
            {
                string[] tmp = templatePath.Split('/');
                if (tmp[1].IndexOf('.') > 0)
                {
                    tmp[1] = tmp[1].Substring(0, tmp[1].IndexOf('.'));
                }
                getData = ViewModels.MixTemplates.ReadListItemViewModel.Repository.GetSingleModel(m => m.FolderType == tmp[0] && m.FileName == tmp[1]);
                if (getData.IsSucceed)
                {
                    await MixCacheService.SetAsync(cacheKey, getData);
                }
            }

            return(getData.Data);
        }
コード例 #4
0
        public async Task <RepositoryResponse <bool> > InitLanguages([FromBody] List <MixLanguage> model)
        {
            if (model != null)
            {
                var result = new RepositoryResponse <bool>();
                if (MixService.GetConfig <int>("InitStatus") == 3)
                {
                    string         culture = MixService.GetConfig <string>("DefaultCulture");
                    InitCmsService sv      = new InitCmsService();
                    result = await sv.InitLanguagesAsync(culture, model);

                    if (result.IsSucceed)
                    {
                        MixService.LoadFromDatabase();
                        MixService.SetConfig("InitStatus", 4);
                        MixService.SetConfig("IsInit", true);
                        MixService.SaveSettings();
                        _ = MixCacheService.RemoveCacheAsync();
                        MixService.Reload();
                    }
                }
                return(result);
            }
            return(new RepositoryResponse <bool>());
        }
コード例 #5
0
        protected RepositoryResponse <List <TView> > SaveList <TView>(List <TView> lstVm, bool isSaveSubModel)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            var result = new RepositoryResponse <List <TView> >()
            {
                IsSucceed = true
            };

            if (lstVm != null)
            {
                foreach (var vm in lstVm)
                {
                    var tmp = vm.SaveModel(isSaveSubModel);
                    result.IsSucceed = result.IsSucceed && tmp.IsSucceed;
                    if (!tmp.IsSucceed)
                    {
                        result.Exception = tmp.Exception;
                        result.Errors.AddRange(tmp.Errors);
                    }
                }
                Task.Run(() => MixCacheService.RemoveCacheAsync());
                return(result);
            }

            return(result);
        }
コード例 #6
0
ファイル: DeleteViewModel.cs プロジェクト: mixcore/mix.core
        public override async Task RemoveCache(MixDatabaseColumn model, MixCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            string key = $"_{MixDatabaseId}";
            await MixCacheService.RemoveCacheAsync(typeof(MixDatabase), key);

            await base.RemoveCache(model, _context, _transaction);
        }
コード例 #7
0
        protected async Task <RepositoryResponse <TView> > GetSingleAsync <TView, TModel>(string key, Expression <Func <TModel, bool> > predicate = null, TModel model = null)
            where TView : ViewModelBase <TDbContext, TModel, TView>
            where TModel : class
        {
            var cacheKey = $"{typeof(TModel).Name}_details_{_lang}_{key}";
            RepositoryResponse <TView> data = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                data = await MixCacheService.GetAsync <RepositoryResponse <TView> >(cacheKey);
            }
            if (data == null)
            {
                if (predicate != null)
                {
                    data = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetSingleModelAsync(predicate);

                    //_memoryCache.Set(cacheKey, data);
                    await MixCacheService.SetAsync(cacheKey, data);
                }
                else
                {
                    data = new RepositoryResponse <TView>()
                    {
                        IsSucceed = true,
                        Data      = DefaultRepository <TDbContext, TModel, TView> .Instance.ParseView(model)
                    };
                }
                AlertAsync("Add Cache", 200, cacheKey);
            }
            data.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
            return(data);
        }
コード例 #8
0
        async System.Threading.Tasks.Task <IActionResult> TagAsync(string tagName)
        {
            string seoName = "tag";

            ViewData["TopPages"] = await GetCategoryAsync(CatePosition.Nav, seoName);

            ViewData["HeaderPages"] = await GetCategoryAsync(CatePosition.Top, seoName);

            ViewData["FooterPages"] = await GetCategoryAsync(CatePosition.Footer, seoName);

            ViewData["LeftPages"] = await GetCategoryAsync(CatePosition.Left, seoName);

            ViewData["TagName"] = tagName;

            int?   pageSize       = MixService.GetConfig <int?>("TagPageSize");
            string orderBy        = MixService.GetConfig <string>("OrderBy");
            int    orderDirection = MixService.GetConfig <int>("OrderDirection");

            int.TryParse(Request.Query["pageIndex"], out int pageIndex);
            var cacheKey = $"mvc_{_culture}_tag_{tagName}_{pageSize}_{pageIndex}_{orderBy}_{orderDirection}";
            RepositoryResponse <Lib.ViewModels.MixPages.ReadMvcViewModel> getPage = null;


            if (MixService.GetConfig <bool>("IsCache"))
            {
                getPage = await MixCacheService.GetAsync <RepositoryResponse <Lib.ViewModels.MixPages.ReadMvcViewModel> >(cacheKey);
            }
            if (getPage == null)
            {
                Expression <Func <MixPage, bool> > predicate;

                predicate = p =>
                            p.SeoName == "tag" &&
                            p.Status == (int)MixContentStatus.Published && p.Specificulture == _culture;

                getPage = await Lib.ViewModels.MixPages.ReadMvcViewModel.Repository.GetSingleModelAsync(predicate);

                if (getPage.IsSucceed)
                {
                    getPage.Data.LoadDataByTag(tagName, orderBy, orderDirection, pageIndex: pageIndex, pageSize: pageSize);
                    GeneratePageDetailsUrls(getPage.Data);
                    MixCacheService.SetAsync(cacheKey, getPage);
                }
            }

            if (getPage.IsSucceed)// && getPage.Data.View != null
            {
                ViewData["Title"]               = getPage.Data.SeoTitle;
                ViewData["Description"]         = getPage.Data.SeoDescription;
                ViewData["Keywords"]            = getPage.Data.SeoKeywords;
                ViewData["Image"]               = getPage.Data.ImageUrl;
                ViewData["PageClass"]           = getPage.Data.CssClass;
                getPage.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
                return(View(getPage.Data));
            }
            else
            {
                return(Redirect($"/error/404"));
            }
        }
コード例 #9
0
        protected async System.Threading.Tasks.Task <IActionResult> Data(string id)
        {
            RepositoryResponse <Lib.ViewModels.MixAttributeSetDatas.ReadMvcViewModel> getPost = null;
            var cacheKey = $"mvc_{culture}_post_{id}";

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getPost = await MixCacheService.GetAsync <RepositoryResponse <Lib.ViewModels.MixAttributeSetDatas.ReadMvcViewModel> >(cacheKey);
            }
            if (getPost == null)
            {
                Expression <Func <MixAttributeSetData, bool> > predicate;
                predicate = p =>
                            p.Id == id &&
                            p.Status == MixContentStatus.Published.ToString() &&
                            p.Specificulture == culture;

                getPost = await Lib.ViewModels.MixAttributeSetDatas.ReadMvcViewModel.Repository.GetFirstModelAsync(predicate);
            }

            if (getPost.IsSucceed)
            {
                getPost.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
                return(View(getPost.Data));
            }
            else
            {
                return(await Page("404"));
            }
        }
コード例 #10
0
        [RequestFormSizeLimit(valueCountLimit: 214748364)] // 200Mb
        public async Task <RepositoryResponse <Cms.Lib.ViewModels.MixThemes.InitViewModel> > Save([FromForm] string model, [FromForm] IFormFile assets, [FromForm] IFormFile theme)
        {
            var json = JObject.Parse(model);
            var data = json.ToObject <Lib.ViewModels.MixThemes.InitViewModel>();

            if (theme != null)
            {
                string importFolder = $"Imports/Themes/{DateTime.UtcNow.ToString("dd-MM-yyyy")}/{data.Name}";
                FileRepository.Instance.SaveWebFile(theme, theme.FileName, importFolder);
                data.TemplateAsset = new Lib.ViewModels.FileViewModel(theme, importFolder);
            }
            else
            {
                if (data.IsCreateDefault)
                {
                    data.TemplateAsset = new Lib.ViewModels.FileViewModel()
                    {
                        Filename   = "default",
                        Extension  = ".zip",
                        FileFolder = "Imports/Themes"
                    };
                }
                else
                {
                    data.TemplateAsset = new Lib.ViewModels.FileViewModel()
                    {
                        Filename   = "default_blank",
                        Extension  = ".zip",
                        FileFolder = "Imports/Themes"
                    };
                }
            }

            if (data != null)
            {
                string culture = MixService.GetConfig <string>("DefaultCulture");
                data.Title          = MixService.GetConfig <string>("SiteName", culture);
                data.Name           = SeoHelper.GetSEOString(data.Title);
                data.CreatedBy      = User.Claims.FirstOrDefault(c => c.Type == "Username")?.Value ?? "Init";
                data.Specificulture = _lang;
                var result = await data.SaveModelAsync(true);

                if (result.IsSucceed)
                {
                    // MixService.SetConfig<string>("SiteName", _lang, data.Title);
                    MixService.LoadFromDatabase();
                    MixService.SetConfig("InitStatus", 3);
                    MixService.SetConfig("IsInit", false);
                    MixService.SaveSettings();
                    _ = MixCacheService.RemoveCacheAsync();
                    MixService.Reload();
                }
                return(result);
            }
            return(new RepositoryResponse <Lib.ViewModels.MixThemes.InitViewModel>()
            {
                Status = 501
            });
        }
コード例 #11
0
        async System.Threading.Tasks.Task <List <Lib.ViewModels.MixPages.ReadListItemViewModel> > GetCategoryAsync(MixEnums.CatePosition position, string seoName)
        {
            List <Lib.ViewModels.MixPages.ReadListItemViewModel> result = null;
            var cacheKey = $"mvc_menus_{position}";

            if (MixService.GetConfig <bool>("IsCache"))
            {
                result = await MixCacheService.GetAsync <List <Lib.ViewModels.MixPages.ReadListItemViewModel> >(cacheKey);
            }
            if (result == null)
            {
                var getTopCates = Lib.ViewModels.MixPages.ReadListItemViewModel.Repository.GetModelListBy
                                      (c => c.Specificulture == _culture && c.MixPagePosition.Any(
                                          p => p.PositionId == (int)position)
                                      );

                result = getTopCates.Data ?? new List <Lib.ViewModels.MixPages.ReadListItemViewModel>();
                foreach (var cate in result)
                {
                    switch (cate.Type)
                    {
                    case MixPageType.Blank:
                        foreach (var child in cate.Childs)
                        {
                            child.Page.DetailsUrl = GenerateDetailsUrl(
                                new { culture = _culture, seoName = child.Page.SeoName });
                        }
                        break;

                    case MixPageType.StaticUrl:
                        cate.DetailsUrl = cate.StaticUrl;
                        break;

                    case MixPageType.Home:
                    case MixPageType.ListPost:
                    case MixPageType.Post:
                    case MixPageType.Modules:
                    default:
                        cate.DetailsUrl = GenerateDetailsUrl(
                            new { culture = _culture, seoName = cate.SeoName }
                            );
                        break;
                    }
                }
            }

            foreach (var cate in result)
            {
                cate.IsActived = (cate.SeoName == seoName ||
                                  (cate.Type == MixPageType.Home && string.IsNullOrEmpty(seoName)));
                cate.Childs.ForEach((Action <Lib.ViewModels.MixPagePages.ReadViewModel>)(c =>
                {
                    c.IsActived = (
                        c.Page.SeoName == seoName);
                    cate.IsActived = cate.IsActived || c.IsActived;
                }));
            }
            return(result);
        }
コード例 #12
0
        async System.Threading.Tasks.Task <IActionResult> PostViewAsync(int id)
        {
            RepositoryResponse <Lib.ViewModels.MixPosts.ReadMvcViewModel> getPost = null;
            var cacheKey = $"mvc_{_culture}_post_{id}";

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getPost = await MixCacheService.GetAsync <RepositoryResponse <Lib.ViewModels.MixPosts.ReadMvcViewModel> >(cacheKey);
            }
            if (getPost == null)
            {
                Expression <Func <MixPost, bool> > predicate;
                predicate = p =>
                            p.Id == id &&
                            p.Status == (int)MixContentStatus.Published &&
                            p.Specificulture == _culture;

                getPost = await Lib.ViewModels.MixPosts.ReadMvcViewModel.Repository.GetSingleModelAsync(predicate);

                if (getPost.IsSucceed)
                {
                    getPost.Data.DetailsUrl = GenerateDetailsUrl(
                        new { culture = _culture, action = "post", id = getPost.Data.Id, seoName = getPost.Data.SeoName }
                        );
                    //Generate details url for related posts
                    if (getPost.IsSucceed)
                    {
                        if (getPost.Data.PostNavs != null && getPost.Data.PostNavs.Count > 0)
                        {
                            getPost.Data.PostNavs.ForEach(n => n.RelatedPost.DetailsUrl = GenerateDetailsUrl(
                                                              new { culture = _culture, action = "post", id = n.RelatedPost.Id, seoName = n.RelatedPost.SeoName }));
                        }
                        MixCacheService.SetAsync(cacheKey, getPost);
                    }
                }
            }

            if (getPost.IsSucceed)
            {
                ViewData["TopPages"] = await GetCategoryAsync(CatePosition.Nav, getPost.Data.SeoName);

                ViewData["HeaderPages"] = await GetCategoryAsync(CatePosition.Top, getPost.Data.SeoName);

                ViewData["FooterPages"] = await GetCategoryAsync(CatePosition.Footer, getPost.Data.SeoName);

                ViewData["LeftPages"] = await GetCategoryAsync(CatePosition.Left, getPost.Data.SeoName);

                ViewData["Title"]               = getPost.Data.SeoTitle;
                ViewData["Description"]         = getPost.Data.SeoDescription;
                ViewData["Keywords"]            = getPost.Data.SeoKeywords;
                ViewData["Image"]               = getPost.Data.ImageUrl;
                getPost.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
                return(View(getPost.Data));
            }
            else
            {
                return(Redirect($"/error/404"));
            }
        }
コード例 #13
0
        public override Task RemoveCache(MixDatabaseColumn model, MixCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            _context ??= new MixCmsContext();
            var relatedDatabaseId = _context.MixDatabase.Where(m => m.Id == MixDatabaseId).Select(m => m.Id);

            MixCacheService.RemoveCacheAsync(typeof(MixDatabase), relatedDatabaseId.ToString());
            return(base.RemoveCache(model, _context, _transaction));
        }
コード例 #14
0
        public virtual async Task <ActionResult> ClearCacheAsync(string id)
        {
            string key = $"_{id}";

            key += !string.IsNullOrEmpty(_lang) ? $"_{_lang}" : string.Empty;
            await MixCacheService.RemoveCacheAsync(typeof(TModel), key);

            return(NoContent());
        }
コード例 #15
0
        protected async Task <RepositoryResponse <List <TView> > > SaveListAsync <TView>(List <TView> lstVm, bool isSaveSubModel)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            var result = await DefaultRepository <TDbContext, TModel, TView> .Instance.SaveListModelAsync(lstVm, isSaveSubModel);

            if (result.IsSucceed)
            {
                await MixCacheService.RemoveCacheAsync();
            }

            return(result);
        }
コード例 #16
0
        protected async Task <RepositoryResponse <List <TModel> > > DeleteListAsync <TView>(bool isRemoveRelatedModel, Expression <Func <TModel, bool> > predicate, bool isDeleteRelated = false)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            var data = await DefaultRepository <TDbContext, TModel, TView> .Instance.RemoveListModelAsync(isRemoveRelatedModel, predicate);

            if (data.IsSucceed)
            {
                await MixCacheService.RemoveCacheAsync();
            }

            return(data);
        }
コード例 #17
0
        async System.Threading.Tasks.Task <IActionResult> AliasAsync(string seoName)
        {
            // Home Page
            int?   pageSize       = MixService.GetConfig <int?>("TagPageSize");
            string orderBy        = MixService.GetConfig <string>("OrderBy");
            int    orderDirection = MixService.GetConfig <int>("OrderDirection");

            int.TryParse(Request.Query["pageIndex"], out int pageIndex);
            var cacheKey = $"mvc_{_culture}_alias_{seoName}_{pageSize}_{pageIndex}_{orderBy}_{orderDirection}";

            RepositoryResponse <Lib.ViewModels.MixUrlAliases.UpdateViewModel> getAlias = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getAlias = await MixCacheService.GetAsync <RepositoryResponse <Lib.ViewModels.MixUrlAliases.UpdateViewModel> >(cacheKey);
            }
            if (getAlias == null)
            {
                Expression <Func <MixUrlAlias, bool> > predicate;

                predicate = p =>
                            p.Alias == seoName &&
                            p.Status == (int)MixContentStatus.Published && p.Specificulture == _culture;

                getAlias = await Lib.ViewModels.MixUrlAliases.UpdateViewModel.Repository.GetSingleModelAsync(predicate);

                if (getAlias.IsSucceed)
                {
                    MixCacheService.SetAsync(cacheKey, getAlias);
                }
            }

            if (getAlias.IsSucceed)// && getPage.Data.View != null
            {
                switch (getAlias.Data.Type)
                {
                case UrlAliasType.Page:
                    return(await PageAsync(int.Parse(getAlias.Data.SourceId)));

                case UrlAliasType.Post:
                    return(await PostViewAsync(int.Parse(getAlias.Data.SourceId)));

                case UrlAliasType.Module:     // TODO: Create view for module
                case UrlAliasType.ModuleData: // TODO: Create view for module data
                default:
                    return(await PageError());
                }
            }
            else
            {
                return(await PageAsync(seoName));
            }
        }
コード例 #18
0
        protected async Task <RepositoryResponse <TView> > SaveAsync <TView>(TView vm, bool isSaveSubModel)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            if (vm != null)
            {
                var result = await vm.SaveModelAsync(isSaveSubModel).ConfigureAwait(false);

                await MixCacheService.RemoveCacheAsync();

                return(result);
            }
            return(new RepositoryResponse <TView>());
        }
コード例 #19
0
        public async Task <RepositoryResponse <List <ReadViewModel> > > UpdateInfos([FromBody] List <ReadViewModel> models)
        {
            if (models != null)
            {
                await MixCacheService.RemoveCacheAsync();

                return(await ReadViewModel.UpdateInfosAsync(models));
            }
            else
            {
                return(new RepositoryResponse <List <ReadViewModel> >());
            }
        }
コード例 #20
0
ファイル: BaseController.cs プロジェクト: teksoi/mix.core
        protected async System.Threading.Tasks.Task <IActionResult> Post(int id)
        {
            RepositoryResponse <Lib.ViewModels.MixPosts.ReadMvcViewModel> getPost = null;
            var cacheKey = $"mvc_{culture}_post_{id}";

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getPost = await MixCacheService.GetAsync <RepositoryResponse <Lib.ViewModels.MixPosts.ReadMvcViewModel> >(cacheKey);
            }
            if (getPost == null)
            {
                Expression <Func <MixPost, bool> > predicate;
                predicate = p =>
                            p.Id == id &&
                            p.Status == MixContentStatus.Published.ToString() &&
                            p.Specificulture == culture;

                getPost = await Lib.ViewModels.MixPosts.ReadMvcViewModel.Repository.GetFirstModelAsync(predicate);

                if (getPost.IsSucceed)
                {
                    getPost.Data.DetailsUrl = GenerateDetailsUrl(
                        new { culture = culture, action = "post", id = getPost.Data.Id, seoName = getPost.Data.SeoName }
                        );
                    //Generate details url for related posts
                    if (getPost.IsSucceed)
                    {
                        if (getPost.Data.PostNavs != null && getPost.Data.PostNavs.Count > 0)
                        {
                            getPost.Data.PostNavs.ForEach(n => n.RelatedPost.DetailsUrl = GenerateDetailsUrl(
                                                              new { action = "post", culture = culture, id = n.RelatedPost.Id, seoName = n.RelatedPost.SeoName }));
                        }
                        //_ = MixCacheService.SetAsync(cacheKey, getPost);
                    }
                }
            }

            if (getPost.IsSucceed)
            {
                ViewData["Title"]               = getPost.Data.SeoTitle;
                ViewData["Description"]         = getPost.Data.SeoDescription;
                ViewData["Keywords"]            = getPost.Data.SeoKeywords;
                ViewData["Image"]               = getPost.Data.ImageUrl;
                getPost.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
                return(View(getPost.Data));
            }
            else
            {
                return(await Error("404"));
            }
        }
コード例 #21
0
        protected async Task <RepositoryResponse <TModel> > DeleteAsync <TView>(Expression <Func <TModel, bool> > predicate, bool isDeleteRelated = false)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            var result = new RepositoryResponse <TModel>();
            var data   = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetSingleModelAsync(predicate);

            if (data.IsSucceed)
            {
                result = await data.Data.RemoveModelAsync(isDeleteRelated).ConfigureAwait(false);

                if (result.IsSucceed)
                {
                    await MixCacheService.RemoveCacheAsync();
                }
            }

            return(result);
        }
コード例 #22
0
        protected async Task <RepositoryResponse <TModel> > DeleteAsync <TView>(TView data, bool isDeleteRelated = false)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            if (data != null)
            {
                var result = await data.RemoveModelAsync(isDeleteRelated).ConfigureAwait(false);

                if (result.IsSucceed)
                {
                    await MixCacheService.RemoveCacheAsync();
                }

                return(result);
            }
            return(new RepositoryResponse <TModel>()
            {
                IsSucceed = false
            });
        }
コード例 #23
0
ファイル: Startup.cs プロジェクト: mixcore/mix.core
        // Mix: Check custom cms config
        private void VerifyInitData(IServiceCollection services)
        {
            // Mix: Migrate db if already inited

            if (!MixService.GetAppSetting <bool>(MixAppSettingKeywords.IsInit))
            {
                using (var ctx = MixService.GetDbContext())
                {
                    ctx.Database.Migrate();
                    var transaction      = ctx.Database.BeginTransaction();
                    var sysDatabasesFile = MixFileRepository.Instance.GetFile("sys_databases", MixFileExtensions.Json, $"{MixFolders.DataFolder}");
                    var sysDatabases     = JObject.Parse(sysDatabasesFile.Content)["data"].ToObject <List <Lib.ViewModels.MixDatabases.ImportViewModel> >();
                    foreach (var db in sysDatabases)
                    {
                        if (!ctx.MixDatabase.Any(m => m.Name == db.Name))
                        {
                            db.Id = 0;
                            db.SaveModel(true, ctx, transaction);
                        }
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                using (var cacheCtx = MixCacheService.GetCacheDbContext())
                {
                    cacheCtx.Database.Migrate();
                }
                var serviceProvider = services.BuildServiceProvider();
                var _roleManager    = serviceProvider.GetService <RoleManager <IdentityRole> >();
                InitCmsService.InitRolesAsync(_roleManager).GetAwaiter();
            }

            // Mix: Check if require ssl
            if (MixService.GetAppSetting <bool>("IsHttps"))
            {
                services.AddHttpsRedirection(options =>
                {
                    options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
                    options.HttpsPort          = 443;
                });
            }
        }
コード例 #24
0
        public static async System.Threading.Tasks.Task <ViewModels.MixPages.ReadMvcViewModel> GetPageAsync(int id, string culture)
        {
            var cacheKey = $"vm_{culture}_page_{id}_mvc";
            RepositoryResponse <ViewModels.MixPages.ReadMvcViewModel> getPage = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                getPage = await MixCacheService.GetAsync <RepositoryResponse <ViewModels.MixPages.ReadMvcViewModel> >(cacheKey);
            }
            if (getPage == null)
            {
                getPage = ViewModels.MixPages.ReadMvcViewModel.Repository.GetSingleModel(m => m.Id == id && m.Specificulture == culture);
                if (getPage.IsSucceed)
                {
                    await MixCacheService.SetAsync(cacheKey, getPage);
                }
            }

            return(getPage.Data);
        }
コード例 #25
0
        public override async Task <RepositoryResponse <bool> > RemoveRelatedModelsAsync(DeleteViewModel view, MixCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };
            var removeDataIds     = _context.MixDatabaseDataValue.Where(m => m.MixDatabaseColumnId == Id).Select(m => m.DataId).ToList();
            var removeFieldValues = await MixDatabaseDataValues.DeleteViewModel.Repository.RemoveListModelAsync(false, f => f.MixDatabaseColumnId == Id
                                                                                                                , _context, _transaction);

            ViewModelHelper.HandleResult(removeFieldValues, ref result);
            if (result.IsSucceed)
            {
                foreach (var item in removeDataIds)
                {
                    _ = MixCacheService.RemoveCacheAsync(typeof(MixDatabaseData), item);
                }
            }
            return(result);
        }
コード例 #26
0
        protected async Task <RepositoryResponse <PaginationModel <TView> > > GetListAsync <TView>(string key, RequestPaging request, Expression <Func <TModel, bool> > predicate = null, TModel model = null)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            var cacheKey = $"api_{_lang}_{typeof(TModel).Name.ToLower()}_list_{key}_{request.Status}_{request.Keyword}_{request.OrderBy}_{request.Direction}_{request.PageSize}_{request.PageIndex}_{request.Query}";
            RepositoryResponse <PaginationModel <TView> > data = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                data = await MixCacheService.GetAsync <RepositoryResponse <PaginationModel <TView> > >(cacheKey);
            }

            if (data == null)
            {
                if (predicate != null)
                {
                    data = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetModelListByAsync(
                        predicate, request.OrderBy, request.Direction, request.PageSize, request.PageIndex, null, null);

                    if (data.IsSucceed)
                    {
                        await MixCacheService.SetAsync(cacheKey, data);

                        AlertAsync("Add Cache", 200, cacheKey);
                    }
                }
                else
                {
                    data = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetModelListAsync(request.OrderBy, request.Direction, request.PageSize, request.PageIndex, null, null).ConfigureAwait(false);

                    if (data.IsSucceed)
                    {
                        await MixCacheService.SetAsync(cacheKey, data);

                        AlertAsync("Add Cache", 200, cacheKey);
                    }
                }
            }
            data.LastUpdateConfiguration = MixService.GetConfig <DateTime?>("LastUpdateConfiguration");
            return(data);
        }
コード例 #27
0
        public RepositoryResponse <JObject> SaveAppSettings([FromBody] JObject model)
        {
            var settings = FileRepository.Instance.GetFile("appsettings", ".json", string.Empty, true, "{}");

            if (model != null)
            {
                settings.Content = model.ToString();
                if (FileRepository.Instance.SaveFile(settings))
                {
                    MixService.Reload();
                    if (!MixService.GetConfig <bool>("IsCache"))
                    {
                        MixCacheService.RemoveCacheAsync();
                    }
                }
                MixService.SetConfig("LastUpdateConfiguration", DateTime.UtcNow);
            }
            return(new RepositoryResponse <JObject>()
            {
                IsSucceed = model != null, Data = model
            });
        }
コード例 #28
0
        public virtual async Task <ActionResult <TUpdate> > Duplicate(string id)
        {
            var getData = await GetSingleAsync(id);

            if (getData.IsSucceed)
            {
                var data       = getData.Data;
                var idProperty = ReflectionHelper.GetPropertyType(data.GetType(), MixQueryColumnName.Id);
                switch (idProperty.Name.ToLower())
                {
                case "int32":
                    ReflectionHelper.SetPropertyValue(data, new JProperty("id", 0));
                    break;

                default:
                    ReflectionHelper.SetPropertyValue(data, new JProperty("id", default));
                    break;
                }

                var saveResult = await data.SaveModelAsync(true);

                if (saveResult.IsSucceed)
                {
                    string key = $"_{id}";
                    key += !string.IsNullOrEmpty(_lang) ? $"_{_lang}" : string.Empty;
                    await MixCacheService.RemoveCacheAsync(typeof(TModel), key);

                    return(Ok(saveResult.Data));
                }
                else
                {
                    return(BadRequest(saveResult.Errors));
                }
            }
            else
            {
                return(NoContent());
            }
        }
コード例 #29
0
        public virtual async Task <ActionResult> ClearCacheAsync()
        {
            await MixCacheService.RemoveCacheAsync(typeof(TModel));

            return(NoContent());
        }
コード例 #30
0
        protected async Task <List <TView> > GetListAsync <TView>(Expression <Func <TModel, bool> > predicate, string key, ODataQueryOptions <TModel> queryOptions)
            where TView : ViewModelBase <TDbContext, TModel, TView>
        {
            if (queryOptions.Filter != null)
            {
                ODataHelper <TModel> .ParseFilter(queryOptions.Filter.FilterClause.Expression, ref predicate);
            }
            int?          top     = queryOptions.Top?.Value;
            var           skip    = queryOptions.Skip?.Value ?? 0;
            RequestPaging request = new RequestPaging()
            {
                PageIndex = 0,
                PageSize  = top.HasValue ? top + top * (skip / top + 1) : null,
                OrderBy   = queryOptions.OrderBy?.RawValue
                            //Top = queryOptions.Top?.Value,
                            //Skip = queryOptions.Skip?.Value
            };
            var          cacheKey = $"odata_{_lang}_{typeof(TView).FullName}_{key}_{SeoHelper.GetSEOString(queryOptions.Filter?.RawValue, '_')}_ps-{request.PageSize}";
            List <TView> data     = null;

            if (MixService.GetConfig <bool>("IsCache"))
            {
                var getData = await MixCacheService.GetAsync <RepositoryResponse <PaginationModel <TView> > >(cacheKey);

                if (getData != null)
                {
                    data = getData.Data.Items;
                }
            }

            if (data == null)
            {
                if (predicate != null)
                {
                    var getData = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetModelListByAsync(predicate,
                                                                                                                    request.OrderBy, request.Direction, request.PageSize, request.PageIndex, request.Skip, request.Top).ConfigureAwait(false);

                    if (getData.IsSucceed)
                    {
                        await MixCacheService.SetAsync(cacheKey, getData);

                        data = getData.Data.Items;
                    }
                }
                else
                {
                    var getData = await DefaultRepository <TDbContext, TModel, TView> .Instance.GetModelListAsync(
                        request.OrderBy, request.Direction, request.PageSize, request.PageIndex
                        , null, null).ConfigureAwait(false);

                    if (getData.IsSucceed)
                    {
                        await MixCacheService.SetAsync(cacheKey, getData);

                        data = getData.Data.Items;
                    }
                }
            }

            return(data);
        }