Module Creator external template changes for 2.1 - supporting mutliple databases, EF Core migrations, and other multi-tenancy improvements
This commit is contained in:
		| @ -5,27 +5,19 @@ using Microsoft.AspNetCore.Http; | ||||
| using Oqtane.Shared; | ||||
| using Oqtane.Enums; | ||||
| using Oqtane.Infrastructure; | ||||
| using [Owner].[Module].Models; | ||||
| using [Owner].[Module].Repository; | ||||
| using Oqtane.Controllers; | ||||
|  | ||||
| namespace [Owner].[Module].Controllers | ||||
| { | ||||
|     [Route(ControllerRoutes.Default)] | ||||
|     public class [Module]Controller : Controller | ||||
|     [Route(ControllerRoutes.ApiRoute)] | ||||
|     public class [Module]Controller : ModuleControllerBase | ||||
|     { | ||||
|         private readonly I[Module]Repository _[Module]Repository; | ||||
|         private readonly ILogManager _logger; | ||||
|         protected int _entityId = -1; | ||||
|  | ||||
|         public [Module]Controller(I[Module]Repository [Module]Repository, ILogManager logger, IHttpContextAccessor accessor) | ||||
|         public [Module]Controller(I[Module]Repository [Module]Repository, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor) | ||||
|         { | ||||
|             _[Module]Repository = [Module]Repository; | ||||
|             _logger = logger; | ||||
|  | ||||
|             if (accessor.HttpContext.Request.Query.ContainsKey("entityid")) | ||||
|             { | ||||
|                 _entityId = int.Parse(accessor.HttpContext.Request.Query["entityid"]); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // GET: api/<controller>?moduleid=x | ||||
| @ -33,7 +25,14 @@ namespace [Owner].[Module].Controllers | ||||
|         [Authorize(Policy = PolicyNames.ViewModule)] | ||||
|         public IEnumerable<Models.[Module]> Get(string moduleid) | ||||
|         { | ||||
|             return _[Module]Repository.Get[Module]s(int.Parse(moduleid)); | ||||
|             if (int.Parse(moduleid) == _entityId) | ||||
|             { | ||||
|                 return _[Module]Repository.Get[Module]s(int.Parse(moduleid)); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/5 | ||||
|  | ||||
| @ -1,34 +1,38 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text.Json; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Oqtane.Modules; | ||||
| using Oqtane.Models; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Repository; | ||||
| using [Owner].[Module].Models; | ||||
| using Oqtane.Enums; | ||||
| using [Owner].[Module].Repository; | ||||
|  | ||||
| namespace [Owner].[Module].Manager | ||||
| { | ||||
|     public class [Module]Manager : IInstallable, IPortable | ||||
|     public class [Module]Manager : MigratableModuleBase, IInstallable, IPortable | ||||
|     { | ||||
|         private I[Module]Repository _[Module]Repository; | ||||
|         private ISqlRepository _sql; | ||||
|         private readonly ITenantManager _tenantManager; | ||||
|         private readonly IHttpContextAccessor _accessor; | ||||
|  | ||||
|         public [Module]Manager(I[Module]Repository [Module]Repository, ISqlRepository sql) | ||||
|         public [Module]Manager(I[Module]Repository [Module]Repository, ITenantManager tenantManager, IHttpContextAccessor accessor) | ||||
|         { | ||||
|             _[Module]Repository = [Module]Repository; | ||||
|             _sql = sql; | ||||
|             _tenantManager = tenantManager; | ||||
|             _accessor = accessor; | ||||
|         } | ||||
|  | ||||
|         public bool Install(Tenant tenant, string version) | ||||
|         { | ||||
|             return _sql.ExecuteScript(tenant, GetType().Assembly, "[Owner].[Module]." + version + ".sql"); | ||||
|             _tenantManager.SetTenant(tenant.TenantId); | ||||
|             return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Up); | ||||
|         } | ||||
|  | ||||
|         public bool Uninstall(Tenant tenant) | ||||
|         { | ||||
|             return _sql.ExecuteScript(tenant, GetType().Assembly, "[Owner].[Module].Uninstall.sql"); | ||||
|             _tenantManager.SetTenant(tenant.TenantId); | ||||
|             return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Down); | ||||
|         } | ||||
|  | ||||
|         public string ExportModule(Module module) | ||||
|  | ||||
							
								
								
									
										30
									
								
								Oqtane.Server/wwwroot/Modules/Templates/External/Server/Migrations/01000000_InitializeModule.cs
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Oqtane.Server/wwwroot/Modules/Templates/External/Server/Migrations/01000000_InitializeModule.cs
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||||
| using Microsoft.EntityFrameworkCore.Migrations; | ||||
| using Oqtane.Databases.Interfaces; | ||||
| using Oqtane.Migrations; | ||||
| using [Owner].[Module].Migrations.EntityBuilders; | ||||
| using [Owner].[Module].Repository; | ||||
|  | ||||
| namespace [Owner].[Module].Migrations | ||||
| { | ||||
|     [DbContext(typeof([Module]Context))] | ||||
|     [Migration("[Module].01.00.00.00")] | ||||
|     public class InitializeModule : MultiDatabaseMigration | ||||
|     { | ||||
|         public InitializeModule(IDatabase database) : base(database) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         protected override void Up(MigrationBuilder migrationBuilder) | ||||
|         { | ||||
|             var entityBuilder = new [Module]EntityBuilder(migrationBuilder, ActiveDatabase); | ||||
|             entityBuilder.Create(); | ||||
|         } | ||||
|  | ||||
|         protected override void Down(MigrationBuilder migrationBuilder) | ||||
|         { | ||||
|             var entityBuilder = new [Module]EntityBuilder(migrationBuilder, ActiveDatabase); | ||||
|             entityBuilder.Drop(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| using Microsoft.EntityFrameworkCore.Migrations; | ||||
| using Microsoft.EntityFrameworkCore.Migrations.Operations; | ||||
| using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders; | ||||
| using Oqtane.Databases.Interfaces; | ||||
| using Oqtane.Migrations; | ||||
| using Oqtane.Migrations.EntityBuilders; | ||||
|  | ||||
| namespace [Owner].[Module].Migrations.EntityBuilders | ||||
| { | ||||
|     public class [Module]EntityBuilder : AuditableBaseEntityBuilder<[Module]EntityBuilder> | ||||
|     { | ||||
|         private const string _entityTableName = "[Owner][Module]"; | ||||
|         private readonly PrimaryKey<[Module]EntityBuilder> _primaryKey = new("PK_[Owner][Module]", x => x.[Module]Id); | ||||
|         private readonly ForeignKey<[Module]EntityBuilder> _moduleForeignKey = new("FK_[Owner][Module]_Module", x => x.ModuleId, "Module", "ModuleId", ReferentialAction.Cascade); | ||||
|  | ||||
|         public [Module]EntityBuilder(MigrationBuilder migrationBuilder, IDatabase database) : base(migrationBuilder, database) | ||||
|         { | ||||
|             EntityTableName = _entityTableName; | ||||
|             PrimaryKey = _primaryKey; | ||||
|             ForeignKeys.Add(_moduleForeignKey); | ||||
|         } | ||||
|  | ||||
|         protected override [Module]EntityBuilder BuildTable(ColumnsBuilder table) | ||||
|         { | ||||
|             [Module]Id = AddAutoIncrementColumn(table,"[Module]Id"); | ||||
|             ModuleId = AddIntegerColumn(table,"ModuleId"); | ||||
|             Name = AddMaxStringColumn(table,"Name"); | ||||
|             AddAuditableColumns(table); | ||||
|             return this; | ||||
|         } | ||||
|  | ||||
|         public OperationBuilder<AddColumnOperation> [Module]Id { get; set; } | ||||
|         public OperationBuilder<AddColumnOperation> ModuleId { get; set; } | ||||
|         public OperationBuilder<AddColumnOperation> Name { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -2,15 +2,16 @@ using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Oqtane.Modules; | ||||
| using Oqtane.Repository; | ||||
| using [Owner].[Module].Models; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Repository.Databases.Interfaces; | ||||
|  | ||||
| namespace [Owner].[Module].Repository | ||||
| { | ||||
|     public class [Module]Context : DBContextBase, IService | ||||
|     public class [Module]Context : DBContextBase, IService, IMultiDatabase | ||||
|     { | ||||
|         public virtual DbSet<Models.[Module]> [Module] { get; set; } | ||||
|  | ||||
|         public [Module]Context(ITenantResolver tenantResolver, IHttpContextAccessor accessor) : base(tenantResolver, accessor) | ||||
|         public [Module]Context(ITenantManager tenantManager, IHttpContextAccessor accessor) : base(tenantManager, accessor) | ||||
|         { | ||||
|             // ContextBase handles multi-tenant database connections | ||||
|         } | ||||
|  | ||||
| @ -1,26 +0,0 @@ | ||||
| /*   | ||||
| Create [Owner][Module] table | ||||
| */ | ||||
|  | ||||
| CREATE TABLE [dbo].[[Owner][Module]]( | ||||
| 	[[Module]Id] [int] IDENTITY(1,1) NOT NULL, | ||||
| 	[ModuleId] [int] NOT NULL, | ||||
| 	[Name] [nvarchar](256) NOT NULL, | ||||
| 	[CreatedBy] [nvarchar](256) NOT NULL, | ||||
| 	[CreatedOn] [datetime] NOT NULL, | ||||
| 	[ModifiedBy] [nvarchar](256) NOT NULL, | ||||
| 	[ModifiedOn] [datetime] NOT NULL, | ||||
|   CONSTRAINT [PK_[Owner][Module]] PRIMARY KEY CLUSTERED  | ||||
|   ( | ||||
| 	[[Module]Id] ASC | ||||
|   ) | ||||
| ) | ||||
| GO | ||||
|  | ||||
| /*   | ||||
| Create foreign key relationships | ||||
| */ | ||||
| ALTER TABLE [dbo].[[Owner][Module]] WITH CHECK ADD CONSTRAINT [FK_[Owner][Module]_Module] FOREIGN KEY([ModuleId]) | ||||
| REFERENCES [dbo].Module ([ModuleId]) | ||||
| ON DELETE CASCADE | ||||
| GO | ||||
| @ -1,6 +0,0 @@ | ||||
| /*   | ||||
| Remove [Owner][Module] table | ||||
| */ | ||||
|  | ||||
| DROP TABLE [dbo].[[Owner][Module]] | ||||
| GO | ||||
| @ -13,23 +13,18 @@ | ||||
|     <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <EmbeddedResource Include="Scripts\[Owner].[Module].1.0.0.sql" /> | ||||
|     <EmbeddedResource Include="Scripts\[Owner].[Module].Uninstall.sql" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <Content Remove="wwwroot\_content\**\*.*" /> | ||||
|     <None Include="wwwroot\_content\**\*.*" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.Extensions.Localization" Version="5.0.0" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.4" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.4" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" /> | ||||
|     <PackageReference Include="Microsoft.Extensions.Localization" Version="5.0.4" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Shaun Walker
					Shaun Walker