Added transaction event and mail sending for the transaction

This commit is contained in:
Baumgartner Marcel 2023-10-26 09:45:54 +02:00
parent 7a3d61c659
commit 4de6804f13
5 changed files with 38 additions and 7 deletions

View 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; }
}

View file

@ -11,4 +11,5 @@ public class Events
public static EventHandler<User> OnUserTotpSet;
public static EventHandler<MailVerificationEventArgs> OnUserMailVerify;
public static EventHandler<Service> OnServiceOrdered;
public static EventHandler<TransactionCreatedEventArgs> OnTransactionCreated;
}

View file

@ -1,6 +1,7 @@
using Moonlight.App.Database.Entities;
using Moonlight.App.Database.Entities.Store;
using Moonlight.App.Event;
using Moonlight.App.Event.Args;
namespace Moonlight.App.Services.Background;
@ -14,6 +15,18 @@ public class AutoMailSendService // This service is responsible for sending mail
Events.OnUserRegistered += OnUserRegistered;
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)

View file

@ -1,5 +1,7 @@
using Moonlight.App.Database.Entities;
using Moonlight.App.Database.Entities.Store;
using Moonlight.App.Event;
using Moonlight.App.Extensions;
using Moonlight.App.Helpers;
using Moonlight.App.Repositories;
@ -14,16 +16,17 @@ public class TransactionService
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
user.Transactions.Add(new Transaction()
var transaction = new Transaction()
{
Text = message,
Price = amount
});
};
user.Transactions.Add(transaction);
UserRepository.Update(user);
// We divide the call to ensure the transaction can be written to the database
@ -33,6 +36,10 @@ public class TransactionService
UserRepository.Update(user);
return Task.CompletedTask;
await Events.OnTransactionCreated.InvokeAsync(new()
{
Transaction = transaction,
User = user
});
}
}

View file

@ -1,3 +1,3 @@
@page "/admin/store"
<AdminStoreNavigation Index="0" />
<AdminStoreNavigation Index="0"/>