-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing transaction-style toolbox backend
- Loading branch information
1 parent
3b0245b
commit 5c07091
Showing
6 changed files
with
165 additions
and
165 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Portal/src/Datahub.Application/Services/Toolbox/IToolboxService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Datahub.Application.Services.Toolbox | ||
{ | ||
public interface IToolboxService | ||
{ | ||
public List<ToolboxTransaction> BeginTransaction(); | ||
} | ||
|
||
public class ToolboxTransaction | ||
{ | ||
public string Tool { get; set; } | ||
public ToolboxTransactionType Type { get; set; } | ||
public dynamic? OriginalData { get; set; } | ||
public dynamic? UpdatedData { get; set; } | ||
} | ||
|
||
public enum ToolboxTransactionType | ||
{ | ||
Add, | ||
Remove, | ||
Update | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Portal/src/Datahub.Infrastructure/Services/Toolbox/ToolboxService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Reflection; | ||
using Datahub.Application.Services.Toolbox; | ||
using Datahub.Shared.Entities; | ||
|
||
namespace Datahub.Infrastructure.Services.Toolbox | ||
{ | ||
public class ToolboxService : IToolboxService | ||
{ | ||
public List<ToolboxTransaction> BeginTransaction() | ||
{ | ||
return new List<ToolboxTransaction>(); | ||
} | ||
} | ||
|
||
public static class ToolboxTransactionExtensions | ||
{ | ||
public static ToolboxTransaction AddTool(this List<ToolboxTransaction> transactions, string tool) | ||
{ | ||
var transaction = new ToolboxTransaction | ||
{ | ||
Tool = tool, | ||
Type = ToolboxTransactionType.Add | ||
}; | ||
transactions.Add(transaction); | ||
return transaction; | ||
} | ||
|
||
public static ToolboxTransaction RemoveTool(this List<ToolboxTransaction> transactions, string tool) | ||
{ | ||
var transaction = new ToolboxTransaction | ||
{ | ||
Tool = tool, | ||
Type = ToolboxTransactionType.Remove | ||
}; | ||
transactions.Add(transaction); | ||
return transaction; | ||
} | ||
|
||
public static ToolboxTransaction UpdateTool(this List<ToolboxTransaction> transactions, string tool) | ||
{ | ||
var transaction = new ToolboxTransaction | ||
{ | ||
Tool = tool, | ||
Type = ToolboxTransactionType.Update | ||
}; | ||
transactions.Add(transaction); | ||
return transaction; | ||
} | ||
|
||
public static void Revert(this List<ToolboxTransaction> transactions, ToolboxTransaction transaction) | ||
{ | ||
transactions.Remove(transaction); | ||
} | ||
|
||
public static Dictionary<string, (object Original, object? Updated)> Diff(this ToolboxTransaction transaction) | ||
{ | ||
if (transaction.OriginalData == null || transaction.UpdatedData == null) | ||
{ | ||
throw new InvalidOperationException("OriginalData and UpdatedData must be set before calling Diff"); | ||
} | ||
|
||
if (transaction.OriginalData!.GetType() != transaction.UpdatedData!.GetType()) | ||
{ | ||
throw new InvalidOperationException("OriginalData and UpdatedData must be of same type"); | ||
} | ||
|
||
// Diff the data | ||
PropertyInfo[] originalProperties = transaction.OriginalData.GetType() | ||
.GetProperties(); | ||
|
||
PropertyInfo[] updatedProperties = transaction.UpdatedData.GetType() | ||
.GetProperties(); | ||
|
||
var differences = originalProperties | ||
.Where(prop => updatedProperties.All(p => p.Name != prop.Name) || | ||
!Equals(prop.GetValue(transaction.OriginalData), prop.GetValue(transaction.UpdatedData))) | ||
.ToDictionary(prop => prop.Name, | ||
prop => (Original: prop.GetValue(transaction.OriginalData), | ||
Updated: updatedProperties.First(p => p.Name == prop.Name).GetValue(transaction.UpdatedData))); | ||
|
||
|
||
return differences; | ||
} | ||
} | ||
} |
13 changes: 9 additions & 4 deletions
13
Portal/src/Datahub.Portal/Pages/Workspace/Toolbox/InfoSheet.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.