Пример #1
0
        /// <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)
                        {
                            InitPages(culture.Specificulture, context, transaction);
                            isSucceed = (await context.SaveChangesAsync().ConfigureAwait(false)) > 0;
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Pages");
                        }

                        /**
                         * Init System Positions
                         */
                        if (isSucceed)
                        {
                            isSucceed = await InitPositionsAsync(context, transaction);
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Positions");
                        }

                        /**
                         * Init System Configurations
                         */
                        if (isSucceed)
                        {
                            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)
                        {
                            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();
            }
        }