public static RepositoryResponse <List <MixPagePosts.ReadViewModel> > GetNavAsync(int postId, string specificulture , MixCmsContext _context = null, IDbContextTransaction _transaction = null) { MixCmsContext context = _context ?? new MixCmsContext(); var transaction = _transaction ?? context.Database.BeginTransaction(); try { var navCategoryPostViewModels = context.MixPage.Include(cp => cp.MixPagePost).Where(a => a.Specificulture == specificulture && (a.Type == MixEnums.MixPageType.ListPost.ToString()) ) .AsEnumerable() .Select(p => new MixPagePosts.ReadViewModel( new MixPagePost() { PostId = postId, PageId = p.Id, Specificulture = specificulture }, _context, _transaction) { IsActived = p.MixPagePost.Any(cp => cp.PostId == postId && cp.Specificulture == specificulture), Description = p.Title }); return(new RepositoryResponse <List <ReadViewModel> >() { IsSucceed = true, Data = navCategoryPostViewModels.ToList() }); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { if (_transaction == null) { transaction.Rollback(); } return(new RepositoryResponse <List <MixPagePosts.ReadViewModel> >() { IsSucceed = true, Data = null, Exception = ex }); } finally { if (_context == null) { //if current Context is Root transaction.Dispose(); context.Database.CloseConnection(); transaction.Dispose(); context.Dispose(); } } }
public override async Task <RepositoryResponse <bool> > SaveSubModelsAsync(AspNetRoles parent, MixCmsAccountContext _context, IDbContextTransaction _transaction) { MixCmsContext context = new MixCmsContext(); 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(); } }
/// <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 = null; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = MixService.GetDbContext(); accountContext = new MixCmsAccountContext(); messengerContext = new MixChatServiceContext(); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); await messengerContext.Database.MigrateAsync(); var countCulture = context.MixCulture.Count(); var pendingMigration = context.Database.GetPendingMigrations().Count(); if (pendingMigration == 0) { return(await InitSiteData(siteName, culture)); } } return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); messengerContext?.Dispose(); } }
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(); } }
/// <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 = GetDbContext(); 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?.Database.CloseConnection(); context?.Dispose(); accountContext?.Database.CloseConnection(); accountContext?.Dispose(); } }