Ejemplo n.º 1
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();
            }
        }