|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using JsonApiDotNetCore.Data; |
| 7 | +using JsonApiDotNetCore.Extensions; |
| 8 | +using JsonApiDotNetCore.Models; |
| 9 | +using Microsoft.EntityFrameworkCore; |
| 10 | + |
| 11 | +namespace JsonApiDotNetCore.Internal.Generics |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A special helper service that gets instantiated for the right-type of a many-to-many relationship and is responsible for |
| 15 | + /// processing updates for that relationships |
| 16 | + /// </summary> |
| 17 | + public interface IHasManyThroughUpdateHelper |
| 18 | + { |
| 19 | + Task UpdateAsync(IIdentifiable parent, HasManyThroughAttribute relationship, IEnumerable<string> relationshipIds); |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// A special processor that gets instantiated for a generic type (<T>) |
| 24 | + /// when the actual type is not known until runtime. Specifically, this is used for updating |
| 25 | + /// relationships. |
| 26 | + /// </summary> |
| 27 | + public class HasManyThroughUpdateHelper<T> : IHasManyThroughUpdateHelper where T : class |
| 28 | + { |
| 29 | + private readonly DbContext _context; |
| 30 | + public HasManyThroughUpdateHelper(IDbContextResolver contextResolver) |
| 31 | + { |
| 32 | + _context = contextResolver.GetContext(); |
| 33 | + } |
| 34 | + |
| 35 | + public virtual async Task UpdateAsync(IIdentifiable parent, HasManyThroughAttribute relationship, IEnumerable<string> relationshipIds) |
| 36 | + { |
| 37 | + // we need to create a transaction for the HasManyThrough case so we can get and remove any existing |
| 38 | + // join entities and only commit if all operations are successful |
| 39 | + using (var transaction = await _context.GetCurrentOrCreateTransactionAsync()) |
| 40 | + { |
| 41 | + // ArticleTag |
| 42 | + ParameterExpression parameter = Expression.Parameter(relationship.ThroughType); |
| 43 | + |
| 44 | + // ArticleTag.ArticleId |
| 45 | + Expression property = Expression.Property(parameter, relationship.LeftIdProperty); |
| 46 | + |
| 47 | + // article.Id |
| 48 | + var parentId = TypeHelper.ConvertType(parent.StringId, relationship.LeftIdProperty.PropertyType); |
| 49 | + Expression target = Expression.Constant(parentId); |
| 50 | + |
| 51 | + // ArticleTag.ArticleId.Equals(article.Id) |
| 52 | + Expression equals = Expression.Call(property, "Equals", null, target); |
| 53 | + |
| 54 | + var lambda = Expression.Lambda<Func<T, bool>>(equals, parameter); |
| 55 | + |
| 56 | + // TODO: we shouldn't need to do this instead we should try updating the existing? |
| 57 | + // the challenge here is if a composite key is used, then we will fail to |
| 58 | + // create due to a unique key violation |
| 59 | + var oldLinks = _context |
| 60 | + .Set<T>() |
| 61 | + .Where(lambda.Compile()) |
| 62 | + .ToList(); |
| 63 | + |
| 64 | + _context.RemoveRange(oldLinks); |
| 65 | + |
| 66 | + var newLinks = relationshipIds.Select(x => { |
| 67 | + var link = Activator.CreateInstance(relationship.ThroughType); |
| 68 | + relationship.LeftIdProperty.SetValue(link, TypeHelper.ConvertType(parentId, relationship.LeftIdProperty.PropertyType)); |
| 69 | + relationship.RightIdProperty.SetValue(link, TypeHelper.ConvertType(x, relationship.RightIdProperty.PropertyType)); |
| 70 | + return link; |
| 71 | + }); |
| 72 | + |
| 73 | + _context.AddRange(newLinks); |
| 74 | + await _context.SaveChangesAsync(); |
| 75 | + |
| 76 | + transaction.Commit(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments