Skip to content

Commit

Permalink
Added support for recursive save
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Nov 2, 2022
1 parent 36d15e4 commit 562b1d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions NeuroSpeech.EntityAccessControl/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,23 @@ public BaseController(ISecureQueryProvider db)
// .MakeGenericMethod(type.ClrType)!.Invoke(this, null) as IQueryable;
//}

private Dictionary<int, object> objects = new Dictionary<int, object>();

protected virtual async Task<object> LoadOrCreateAsync(Type? type,
JsonElement body,
bool isChild = false,
CancellationToken cancellationToken = default)
{
IEntityType entityType;

if (body.TryGetInt32Property("$id", out var id))
{
if(objects.TryGetValue(id, out var v))
{
return v;
}
}

if (body.TryGetStringProperty("$type", out var typeName))
{
if (typeName.EqualsIgnoreCase("null"))
Expand Down Expand Up @@ -86,6 +97,10 @@ protected virtual async Task<object> LoadOrCreateAsync(Type? type,
{
db.Remove(e);
}
if (id > 0)
{
objects[id] = e;
}
return e;

}
Expand Down
15 changes: 15 additions & 0 deletions NeuroSpeech.EntityAccessControl/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public static bool TryGetPropertyCaseInsensitive(
}
value = default;
return false;
}

public static bool TryGetInt32Property(
in this JsonElement element,
string name,
[NotNullWhen(true)]
out int value)
{
if (element.TryGetProperty(name, out var token))
{
value = token.GetInt32();
return true;
}
value = default;
return false;
}

public static bool TryGetStringProperty(
Expand Down

0 comments on commit 562b1d8

Please sign in to comment.