|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using JetBrains.Annotations; |
| 7 | +using JsonApiDotNetCore.Configuration; |
| 8 | +using JsonApiDotNetCore.Middleware; |
| 9 | +using JsonApiDotNetCore.Resources; |
| 10 | +using JsonApiDotNetCore.Resources.Annotations; |
| 11 | +using JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Writing.TransactionalOutboxPattern.Messages; |
| 12 | +using Microsoft.EntityFrameworkCore; |
| 13 | + |
| 14 | +namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Writing.TransactionalOutboxPattern |
| 15 | +{ |
| 16 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 17 | + public sealed class DomainGroupDefinition : JsonApiResourceDefinition<DomainGroup, Guid> |
| 18 | + { |
| 19 | + private readonly DomainDbContext _dbContext; |
| 20 | + private readonly List<OutboxMessage> _pendingMessages = new List<OutboxMessage>(); |
| 21 | + private string _beforeGroupName; |
| 22 | + |
| 23 | + public DomainGroupDefinition(IResourceGraph resourceGraph, DomainDbContext dbContext) |
| 24 | + : base(resourceGraph) |
| 25 | + { |
| 26 | + _dbContext = dbContext; |
| 27 | + } |
| 28 | + |
| 29 | + public override Task OnPrepareWriteAsync(DomainGroup group, OperationKind operationKind, CancellationToken cancellationToken) |
| 30 | + { |
| 31 | + if (operationKind == OperationKind.CreateResource) |
| 32 | + { |
| 33 | + group.Id = Guid.NewGuid(); |
| 34 | + } |
| 35 | + else if (operationKind == OperationKind.UpdateResource) |
| 36 | + { |
| 37 | + _beforeGroupName = group.Name; |
| 38 | + } |
| 39 | + |
| 40 | + return Task.CompletedTask; |
| 41 | + } |
| 42 | + |
| 43 | + public override async Task OnSetToManyRelationshipAsync(DomainGroup group, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, |
| 44 | + OperationKind operationKind, CancellationToken cancellationToken) |
| 45 | + { |
| 46 | + if (hasManyRelationship.Property.Name == nameof(DomainGroup.Users)) |
| 47 | + { |
| 48 | + HashSet<Guid> rightUserIds = rightResourceIds.Select(resource => (Guid)resource.GetTypedId()).ToHashSet(); |
| 49 | + |
| 50 | + List<DomainUser> beforeUsers = await _dbContext.Users.Include(user => user.Group).Where(user => rightUserIds.Contains(user.Id)) |
| 51 | + .ToListAsync(cancellationToken); |
| 52 | + |
| 53 | + foreach (DomainUser beforeUser in beforeUsers) |
| 54 | + { |
| 55 | + IMessageContent content = null; |
| 56 | + |
| 57 | + if (beforeUser.Group == null) |
| 58 | + { |
| 59 | + content = new UserAddedToGroupContent |
| 60 | + { |
| 61 | + UserId = beforeUser.Id, |
| 62 | + GroupId = group.Id |
| 63 | + }; |
| 64 | + } |
| 65 | + else if (beforeUser.Group != null && beforeUser.Group.Id != group.Id) |
| 66 | + { |
| 67 | + content = new UserMovedToGroupContent |
| 68 | + { |
| 69 | + UserId = beforeUser.Id, |
| 70 | + BeforeGroupId = beforeUser.Group.Id, |
| 71 | + AfterGroupId = group.Id |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + if (content != null) |
| 76 | + { |
| 77 | + _pendingMessages.Add(OutboxMessage.CreateFromContent(content)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (group.Users != null) |
| 82 | + { |
| 83 | + foreach (DomainUser userToRemoveFromGroup in group.Users.Where(user => !rightUserIds.Contains(user.Id))) |
| 84 | + { |
| 85 | + var message = OutboxMessage.CreateFromContent(new UserRemovedFromGroupContent |
| 86 | + { |
| 87 | + UserId = userToRemoveFromGroup.Id, |
| 88 | + GroupId = group.Id |
| 89 | + }); |
| 90 | + |
| 91 | + _pendingMessages.Add(message); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public override async Task OnAddToRelationshipAsync(Guid groupId, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, |
| 98 | + CancellationToken cancellationToken) |
| 99 | + { |
| 100 | + if (hasManyRelationship.Property.Name == nameof(DomainGroup.Users)) |
| 101 | + { |
| 102 | + HashSet<Guid> rightUserIds = rightResourceIds.Select(resource => (Guid)resource.GetTypedId()).ToHashSet(); |
| 103 | + |
| 104 | + List<DomainUser> beforeUsers = await _dbContext.Users.Include(user => user.Group).Where(user => rightUserIds.Contains(user.Id)) |
| 105 | + .ToListAsync(cancellationToken); |
| 106 | + |
| 107 | + foreach (DomainUser beforeUser in beforeUsers) |
| 108 | + { |
| 109 | + IMessageContent content = null; |
| 110 | + |
| 111 | + if (beforeUser.Group == null) |
| 112 | + { |
| 113 | + content = new UserAddedToGroupContent |
| 114 | + { |
| 115 | + UserId = beforeUser.Id, |
| 116 | + GroupId = groupId |
| 117 | + }; |
| 118 | + } |
| 119 | + else if (beforeUser.Group != null && beforeUser.Group.Id != groupId) |
| 120 | + { |
| 121 | + content = new UserMovedToGroupContent |
| 122 | + { |
| 123 | + UserId = beforeUser.Id, |
| 124 | + BeforeGroupId = beforeUser.Group.Id, |
| 125 | + AfterGroupId = groupId |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + if (content != null) |
| 130 | + { |
| 131 | + _pendingMessages.Add(OutboxMessage.CreateFromContent(content)); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public override Task OnRemoveFromRelationshipAsync(DomainGroup group, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds, |
| 138 | + CancellationToken cancellationToken) |
| 139 | + { |
| 140 | + if (hasManyRelationship.Property.Name == nameof(DomainGroup.Users)) |
| 141 | + { |
| 142 | + HashSet<Guid> rightUserIds = rightResourceIds.Select(resource => (Guid)resource.GetTypedId()).ToHashSet(); |
| 143 | + |
| 144 | + foreach (DomainUser userToRemoveFromGroup in group.Users.Where(user => rightUserIds.Contains(user.Id))) |
| 145 | + { |
| 146 | + var message = OutboxMessage.CreateFromContent(new UserRemovedFromGroupContent |
| 147 | + { |
| 148 | + UserId = userToRemoveFromGroup.Id, |
| 149 | + GroupId = group.Id |
| 150 | + }); |
| 151 | + |
| 152 | + _pendingMessages.Add(message); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return Task.CompletedTask; |
| 157 | + } |
| 158 | + |
| 159 | + public override async Task OnWritingAsync(DomainGroup group, OperationKind operationKind, CancellationToken cancellationToken) |
| 160 | + { |
| 161 | + if (operationKind == OperationKind.CreateResource) |
| 162 | + { |
| 163 | + var message = OutboxMessage.CreateFromContent(new GroupCreatedContent |
| 164 | + { |
| 165 | + GroupId = group.Id, |
| 166 | + GroupName = group.Name |
| 167 | + }); |
| 168 | + |
| 169 | + await _dbContext.OutboxMessages.AddAsync(message, cancellationToken); |
| 170 | + } |
| 171 | + else if (operationKind == OperationKind.UpdateResource) |
| 172 | + { |
| 173 | + if (_beforeGroupName != group.Name) |
| 174 | + { |
| 175 | + var message = OutboxMessage.CreateFromContent(new GroupRenamedContent |
| 176 | + { |
| 177 | + GroupId = group.Id, |
| 178 | + BeforeGroupName = _beforeGroupName, |
| 179 | + AfterGroupName = group.Name |
| 180 | + }); |
| 181 | + |
| 182 | + await _dbContext.OutboxMessages.AddAsync(message, cancellationToken); |
| 183 | + } |
| 184 | + } |
| 185 | + else if (operationKind == OperationKind.DeleteResource) |
| 186 | + { |
| 187 | + DomainGroup groupToDelete = await _dbContext.Groups.Include(domainGroup => domainGroup.Users) |
| 188 | + .FirstOrDefaultAsync(domainGroup => domainGroup.Id == group.Id, cancellationToken); |
| 189 | + |
| 190 | + if (groupToDelete != null) |
| 191 | + { |
| 192 | + foreach (DomainUser user in groupToDelete.Users) |
| 193 | + { |
| 194 | + var removeMessage = OutboxMessage.CreateFromContent(new UserRemovedFromGroupContent |
| 195 | + { |
| 196 | + UserId = user.Id, |
| 197 | + GroupId = group.Id |
| 198 | + }); |
| 199 | + |
| 200 | + await _dbContext.OutboxMessages.AddAsync(removeMessage, cancellationToken); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + var deleteMessage = OutboxMessage.CreateFromContent(new GroupDeletedContent |
| 205 | + { |
| 206 | + GroupId = group.Id |
| 207 | + }); |
| 208 | + |
| 209 | + await _dbContext.OutboxMessages.AddAsync(deleteMessage, cancellationToken); |
| 210 | + } |
| 211 | + |
| 212 | + foreach (OutboxMessage nextMessage in _pendingMessages) |
| 213 | + { |
| 214 | + await _dbContext.OutboxMessages.AddAsync(nextMessage, cancellationToken); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments