Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(User) :some exception #12

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading