Added transaction event and mail sending for the transaction
This commit is contained in:
10
Moonlight/App/Event/Args/TransactionCreatedEventArgs.cs
Normal file
10
Moonlight/App/Event/Args/TransactionCreatedEventArgs.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using Moonlight.App.Database.Entities;
|
||||||
|
using Moonlight.App.Database.Entities.Store;
|
||||||
|
|
||||||
|
namespace Moonlight.App.Event.Args;
|
||||||
|
|
||||||
|
public class TransactionCreatedEventArgs
|
||||||
|
{
|
||||||
|
public Transaction Transaction { get; set; }
|
||||||
|
public User User { get; set; }
|
||||||
|
}
|
||||||
@@ -11,4 +11,5 @@ public class Events
|
|||||||
public static EventHandler<User> OnUserTotpSet;
|
public static EventHandler<User> OnUserTotpSet;
|
||||||
public static EventHandler<MailVerificationEventArgs> OnUserMailVerify;
|
public static EventHandler<MailVerificationEventArgs> OnUserMailVerify;
|
||||||
public static EventHandler<Service> OnServiceOrdered;
|
public static EventHandler<Service> OnServiceOrdered;
|
||||||
|
public static EventHandler<TransactionCreatedEventArgs> OnTransactionCreated;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using Moonlight.App.Database.Entities;
|
using Moonlight.App.Database.Entities;
|
||||||
using Moonlight.App.Database.Entities.Store;
|
using Moonlight.App.Database.Entities.Store;
|
||||||
using Moonlight.App.Event;
|
using Moonlight.App.Event;
|
||||||
|
using Moonlight.App.Event.Args;
|
||||||
|
|
||||||
namespace Moonlight.App.Services.Background;
|
namespace Moonlight.App.Services.Background;
|
||||||
|
|
||||||
@@ -14,6 +15,18 @@ public class AutoMailSendService // This service is responsible for sending mail
|
|||||||
|
|
||||||
Events.OnUserRegistered += OnUserRegistered;
|
Events.OnUserRegistered += OnUserRegistered;
|
||||||
Events.OnServiceOrdered += OnServiceOrdered;
|
Events.OnServiceOrdered += OnServiceOrdered;
|
||||||
|
Events.OnTransactionCreated += OnTransactionCreated;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void OnTransactionCreated(object? sender, TransactionCreatedEventArgs eventArgs)
|
||||||
|
{
|
||||||
|
await MailService.Send(
|
||||||
|
eventArgs.User,
|
||||||
|
"New transaction",
|
||||||
|
"transactionCreated",
|
||||||
|
eventArgs.Transaction,
|
||||||
|
eventArgs.User
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void OnServiceOrdered(object? _, Service service)
|
private async void OnServiceOrdered(object? _, Service service)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using Moonlight.App.Database.Entities;
|
using Moonlight.App.Database.Entities;
|
||||||
using Moonlight.App.Database.Entities.Store;
|
using Moonlight.App.Database.Entities.Store;
|
||||||
|
using Moonlight.App.Event;
|
||||||
|
using Moonlight.App.Extensions;
|
||||||
using Moonlight.App.Helpers;
|
using Moonlight.App.Helpers;
|
||||||
using Moonlight.App.Repositories;
|
using Moonlight.App.Repositories;
|
||||||
|
|
||||||
@@ -14,16 +16,17 @@ public class TransactionService
|
|||||||
UserRepository = userRepository;
|
UserRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Add(User u, double amount, string message)
|
public async Task Add(User u, double amount, string message)
|
||||||
{
|
{
|
||||||
var user = UserRepository.Get().First(x => x.Id == u.Id); // Load user with current repo
|
var user = UserRepository.Get().First(x => x.Id == u.Id); // Load user with current repo
|
||||||
|
|
||||||
user.Transactions.Add(new Transaction()
|
var transaction = new Transaction()
|
||||||
{
|
{
|
||||||
Text = message,
|
Text = message,
|
||||||
Price = amount
|
Price = amount
|
||||||
});
|
};
|
||||||
|
|
||||||
|
user.Transactions.Add(transaction);
|
||||||
UserRepository.Update(user);
|
UserRepository.Update(user);
|
||||||
|
|
||||||
// We divide the call to ensure the transaction can be written to the database
|
// We divide the call to ensure the transaction can be written to the database
|
||||||
@@ -33,6 +36,10 @@ public class TransactionService
|
|||||||
|
|
||||||
UserRepository.Update(user);
|
UserRepository.Update(user);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
await Events.OnTransactionCreated.InvokeAsync(new()
|
||||||
|
{
|
||||||
|
Transaction = transaction,
|
||||||
|
User = user
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user