Skip to content

Commit

Permalink
fix(User) :some exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdijafariii committed Aug 17, 2024
1 parent a538618 commit f5afa60
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 90 deletions.
157 changes: 80 additions & 77 deletions AnalysisData/AnalysisData/Graph/GraphUtility.cs
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;
// }
12 changes: 12 additions & 0 deletions AnalysisData/AnalysisData/Graph/IGraphUtility.cs
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();
// }
21 changes: 8 additions & 13 deletions AnalysisData/AnalysisData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularApp",
corsPolicyBuilder => corsPolicyBuilder.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
Expand Down Expand Up @@ -90,17 +98,4 @@
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;

var context = services.GetRequiredService<ApplicationDbContext>();
context.Database.EnsureCreated();

if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}
}
app.Run();

0 comments on commit f5afa60

Please sign in to comment.