protected bool InitCultures(InitCulture culture, SioCmsContext context, IDbContextTransaction transaction) { bool isSucceed = true; try { if (context.SioCulture.Count() == 0) { // EN-US var enCulture = new SioCulture() { Specificulture = culture.Specificulture, FullName = culture.FullName, Description = culture.Description, Icon = culture.Icon, Alias = culture.Alias, Status = (int)SioEnums.SioContentStatus.Published }; context.Entry(enCulture).State = EntityState.Added; context.SaveChanges(); } } catch { isSucceed = false; } return(isSucceed); }
public List <SupportedCulture> LoadCultures(string initCulture = null, SioCmsContext _context = null, IDbContextTransaction _transaction = null) { var getCultures = SystemCultureViewModel.Repository.GetModelList(_context, _transaction); var result = new List <SupportedCulture>(); if (getCultures.IsSucceed) { foreach (var culture in getCultures.Data) { result.Add( new SupportedCulture() { Icon = culture.Icon, Specificulture = culture.Specificulture, Alias = culture.Alias, FullName = culture.FullName, Description = culture.FullName, Id = culture.Id, Lcid = culture.Lcid, IsSupported = culture.Specificulture == initCulture }); } } return(result); }
public static async Task <RepositoryResponse <bool> > ImportAsync(List <SioPages.ImportViewModel> arrPage, string destCulture, SioCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; bool isRoot = _context == null; var context = _context ?? new SioCmsContext(); var transaction = _transaction ?? context.Database.BeginTransaction(); try { int id = UpdateViewModel.ModelRepository.Max(m => m.Id, context, transaction).Data + 1; var pages = FileRepository.Instance.GetFile(SioConstants.CONST_FILE_PAGES, "data", true, "{}"); var obj = JObject.Parse(pages.Content); var initPages = obj["data"].ToObject <JArray>(); foreach (var item in arrPage) { if (item.Id > initPages.Count) { item.Id = id; item.CreatedDateTime = DateTime.UtcNow; } item.Specificulture = destCulture; var saveResult = await item.SaveModelAsync(true, context, transaction); if (!saveResult.IsSucceed) { result.IsSucceed = false; result.Exception = saveResult.Exception; result.Errors = saveResult.Errors; break; } else if (item.Id > initPages.Count) { id++; } } result.Data = true; UnitOfWorkHelper <SioCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <SioCmsContext> .HandleException <ReadMvcViewModel>(ex, isRoot, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { //if current Context is Root if (isRoot) { context?.Dispose(); } } return(result); }
protected bool InitPositions(SioCmsContext context, IDbContextTransaction transaction) { bool isSucceed = true; var count = context.SioPortalPage.Count(); if (count == 0) { var p = new SioPosition() { Description = nameof(SioEnums.CatePosition.Nav) }; context.Entry(p).State = EntityState.Added; p = new SioPosition() { Description = nameof(SioEnums.CatePosition.Top) }; context.Entry(p).State = EntityState.Added; p = new SioPosition() { Description = nameof(SioEnums.CatePosition.Left) }; context.Entry(p).State = EntityState.Added; p = new SioPosition() { Description = nameof(SioEnums.CatePosition.Footer) }; context.Entry(p).State = EntityState.Added; context.SaveChanges(); } return(isSucceed); }
List <SioPortalPageRoles.ReadViewModel> GetPermission() { using (SioCmsContext context = new SioCmsContext()) { var transaction = context.Database.BeginTransaction(); var query = context.SioPortalPage .Include(cp => cp.SioPortalPageRole) .Select(Category => new SioPortalPageRoles.ReadViewModel( new SioPortalPageRole() { RoleId = Id, PageId = Category.Id, }, context, transaction)); var result = query.ToList(); result.ForEach(nav => { nav.IsActived = context.SioPortalPageRole.Any( m => m.PageId == nav.PageId && m.RoleId == Id); }); transaction.Commit(); return(result.OrderBy(m => m.Priority).ToList()); } }
protected void InitPages(string culture, SioCmsContext context, IDbContextTransaction transaction) { /* Init Languages */ var pages = FileRepository.Instance.GetFile(SioConstants.CONST_FILE_PAGES, "data", true, "{}"); var obj = JObject.Parse(pages.Content); var arrPage = obj["data"].ToObject <List <SioPage> >(); foreach (var page in arrPage) { page.Specificulture = culture; page.SeoTitle = page.Title.ToLower(); page.SeoName = SeoHelper.GetSEOString(page.Title); page.SeoDescription = page.Title.ToLower(); page.SeoKeywords = page.Title.ToLower(); page.CreatedDateTime = DateTime.UtcNow; page.CreatedBy = "SuperAdmin"; context.Entry(page).State = EntityState.Added; var alias = new SioUrlAlias() { Id = page.Id, SourceId = page.Id.ToString(), Type = (int)UrlAliasType.Page, Specificulture = culture, CreatedDateTime = DateTime.UtcNow, Alias = page.Title.ToLower(), Status = (int)SioContentStatus.Published }; context.Entry(alias).State = EntityState.Added; } }
public DashboardViewModel() { using (SioCmsContext context = new SioCmsContext()) { TotalPage = context.SioPage.Count(); TotalArticle = context.SioArticle.Count(); TotalProduct = context.SioProduct.Count(); } }
private async Task <bool> InitLanguagesAsync(InitCulture culture, SioCmsContext context, IDbContextTransaction transaction) { /* Init Languages */ var configurations = FileRepository.Instance.GetFile(SioConstants.CONST_FILE_LANGUAGES, "data", true, "{}"); var obj = JObject.Parse(configurations.Content); var arrLanguage = obj["data"].ToObject <List <SioLanguage> >(); var result = await ViewModels.SioLanguages.ReadMvcViewModel.ImportLanguages(arrLanguage, culture.Specificulture, context, transaction); return(result.IsSucceed); }
public static RepositoryResponse <UpdateViewModel> GetBy( Expression <Func <SioModule, bool> > predicate, string articleId = null, string productId = null, int categoryId = 0 , SioCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = UpdateViewModel.Repository.GetSingleModel(predicate, _context, _transaction); if (result.IsSucceed) { result.Data.ArticleId = articleId; result.Data.CategoryId = categoryId; } return(result); }
public static void LoadFromDatabase(SioCmsContext _context = null, IDbContextTransaction _transaction = null) { UnitOfWorkHelper<SioCmsContext>.InitTransaction(_context, _transaction , out SioCmsContext context, out IDbContextTransaction transaction, out bool isRoot); try { Instance.Translator = new JObject(); var ListLanguage = context.SioLanguage; foreach (var culture in context.SioCulture) { JObject arr = new JObject(); foreach (var lang in ListLanguage.Where(l => l.Specificulture == culture.Specificulture)) { JProperty l = new JProperty(lang.Keyword, lang.Value ?? lang.DefaultValue); arr.Add(l); } Instance.Translator.Add(new JProperty(culture.Specificulture, arr)); } Instance.LocalSettings = new JObject(); var listLocalSettings = context.SioConfiguration; foreach (var culture in context.SioCulture) { JObject arr = new JObject(); foreach (var cnf in listLocalSettings.Where(l => l.Specificulture == culture.Specificulture)) { JProperty l = new JProperty(cnf.Keyword, cnf.Value); arr.Add(l); } Instance.LocalSettings.Add(new JProperty(culture.Specificulture, arr)); } UnitOfWorkHelper<SioCmsContext>.HandleTransaction(true, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper<SioCmsContext>.HandleException<SioLanguage>(ex, isRoot, transaction); } finally { //if current Context is Root if (isRoot) { context?.Dispose(); } } }
public override async Task <RepositoryResponse <bool> > SaveSubModelsAsync(AspNetRoles parent, SioCmsAccountContext _context, IDbContextTransaction _transaction) { SioCmsContext context = new SioCmsContext(); var result = new RepositoryResponse <bool>() { IsSucceed = true }; var transaction = context.Database.BeginTransaction(); try { foreach (var item in Permissions) { if (result.IsSucceed) { result = await HandlePermission(item, context, transaction); } else { break; } } if (result.IsSucceed) { transaction.Commit(); } else { transaction.Rollback(); } return(result); } catch (Exception ex) { transaction.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { transaction.Dispose(); context.Dispose(); } }
public static async Task <RepositoryResponse <bool> > Import(List <SioModule> arrModule, string destCulture, SioCmsContext _context = null, IDbContextTransaction _transaction = null) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; bool isRoot = _context == null; var context = _context ?? new SioCmsContext(); var transaction = _transaction ?? context.Database.BeginTransaction(); try { int id = UpdateViewModel.ModelRepository.Max(m => m.Id, context, transaction).Data + 1; foreach (var item in arrModule) { item.Id = id; item.CreatedDateTime = DateTime.UtcNow; item.Specificulture = destCulture; context.SioModule.Add(item); id++; } await context.SaveChangesAsync(); result.Data = true; UnitOfWorkHelper <SioCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { var error = UnitOfWorkHelper <SioCmsContext> .HandleException <ReadMvcViewModel>(ex, isRoot, transaction); result.IsSucceed = false; result.Errors = error.Errors; result.Exception = error.Exception; } finally { //if current Context is Root if (isRoot) { context?.Dispose(); } } return(result); }
private bool InitThemes(SioCmsContext context, IDbContextTransaction transaction) { bool isSucceed = true; var getThemes = ViewModels.SioThemes.UpdateViewModel.Repository.GetModelList(_context: context, _transaction: transaction); if (!context.SioTheme.Any()) { ViewModels.SioThemes.UpdateViewModel theme = new ViewModels.SioThemes.UpdateViewModel(new SioTheme() { Name = "Default", CreatedBy = "Admin", Status = (int)SioContentStatus.Published }, context, transaction); isSucceed = isSucceed && theme.SaveModel(true, context, transaction).IsSucceed; } return(isSucceed); }
public RepositoryResponse <bool> Validate <T>(IConvertible id, string specificulture, JObject jItem, SioCmsContext _context = null, IDbContextTransaction _transaction = null) where T : class { string val = jItem[Name]["value"].Value <string>(); var jVal = new JProperty(Name, jItem[Name]); var result = new RepositoryResponse <bool>() { IsSucceed = true }; if (IsUnique) { //string query = @"SELECT * FROM [Sio_module_data] WHERE JSON_VALUE([Value],'$.{0}.value') = '{1}'"; // AND Specificulture = '{2}' AND Id <> '{3}' //var temp = string.Format(query, Name, val);//, specificulture, id?.ToString() //int count = _context.SioModuleData.FromSql(query, Name, val).Count(d=>d.Specificulture == specificulture && d.Id != id.ToString());//, specificulture, id?.ToString() //string query = $"SELECT * FROM Sio_module_data WHERE JSON_VALUE([Value],'$.{Name}.value') = '{val}' AND Specificulture = '{specificulture}' AND Id != '{id}'"; //int count = _context.SioModuleData.FromSql(sql: new RawSqlString(query)).Count(); var strId = id?.ToString(); int count = _context.SioModuleData.Count(d => d.Specificulture == specificulture && d.Value.Contains(jVal.ToString(Formatting.None)) && d.Id != strId); if (count > 0) { result.IsSucceed = false; result.Errors.Add($"{Title} is existed"); } } if (IsRequired) { if (string.IsNullOrEmpty(val)) { result.IsSucceed = false; result.Errors.Add($"{Title} is required"); } } return(result); }
private async Task <RepositoryResponse <ViewModels.SioThemes.InitViewModel> > InitThemesAsync(string siteName, SioCmsContext context, IDbContextTransaction transaction) { var getThemes = ViewModels.SioThemes.InitViewModel.Repository.GetModelList(_context: context, _transaction: transaction); if (!context.SioTheme.Any()) { ViewModels.SioThemes.InitViewModel theme = new ViewModels.SioThemes.InitViewModel(new SioTheme() { Id = 1, Title = siteName, Name = SeoHelper.GetSEOString(siteName), CreatedDateTime = DateTime.UtcNow, CreatedBy = "Admin", Status = (int)SioContentStatus.Published, }, context, transaction); return(await theme.SaveModelAsync(true, context, transaction)); } return(new RepositoryResponse <ViewModels.SioThemes.InitViewModel>() { IsSucceed = true }); }
private async Task <bool> InitConfigurationsAsync(string siteName, InitCulture culture, SioCmsContext context, IDbContextTransaction transaction) { /* Init Configs */ var configurations = FileRepository.Instance.GetFile(SioConstants.CONST_FILE_CONFIGURATIONS, "data", true, "{}"); var obj = JObject.Parse(configurations.Content); var arrConfiguration = obj["data"].ToObject <List <SioConfiguration> >(); if (!string.IsNullOrEmpty(siteName)) { arrConfiguration.Find(c => c.Keyword == "SiteName").Value = siteName; } var result = await ViewModels.SioConfigurations.ReadMvcViewModel.ImportConfigurations(arrConfiguration, culture.Specificulture, context, transaction); return(result.IsSucceed); }
public async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); SioCmsContext context = null; SioCmsAccountContext accountContext = null; SioChatServiceContext messengerContext; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION))) { context = new SioCmsContext(); accountContext = new SioCmsAccountContext(); messengerContext = new SioChatServiceContext(); //SioChatServiceContext._cnn = SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); await messengerContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.SioCulture.Count(); var isInit = countCulture > 0; if (!isInit) { SioService.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.SioPage.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(); } }
async Task <RepositoryResponse <bool> > HandlePermission(SioPortalPages.UpdateRolePermissionViewModel item, SioCmsContext context, IDbContextTransaction transaction) { var result = new RepositoryResponse <bool>() { IsSucceed = true }; if (item.NavPermission.IsActived) { item.NavPermission.CreatedBy = item.CreatedBy; var saveResult = await item.NavPermission.SaveModelAsync(false, context, transaction); result.IsSucceed = saveResult.IsSucceed; /* skip child nav * if (result.IsSucceed) * { * foreach (var child in item.ChildPages) * { * result = await HandlePermission(child.Page, context, transaction); * if (!result.IsSucceed) * { * break; * } * } * }*/ if (!result.IsSucceed) { result.Exception = saveResult.Exception; Errors.AddRange(saveResult.Errors); } } else { var saveResult = await item.NavPermission.RemoveModelAsync(false, context, transaction); /* skip child nav */ result.IsSucceed = saveResult.IsSucceed; if (result.IsSucceed) { foreach (var child in item.ChildPages) { child.Page.NavPermission.IsActived = false; result = await HandlePermission(child.Page, context, transaction); } } if (!result.IsSucceed) { result.Exception = saveResult.Exception; Errors.AddRange(saveResult.Errors); } } return(result); }
public async Task <RepositoryResponse <bool> > InitCms(InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); SioCmsContext context = null; SioCmsAccountContext accountContext = null; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION))) { context = new SioCmsContext(); accountContext = new SioCmsAccountContext(); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.SioCulture.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.SioPage.Count() == 0) { var cate = new SioPage() { Id = 1, Level = 0, Title = "Home", Specificulture = culture.Specificulture, Template = "Pages/_Home.cshtml", Type = (int)SioPageType.Home, CreatedBy = "Admin", CreatedDateTime = DateTime.UtcNow, Status = (int)PageStatus.Published }; context.Entry(cate).State = EntityState.Added; var alias = new SioUrlAlias() { 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 SioPage() { Id = 2, Title = "404", Level = 0, Specificulture = culture.Specificulture, Template = "Pages/_404.cshtml", Type = (int)SioPageType.Article, CreatedBy = "Admin", CreatedDateTime = DateTime.UtcNow, Status = (int)PageStatus.Published }; var alias404 = new SioUrlAlias() { 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(); } }