-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathRetailTenantChangeService.cs
216 lines (186 loc) · 10.6 KB
/
RetailTenantChangeService.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2021 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using AuthPermissions.AdminCode;
using AuthPermissions.AdminCode.Services;
using AuthPermissions.AspNetCore.ShardingServices;
using AuthPermissions.BaseCode;
using AuthPermissions.BaseCode.CommonCode;
using AuthPermissions.BaseCode.DataLayer.Classes;
using Example4.ShopCode.EfCoreClasses;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Example4.ShopCode.EfCoreCode
{
public class RetailTenantChangeService : ITenantChangeService
{
private readonly RetailDbContext _context;
private readonly ILogger _logger;
public IReadOnlyList<int> DeletedTenantIds { get; private set; }
public RetailTenantChangeService(RetailDbContext context, ILogger<RetailTenantChangeService> logger)
{
_context = context;
_logger = logger;
}
/// <summary>
/// When a new AuthP Tenant is created, then this method is called. If you have a tenant-type entity in your
/// application's database, then this allows you to create a new entity for the new tenant.
/// You should apply multiple changes within a transaction so that if any fails then any previous changes will be rolled back.
/// NOTE: With hierarchical tenants you cannot be sure that the tenant has, or will have, children so we always add a retail
/// </summary>
/// <param name="tenant">The tenant data used to create a new tenant</param>
/// <returns>Returns null if all OK, otherwise the create is rolled back and the return string is shown to the user</returns>
public async Task<string> CreateNewTenantAsync(Tenant tenant)
{
_context.Add(new RetailOutlet(tenant.TenantId, tenant.TenantFullName, tenant.GetTenantDataKey()));
await _context.SaveChangesAsync();
return null;
}
//not used
public Task<string> SingleTenantUpdateNameAsync(Tenant tenant)
{
throw new NotImplementedException();
}
/// <summary>
/// This is called when the name of your Tenants is changed. This is useful if you use the tenant name in your multi-tenant data.
/// NOTE: The created application's DbContext won't have a DataKey, so you will need to use IgnoreQueryFilters on any EF Core read.
/// You should apply multiple changes within a transaction so that if any fails then any previous changes will be rolled back.
/// </summary>
/// <param name="tenantsToUpdate">This contains the tenants to update.</param>
/// <returns>Returns null if all OK, otherwise the name change is rolled back and the return string is shown to the user</returns>
public async Task<string> HierarchicalTenantUpdateNameAsync(List<Tenant> tenantsToUpdate)
{
await using var transaction = await _context.Database.BeginTransactionAsync(IsolationLevel.Serializable);
try
{
foreach (var tenant in tenantsToUpdate)
{
//Higher hierarchical levels don't have data in this example
var retailOutletToUpdate =
await _context.RetailOutlets
.IgnoreQueryFilters().SingleOrDefaultAsync(x => x.AuthPTenantId == tenant.TenantId);
if (retailOutletToUpdate != null)
{
retailOutletToUpdate.UpdateNames(tenant.TenantFullName);
await _context.SaveChangesAsync();
}
}
await transaction.CommitAsync();
}
catch (Exception e)
{
_logger.LogError(e, "Failure when trying to update a hierarchical tenant.");
return "There was a system-level problem - see logs for more detail";
}
return null;
}
//Not used
public Task<string> SingleTenantDeleteAsync(Tenant tenant)
{
throw new System.NotImplementedException();
}
/// <summary>
/// This is used with hierarchical tenants to either
/// a) delete all the application-side data with the given DataKey, or
/// b) soft-delete the data.
/// You should apply multiple changes within a transaction so that if any fails then any previous changes will be rolled back
/// Notes:
/// - The created application's DbContext won't have a DataKey, so you will need to use IgnoreQueryFilters on any EF Core read
/// - You can provide information of what you have done by adding public parameters to this class.
/// The TenantAdmin <see cref="AuthTenantAdminService.DeleteTenantAsync"/> method returns your class on a successful Delete
/// </summary>
/// <param name="tenantsInOrder">The tenants to delete with the children first in case a higher level links to a lower level</param>
/// <returns>Returns null if all OK, otherwise the AuthP part of the delete is rolled back and the return string is shown to the user</returns>
public async Task<string> HierarchicalTenantDeleteAsync(List<Tenant> tenantsInOrder)
{
await using var transaction = await _context.Database.BeginTransactionAsync(IsolationLevel.Serializable);
try
{
var deletedTenantIds = new List<int>();
foreach (var tenant in tenantsInOrder)
{
//Higher hierarchical levels don't have data in this example, so it only tries to delete data if there is a RetailOutlet
var retailOutletToDelete =
await _context.RetailOutlets
.IgnoreQueryFilters().SingleOrDefaultAsync(x => x.AuthPTenantId == tenant.TenantId);
if (retailOutletToDelete != null)
{
//yes, its a shop so delete all the stock / sales
var deleteSalesSql =
$"DELETE FROM retail.{nameof(RetailDbContext.ShopSales)} WHERE DataKey = '{tenant.GetTenantDataKey()}'";
await _context.Database.ExecuteSqlRawAsync(deleteSalesSql);
var deleteStockSql =
$"DELETE FROM retail.{nameof(RetailDbContext.ShopStocks)} WHERE DataKey = '{tenant.GetTenantDataKey()}'";
await _context.Database.ExecuteSqlRawAsync(deleteStockSql);
_context.Remove(retailOutletToDelete); //finally delete the RetailOutlet
await _context.SaveChangesAsync();
deletedTenantIds.Add(tenant.TenantId);
}
}
await transaction.CommitAsync();
DeletedTenantIds = deletedTenantIds.AsReadOnly();
}
catch (Exception e)
{
_logger.LogError(e, "Failure when trying to delete a hierarchical tenant.");
return "There was a system-level problem - see logs for more detail";
}
return null; //null means OK, otherwise the delete is rolled back and the return string is shown to the user
}
/// <summary>
/// This is used with hierarchical tenants, where you move one tenant (and its children) to another tenant
/// This requires you to change the DataKeys of each application's tenant data, so they link to the new tenant.
/// Also, if you contain the name of the tenant in your data, then you need to update its new FullName
/// Notes:
/// - The created application's DbContext won't have a DataKey, so you will need to use IgnoreQueryFilters on any EF Core read
/// - You can get multiple calls if move a higher level
/// </summary>
/// <param name="tenantToUpdate">The data to update each tenant. This starts at the parent and then recursively works down the children</param>
/// <returns>Returns null if all OK, otherwise AuthP part of the move is rolled back and the return string is shown to the user</returns>
public async Task<string> MoveHierarchicalTenantDataAsync(List<(string oldDataKey, Tenant tenantToMove)> tenantToUpdate)
{
await using var transaction = await _context.Database.BeginTransactionAsync(IsolationLevel.Serializable);
try
{
foreach (var tuple in tenantToUpdate)
{
//Higher hierarchical levels don't have data in this example, so it only tries to move data if there is a RetailOutlet
var retailOutletMove =
await _context.RetailOutlets
.IgnoreQueryFilters()
.SingleOrDefaultAsync(x => x.AuthPTenantId == tuple.tenantToMove.TenantId);
if (retailOutletMove != null)
{
//yes, its a shop so move all the stock / sales
//This code will update the DataKey of every entity that has the IDataKeyFilterReadOnly interface
foreach (var entityType in _context.Model.GetEntityTypes()
.Where(x => typeof(IDataKeyFilterReadOnly).IsAssignableFrom(x.ClrType)))
{
var updateDataKey = $"UPDATE {entityType.FormSchemaTableFromModel()} " +
$"SET DataKey = '{tuple.tenantToMove.GetTenantDataKey()}' WHERE DataKey = '{tuple.oldDataKey}'";
await _context.Database.ExecuteSqlRawAsync(updateDataKey);
}
retailOutletMove.UpdateNames(tuple.tenantToMove.TenantFullName);
await _context.SaveChangesAsync();
}
}
await transaction.CommitAsync();
}
catch (Exception e)
{
_logger.LogError(e, "Failure when trying to Move a hierarchical tenant.");
return "There was a system-level problem - see logs for more detail";
}
return null;
}
public Task<string> MoveToDifferentDatabaseAsync(string oldDatabaseInfoName, string oldDataKey,
Tenant updatedTenant)
{
throw new NotImplementedException();
}
}
}