-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a538618
commit 014395c
Showing
3 changed files
with
100 additions
and
90 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,77 +1,80 @@ | ||
using AnalysisData.Graph.DataManage.Model; | ||
using AnalysisData.Repository.AccountRepository; | ||
using AnalysisData.Repository.TransactionRepository; | ||
using QuickGraph; | ||
|
||
namespace AnalysisData.Graph; | ||
|
||
public class GraphUtility | ||
{ | ||
public BidirectionalGraph<Account, TransactionEdgeAdapter> Graph { get; private set; } | ||
private readonly AccountRepository _accountService; | ||
private readonly TransactionRepository _transactionService; | ||
|
||
public GraphUtility(AccountRepository accountService, TransactionRepository transactionService) | ||
{ | ||
Graph = new BidirectionalGraph<Account, TransactionEdgeAdapter>(); | ||
_accountService = accountService; | ||
_transactionService = transactionService; | ||
} | ||
|
||
public async Task BuildGraphAsync() | ||
{ | ||
var accounts = await _accountService.GetAllAccounts(); | ||
foreach (var account in accounts) | ||
{ | ||
if (!Graph.ContainsVertex(account)) | ||
{ | ||
Graph.AddVertex(account); | ||
} | ||
} | ||
|
||
var transactions = await _transactionService.GetAllTransaction(); | ||
foreach (var transaction in transactions) | ||
{ | ||
await AddTransactionAsync(transaction); | ||
} | ||
} | ||
|
||
public async Task AddTransactionAsync(Transaction transaction) | ||
{ | ||
var sourceAccount = await _accountService.GetAccountById(transaction.SourceAccount); | ||
var targetAccount = await _accountService.GetAccountById(transaction.DestinationAccount); | ||
|
||
if (sourceAccount == null || targetAccount == null) | ||
{ | ||
throw new ArgumentException("Both accounts must exist in the database."); | ||
} | ||
|
||
var edge = new TransactionEdgeAdapter(sourceAccount, targetAccount, transaction); | ||
Graph.AddEdge(edge); | ||
} | ||
|
||
public string ToJson() | ||
{ | ||
var transactions = new List<object>(); | ||
|
||
foreach (var edge in Graph.Edges) | ||
{ | ||
transactions.Add(new | ||
{ | ||
From = edge.Source.AccountID, | ||
To = edge.Target.AccountID, | ||
Transaction = new | ||
{ | ||
SourceAccount = edge.Transaction.SourceAccount, | ||
DestinationAccount = edge.Transaction.DestinationAccount, | ||
Amount = edge.Transaction.Amount, | ||
Date = edge.Transaction.Date, | ||
TransactionID = edge.Transaction.TransactionID, | ||
Type = edge.Transaction.Type | ||
} | ||
}); | ||
} | ||
|
||
return System.Text.Json.JsonSerializer.Serialize(transactions, new System.Text.Json.JsonSerializerOptions { WriteIndented = true }); | ||
} | ||
} | ||
// using AnalysisData.Graph.DataManage.Model; | ||
// using AnalysisData.Repository.AccountRepository; | ||
// using AnalysisData.Repository.TransactionRepository; | ||
// using QuickGraph; | ||
// | ||
// namespace AnalysisData.Graph; | ||
// | ||
// public class GraphUtility | ||
// { | ||
// public BidirectionalGraph<Account, TransactionEdgeAdapter> Graph { get; private set; } | ||
// private readonly AccountRepository _accountService; | ||
// private readonly TransactionRepository _transactionService; | ||
// | ||
// public GraphUtility(AccountRepository accountService, TransactionRepository transactionService) | ||
// { | ||
// Graph = new BidirectionalGraph<Account, TransactionEdgeAdapter>(); | ||
// _accountService = accountService; | ||
// _transactionService = transactionService; | ||
// } | ||
// | ||
// public async Task BuildGraphAsync() | ||
// { | ||
// var accounts = await _accountService.GetAllAccounts(); | ||
// foreach (var account in accounts) | ||
// { | ||
// if (!Graph.ContainsVertex(account)) | ||
// { | ||
// Graph.AddVertex(account); | ||
// } | ||
// } | ||
// | ||
// var transactions = await _transactionService.GetAllTransaction(); | ||
// foreach (var transaction in transactions) | ||
// { | ||
// await AddTransactionAsync(transaction); | ||
// } | ||
// } | ||
// | ||
// public async Task AddTransactionAsync(Transaction transaction) | ||
// { | ||
// var sourceAccount = await _accountService.GetAccountById(transaction.SourceAccount); | ||
// var targetAccount = await _accountService.GetAccountById(transaction.DestinationAccount); | ||
// | ||
// if (sourceAccount == null || targetAccount == null) | ||
// { | ||
// throw new ArgumentException("Both accounts must exist in the database."); | ||
// } | ||
// | ||
// var edge = new TransactionEdgeAdapter(sourceAccount, targetAccount, transaction); | ||
// Graph.AddEdge(edge); | ||
// } | ||
// | ||
// public string ToJson() | ||
// { | ||
// var transactions = new List<object>(); | ||
// | ||
// foreach (var edge in Graph.Edges) | ||
// { | ||
// transactions.Add(new | ||
// { | ||
// From = edge.Source.AccountID, | ||
// To = edge.Target.AccountID, | ||
// Transaction = new | ||
// { | ||
// SourceAccount = edge.Transaction.SourceAccount, | ||
// DestinationAccount = edge.Transaction.DestinationAccount, | ||
// Amount = edge.Transaction.Amount, | ||
// Date = edge.Transaction.Date, | ||
// TransactionID = edge.Transaction.TransactionID, | ||
// Type = edge.Transaction.Type | ||
// } | ||
// }); | ||
// } | ||
// | ||
// return System.Text.Json.JsonSerializer.Serialize(transactions, new System.Text.Json.JsonSerializerOptions { WriteIndented = true }); | ||
// } | ||
// | ||
// | ||
// public BidirectionalGraph<Account, ITransactionEdge> GetGraph() => Graph; | ||
// } |
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,12 @@ | ||
// using AnalysisData.Graph.DataManage.Model; | ||
// using QuickGraph; | ||
// | ||
// namespace AnalysisData.Graph; | ||
// | ||
// public interface IGraphUtility | ||
// { | ||
// Task BuildGraphAsync(); | ||
// Task AddTransactionAsync(Transaction transaction); | ||
// string ToJson(); | ||
// BidirectionalGraph<Account, Transaction> GetGraph(); | ||
// } |
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