Skip to content

Commit

Permalink
Added RelatedEntitiesOnUpdate attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix-CodingClimber committed Mar 12, 2024
1 parent 0b83c2f commit 1b9d6c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/DotNetElements.Core/Core/RelatedEntitiesOnUpdateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace DotNetElements.Core;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RelatedEntitiesOnUpdateAttribute : Attribute
{
public string[] ReferenceProperties { get; private init; }

public RelatedEntitiesOnUpdateAttribute(string[] referenceProperties)
{
ReferenceProperties = referenceProperties;

}
}

// todo needed for on update?
//[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
//public class RelatedEntitiesCollectionsAttribute : Attribute
//{
// public string[] ReferenceProperties { get; private init; }

// public RelatedEntitiesCollectionsAttribute(string[] referenceProperties)
// {
// ReferenceProperties = referenceProperties;

// }
//}
21 changes: 19 additions & 2 deletions src/DotNetElements.Core/Core/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public abstract class Repository<TDbContext, TEntity, TKey> : ReadOnlyRepository
protected ICurrentUserProvider CurrentUserProvider { get; private init; }
protected TimeProvider TimeProvider { get; private init; }

protected static readonly RelatedEntitiesOnUpdateAttribute? RelatedEntitiesOnUpdate = typeof(TEntity).GetCustomAttribute<RelatedEntitiesOnUpdateAttribute>();

protected static readonly RelatedEntitiesAttribute? RelatedEntities = typeof(TEntity).GetCustomAttribute<RelatedEntitiesAttribute>();
protected static readonly RelatedEntitiesCollectionsAttribute? RelatedEntitiesCollections = typeof(TEntity).GetCustomAttribute<RelatedEntitiesCollectionsAttribute>();

Expand Down Expand Up @@ -71,7 +73,9 @@ public virtual async Task<CrudResult<TSelf>> CreateOrUpdateAsync<TSelf>(TKey id,
else
{
// Update existing entity
TSelf? existingEntity = await DbContext.Set<TSelf>().FirstOrDefaultAsync(WithId<TSelf>(id));
IQueryable<TSelf> query = LoadRelatedEntitiesOnUpdate(DbContext.Set<TSelf>());

TSelf? existingEntity = await query.FirstOrDefaultAsync(WithId<TSelf>(id));

if (existingEntity is null)
return CrudResult.NotFound(id);
Expand All @@ -98,7 +102,9 @@ public virtual async Task<CrudResult<TEntity>> UpdateAsync<TFrom>(TKey id, TFrom
{
ThrowHelper.ThrowIfDefault(id);

TEntity? existingEntity = await Entities.FirstOrDefaultAsync(WithId(id));
IQueryable<TEntity> query = LoadRelatedEntitiesOnUpdate(Entities);

TEntity? existingEntity = await query.FirstOrDefaultAsync(WithId(id));

if (existingEntity is null)
return CrudResult.NotFound(id);
Expand Down Expand Up @@ -265,6 +271,17 @@ protected async Task<bool> SaveChangesWithVersionCheckAsync()
return true;
}

protected IQueryable<TUpdatedEntity> LoadRelatedEntitiesOnUpdate<TUpdatedEntity>(IQueryable<TUpdatedEntity> query)
where TUpdatedEntity : Entity<TKey>
{
if (RelatedEntitiesOnUpdate is not null)
{
foreach (string relatedProperty in RelatedEntitiesOnUpdate.ReferenceProperties)
query = query.Include(relatedProperty);
}
return query;
}

protected Task LoadRelatedEntities(TEntity entity)
{
return LoadRelatedEntities(DbContext.Entry(entity));
Expand Down

0 comments on commit 1b9d6c5

Please sign in to comment.