This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Module.cs
110 lines (97 loc) · 5.79 KB
/
Module.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using VirtoCommerce.CoreModule.Core;
using VirtoCommerce.CoreModule.Core.Common;
using VirtoCommerce.CoreModule.Core.Conditions;
using VirtoCommerce.CoreModule.Core.Conditions.Browse;
using VirtoCommerce.CoreModule.Core.Conditions.GeoConditions;
using VirtoCommerce.CoreModule.Core.Currency;
using VirtoCommerce.CoreModule.Core.Package;
using VirtoCommerce.CoreModule.Core.Seo;
using VirtoCommerce.CoreModule.Data.Currency;
using VirtoCommerce.CoreModule.Data.Package;
using VirtoCommerce.CoreModule.Data.Repositories;
using VirtoCommerce.CoreModule.Data.Seo;
using VirtoCommerce.CoreModule.Data.Services;
using VirtoCommerce.CoreModule.Web.ExportImport;
using VirtoCommerce.CoreModule.Web.JsonConverters;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.ExportImport;
using VirtoCommerce.Platform.Core.Modularity;
using VirtoCommerce.Platform.Core.Security;
using VirtoCommerce.Platform.Core.Settings;
using VirtoCommerce.Platform.Data.Extensions;
namespace VirtoCommerce.CoreModule.Web
{
public class Module : IModule, IExportSupport, IImportSupport
{
public ManifestModuleInfo ModuleInfo { get; set; }
private IApplicationBuilder _appBuilder;
public void Initialize(IServiceCollection serviceCollection)
{
var configuration = serviceCollection.BuildServiceProvider().GetRequiredService<IConfiguration>();
serviceCollection.AddTransient<ICoreRepository, CoreRepositoryImpl>();
var connectionString = configuration.GetConnectionString("VirtoCommerce.Core") ?? configuration.GetConnectionString("VirtoCommerce");
serviceCollection.AddDbContext<CoreDbContext>(options => options.UseSqlServer(connectionString));
serviceCollection.AddSingleton<Func<ICoreRepository>>(provider => () => provider.CreateScope().ServiceProvider.GetRequiredService<ICoreRepository>());
serviceCollection.AddSingleton<ICurrencyService, CurrencyService>();
serviceCollection.AddSingleton<IPackageTypesService, PackageTypesService>();
//Can be overrided
serviceCollection.AddSingleton<ISeoDuplicatesDetector, NullSeoDuplicateDetector>();
serviceCollection.AddSingleton<CoreExportImport>();
serviceCollection.AddSingleton<IUniqueNumberGenerator, SequenceUniqueNumberGeneratorService>();
serviceCollection.AddSingleton<CompositeSeoBySlugResolver>();
}
public void PostInitialize(IApplicationBuilder appBuilder)
{
_appBuilder = appBuilder;
var settingsRegistrar = appBuilder.ApplicationServices.GetRequiredService<ISettingsRegistrar>();
settingsRegistrar.RegisterSettings(ModuleConstants.Settings.AllSettings, ModuleInfo.Id);
var permissionsProvider = appBuilder.ApplicationServices.GetRequiredService<IPermissionsRegistrar>();
permissionsProvider.RegisterPermissions(ModuleConstants.Security.Permissions.AllPermissions.Select(x => new Permission() { GroupName = "Core", Name = x }).ToArray());
var mvcJsonOptions = appBuilder.ApplicationServices.GetService<IOptions<MvcJsonOptions>>();
mvcJsonOptions.Value.SerializerSettings.Converters.Add(new PolymorphicJsonConverter());
mvcJsonOptions.Value.SerializerSettings.Converters.Add(new ConditionJsonConverter());
AbstractTypeFactory<IConditionTree>.RegisterType<BlockConditionAndOr>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionAgeIs>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGenderIs>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionLanguageIs>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionStoreSearchedPhrase>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionUrlIs>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGeoCity>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGeoCountry>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGeoState>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGeoTimeZone>();
AbstractTypeFactory<IConditionTree>.RegisterType<ConditionGeoZipCode>();
AbstractTypeFactory<IConditionTree>.RegisterType<UserGroupsContainsCondition>();
using (var serviceScope = appBuilder.ApplicationServices.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<CoreDbContext>();
dbContext.Database.MigrateIfNotApplied(MigrationName.GetUpdateV2MigrationName(ModuleInfo.Id));
dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
}
}
public void Uninstall()
{
}
public async Task ExportAsync(Stream outStream, ExportImportOptions options, Action<ExportImportProgressInfo> progressCallback,
ICancellationToken cancellationToken)
{
await _appBuilder.ApplicationServices.GetRequiredService<CoreExportImport>().ExportAsync(outStream, options, progressCallback, cancellationToken);
}
public async Task ImportAsync(Stream inputStream, ExportImportOptions options, Action<ExportImportProgressInfo> progressCallback,
ICancellationToken cancellationToken)
{
await _appBuilder.ApplicationServices.GetRequiredService<CoreExportImport>().ImportAsync(inputStream, options, progressCallback, cancellationToken);
}
}
}