Пример #1
0
        /// <summary>
        /// Try to install the blog
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult TryInstall(InstallModel model)
        {
            if (!RequestLocalizationOptions.Value.SupportedCultures.Any(t => t.Name.Equals(model.Language)))
            {
                return(OperationResult.Failure(InstallLocalizer["Not supported language"]));
            }

            lock (_sync)
            {
                if (!NeedToInstall())
                {
                    return(OperationResult.Failure(InstallLocalizer["Blog has been already installed"]));
                }
                else
                {
                    //1. Add admin user
                    AddAdminUser(model);

                    //2. Setup default settings
                    AddSettings(model);

                    //3. Setup widgets
                    AddWidgets(model);

                    this.BlogContext.SaveChanges();

                    //Clear settings cache
                    SettingService settingService = ServiceProvider.GetService <SettingService>();
                    settingService.RemoveCache();

                    //Clear widgets cache
                    WidgetService widgetService = ServiceProvider.GetService <WidgetService>();
                    widgetService.RemoveCache();

                    return(new OperationResult());
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Try to install the blog
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <OperationResult> TryInstallAsync(InstallModel model)
        {
            await _lock.WaitAsync();

            try
            {
                if (!RequestLocalizationOptions.Value.SupportedCultures.Any(t => t.Name.Equals(model.Language)))
                {
                    return(OperationResult.Failure(InstallLocalizer["Not supported language"]));
                }

                if (!NeedToInstall())
                {
                    return(OperationResult.Failure(InstallLocalizer["Blog has been already installed"]));
                }
                else
                {
                    using var transaction = BlogContext.Database.BeginTransaction();

                    //1. Add admin user
                    var result = await AddAdminUserAsync(model);

                    if (!result.Success)
                    {
                        return(result);
                    }

                    //2. Setup default settings
                    result = await AddSettingsAsync(model);

                    if (!result.Success)
                    {
                        return(result);
                    }

                    //3. Setup widgets
                    result = await AddWidgetsAsync(model);

                    if (!result.Success)
                    {
                        return(result);
                    }

                    await BlogContext.SaveChangesAsync();

                    //Clear settings cache
                    SettingService settingService = ServiceProvider.GetService <SettingService>();
                    settingService.RemoveCache();

                    //Clear widgets cache
                    WidgetService widgetService = ServiceProvider.GetService <WidgetService>();
                    widgetService.RemoveCache();

                    //4. Add topic
                    result = await AddSampleTopicAsync(model);

                    if (!result.Success)
                    {
                        return(result);
                    }

                    _cacheOfNeedToInstall = false;

                    await transaction.CommitAsync();

                    return(new OperationResult());
                }
            }
            finally
            {
                _lock.Release();
            }
        }