I am creating a web app using .Net8 Blazor Server Side that can create a transaction and every transaction should deduct the balance.
What happened is that when I Add a Transaction:
private async void Submit()
{
isVisible = true;
await Task.Delay(1000);
Transaction!.UserID = selectedUser?.id;
Transaction!.DOCUMENT_NUM = DocumentNumber;
Transaction.CREATED_DATE = DateValue;
Transaction.PARTICULARS = Particulars;
Transaction.AMOUNT = Amount;
if (Amount > selectedUser!.Balance)
{
Snackbar.Add("User does not have enough balance.", Severity.Error);
return;
}
Balance!.UserID = selectedUser?.id;
Balance.Balance -= Amount;
var tran = await tranService.AddTransactionAsync(Transaction);
var bal = await balanceService.UpdateBalance(Balance);
Snackbar.Add("Transaction added successfully", Severity.Success);
await vwUserService.GetAllUsers();
Transaction = new();
selectedUser = new();
await GenerateDocumentNumberAsync();
Amount = 0;
Particulars = string.Empty;
users = new();
users = await vwUserService.GetAllUsers();
Balance = new();
isVisible = false;
StateHasChanged();
return;
}
this will deduct the balance.
But in my Edit Transaction:
async Task UpdateTransaction()
{
Transaction.ID = VwTransaction.ID;
Transaction.CREATED_DATE = (DateTime)VwTransaction.CREATED_DATE;
Transaction.DOCUMENT_NUM = VwTransaction.DOCUMENT_NUM;
Transaction.UserID = VwTransaction.USERID;
Transaction.PARTICULARS = VwTransaction.PARTICULARS;
Transaction.AMOUNT = VwTransaction.AMOUNT;
var updated = await tranService.UpdateTransaction(Transaction);
if(updated is not null)
{
await Task.Delay(1000);
Balance!.UserID = VwTransaction?.USERID;
Balance.Balance -= Transaction.AMOUNT;
await balanceService.UpdateBalance(Balance);
Snackbar.Add("Transaction updated successfully", Severity.Success);
return;
}
Snackbar.Add("Error Encountered", Severity.Error);
}
What I want to do is this:
- Let’s say I have a balance of 5,000.00.
- I added a transaction of 1,000.00.
- So the balance should be 4,000.00, since 5,000.00 – 1,000.00 is 4,000.00.
- But then I made a mistake in adding the transaction.
- Instead of 1,000.00 the correct amount in adding the transaction should be 1,200.00.
- So i try to edit the transaction and changed 1,000.00 to 1,200.00.
- After editing the transaction, it should be 5,000.00 – 1,200.00 = 3,800.00
I am having trouble adding the 200.00 to the 1,000.00 and deducting the original balance and likewise, if I want to edit the added amount to something that is lower than 1,000.00 and deduct it to the original balance.
But with the code I have shown, it deducts twice. (1) when I add a transaction. (2) When I edit the transaction. The problem is in my Edit Transaction and I don’t know how to do it.