| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Identity.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Memory counterpart of the EF <c>PerTenantIdentityUniqueness</c> indexes on |
| | | 8 | | /// <c>(TenantId, Name)</c> and <c>(TenantId, ClientId)</c>. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class MemoryIdentityUniqueness |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Throws when another Id in the same tenant already owns <paramref name="keySelector"/>. |
| | | 14 | | /// Same-Id upserts are allowed so a row can rename itself. |
| | | 15 | | /// </summary> |
| | | 16 | | public static void EnsureAvailable<T>( |
| | | 17 | | MemoryStore<T> store, |
| | | 18 | | T entity, |
| | | 19 | | Func<T, string?> keySelector, |
| | | 20 | | string keyName) |
| | | 21 | | where T : Entity |
| | | 22 | | { |
| | 163 | 23 | | var key = keySelector(entity); |
| | 163 | 24 | | var existing = store.Find(candidate => |
| | 261 | 25 | | candidate.TenantId == entity.TenantId |
| | 261 | 26 | | && candidate.Id != entity.Id |
| | 261 | 27 | | && keySelector(candidate) == key); |
| | | 28 | | |
| | 163 | 29 | | if (existing is not null) |
| | | 30 | | { |
| | 11 | 31 | | throw new InvalidOperationException( |
| | 11 | 32 | | $"A {typeof(T).Name.ToLowerInvariant()} already exists with {keyName} '{key}' in tenant '{entity.TenantI |
| | | 33 | | } |
| | 152 | 34 | | } |
| | | 35 | | } |