/// <summary> /// Step 1 /// - Init Culture /// - Init System pages /// </summary> /// <param name="siteName"></param> /// <param name="culture"></param> /// <returns></returns> public static async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; MixCmsAccountContext accountContext = null; MixChatServiceContext messengerContext; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = new MixCmsContext(); accountContext = new MixCmsAccountContext(); messengerContext = new MixChatServiceContext(); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); await messengerContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.MixCulture.Count(); var isInit = MixService.GetConfig <bool>("IsInit"); if (isInit) { /** * Init Selected Language as default */ isSucceed = InitCultures(culture, context, transaction); /** * Init System Pages */ //if (isSucceed && context.MixPage.Count() == 0) //{ // InitPages(culture.Specificulture, context, transaction); // isSucceed = (await context.SaveChangesAsync().ConfigureAwait(false)) > 0; //} //else //{ // result.Errors.Add("Cannot init Pages"); //} ///** // * Init System Positions // */ //if (isSucceed && context.MixPosition.Count() == 0) //{ // isSucceed = await InitPositionsAsync(context, transaction); //} //else //{ // result.Errors.Add("Cannot init Positions"); //} /** * Init System Configurations */ if (isSucceed && context.MixConfiguration.Count() == 0) { var saveResult = await InitConfigurationsAsync(siteName, culture.Specificulture, context, transaction); isSucceed = saveResult.IsSucceed; } else { result.Errors.Add("Cannot init Configurations"); } ///** //* Init System Attribute Sets //*/ //if (isSucceed && context.MixAttributeField.Count() == 0) //{ // var saveResult = await InitAttributeSetsAsync(siteName, culture.Specificulture, context, transaction); // isSucceed = saveResult.IsSucceed; //} //else //{ // result.Errors.Add("Cannot init Attribute Sets"); //} } if (isSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } result.IsSucceed = isSucceed; return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); accTransaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); } }
public override ValueTask <RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values) { if (MixService.GetAppSetting <bool>(MixAppSettingKeywords.IsInit)) { return(ValueTask.FromResult(values)); } RouteValueDictionary result = values; var keys = values.Keys.ToList(); var language = (string)values[keys[0]]; var keyIndex = 1; if (!MixService.Instance.CheckValidCulture(language)) { language = MixService.GetAppSetting <string>(MixAppSettingKeywords.DefaultCulture); keyIndex -= 1; } var path = string.Join('/', values.Values.Skip(keyIndex)); if (MixService.Instance.CheckValidAlias(language, path)) { result["controller"] = "home"; result["culture"] = language; result["action"] = "Index"; result["seoName"] = path; return(ValueTask.FromResult(result)); } string notTransformPattern = @"^(.*)\.(xml|json|html|css|js|map|jpg|png|gif|jpeg|svg|map|ico|webmanifest|woff|woff2|ttf|eot)$"; Regex reg = new Regex(notTransformPattern); if (reg.IsMatch(httpContext.Request.Path.Value)) { return(ValueTask.FromResult(values)); } var currentController = GetRouteValue(values, keys, ref keyIndex); var controller = MixService.Translate(currentController, language, currentController); if (!IsValidController(controller)) { controller = "home"; keyIndex -= 1; result["keyword"] = keyIndex < keys.Count ? string.Join('/', values.Values.Skip(keyIndex + 1)) : string.Empty; result["seoName"] = keys.Count > keyIndex ? (string)values[keys[keyIndex]] : string.Empty; result["culture"] = language; result["action"] = "Index"; result["controller"] = controller; } else { if (keys.Count > 2) { result["id"] = GetRouteValue(values, keys, ref keyIndex); } result["controller"] = controller; result["keyword"] = GetRouteValue(values, keys, ref keyIndex); } result["action"] = "Index"; return(ValueTask.FromResult(result)); }
public static async Task <RepositoryResponse <bool> > InitSiteData(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; IDbContextTransaction transaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = MixService.GetDbContext(); transaction = context.Database.BeginTransaction(); var countCulture = context.MixCulture.Count(); /** * Init Selected Language as default */ isSucceed = InitCultures(culture, context, transaction); /** * Init System Configurations */ if (isSucceed && context.MixConfiguration.Count() == 0) { var saveResult = await InitConfigurationsAsync(siteName, culture.Specificulture, context, transaction); result.IsSucceed = saveResult.IsSucceed; result.Errors = saveResult.Errors; result.Exception = saveResult.Exception; } else { result.IsSucceed = false; result.Errors.Add("Cannot init cultures"); } if (result.IsSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Database.CloseConnection(); context?.Dispose(); } }
public async Task <RepositoryResponse <bool> > InitCms(InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; MixCmsAccountContext accountContext = null; //MixChatServiceContext messengerContext; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = new MixCmsContext(); accountContext = new MixCmsAccountContext(); //messengerContext = new MixChatServiceContext(); //MixChatServiceContext._cnn = MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); //await messengerContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.MixCulture.Count(); var isInit = countCulture > 0; if (!isInit) { isSucceed = InitCultures(culture, context, transaction); isSucceed = isSucceed && InitPositions(context, transaction); isSucceed = isSucceed && InitThemes(context, transaction); isSucceed = isSucceed && await InitConfigurationsAsync(culture, context, transaction); isSucceed = isSucceed && await InitLanguagesAsync(culture, context, transaction); } else { isSucceed = true; } if (isSucceed && context.MixPage.Count() == 0) { var cate = new MixPage() { Id = 1, Level = 0, Title = "Home", Specificulture = culture.Specificulture, Template = "Pages/_Home.cshtml", Type = (int)MixPageType.Home, CreatedBy = "Admin", CreatedDateTime = DateTime.UtcNow, Status = (int)PageStatus.Published }; context.Entry(cate).State = EntityState.Added; var alias = new MixUrlAlias() { Id = 1, SourceId = "1", Type = (int)UrlAliasType.Page, Specificulture = culture.Specificulture, CreatedDateTime = DateTime.UtcNow, Alias = cate.Title.ToLower() }; context.Entry(alias).State = EntityState.Added; var createVNHome = await context.SaveChangesAsync().ConfigureAwait(false); isSucceed = createVNHome > 0; var cate404 = new MixPage() { Id = 2, Title = "404", Level = 0, Specificulture = culture.Specificulture, Template = "Pages/_404.cshtml", Type = (int)MixPageType.Article, CreatedBy = "Admin", CreatedDateTime = DateTime.UtcNow, Status = (int)PageStatus.Published }; var alias404 = new MixUrlAlias() { Id = 2, SourceId = "2", Type = (int)UrlAliasType.Page, Specificulture = culture.Specificulture, CreatedDateTime = DateTime.UtcNow, Alias = cate404.Title.ToLower() }; context.Entry(cate404).State = EntityState.Added; context.Entry(alias404).State = EntityState.Added; var create404 = await context.SaveChangesAsync().ConfigureAwait(false); isSucceed = create404 > 0; } if (isSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } result.IsSucceed = isSucceed; return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); accTransaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); } }
public async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; MixCmsAccountContext accountContext = null; MixChatServiceContext messengerContext; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = new MixCmsContext(); accountContext = new MixCmsAccountContext(); messengerContext = new MixChatServiceContext(); //MixChatServiceContext._cnn = MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); await messengerContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.MixCulture.Count(); var isInit = countCulture > 0; if (!isInit) { MixService.SetConfig <string>("SiteName", siteName); isSucceed = InitCultures(culture, context, transaction); if (isSucceed) { isSucceed = isSucceed && InitPositions(context, transaction); } else { result.Errors.Add("Cannot init Cultures"); } if (isSucceed) { isSucceed = isSucceed && await InitConfigurationsAsync(siteName, culture, context, transaction); } else { result.Errors.Add("Cannot init Positions"); } if (isSucceed) { isSucceed = isSucceed && await InitLanguagesAsync(culture, context, transaction); } else { result.Errors.Add("Cannot init Configurations"); } if (isSucceed) { var initTheme = await InitThemesAsync(siteName, context, transaction); isSucceed = isSucceed && initTheme.IsSucceed; result.Errors.AddRange(initTheme.Errors); result.Exception = initTheme.Exception; } else { result.Errors.Add("Cannot init Languages"); } } else { isSucceed = true; } if (isSucceed && context.MixPage.Count() == 0) { InitPages(culture.Specificulture, context, transaction); isSucceed = (await context.SaveChangesAsync().ConfigureAwait(false)) > 0; } else { result.Errors.Add("Cannot init Themes"); } if (isSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } result.IsSucceed = isSucceed; return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); accTransaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); } }