Skip to content

Commit

Permalink
fix update account balance after add/edit transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
gwalus committed May 30, 2024
1 parent ff5c9cb commit a01b743
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ public partial class AddExpensePageViewModel(IUnitOfWork unitOfWork) : Observabl
{
private readonly IUnitOfWork _unitOfWork = unitOfWork;

[ObservableProperty]
private Account _selectedAccount;
public decimal Amount { get; set; }

private Account selectedAccount;
public Account SelectedAccount
{
get => selectedAccount;
set => SetProperty(ref selectedAccount, value);
}

public Category SelectedCategory { get; set; }
public DateTime Date { get; set; }
public string Comment { get; set; }
Expand All @@ -47,6 +41,8 @@ async Task AddExpense()
var selectedAccount = Accounts.FirstOrDefault(x => x.Id == SelectedAccount.Id);

selectedAccount.Amount -= Amount;

_unitOfWork.AccountRepository.Update(selectedAccount);
_unitOfWork.ExpensesRepository.Add(new Expense
{
Amount = Amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async Task EditExpense()
expense.Comment = Comment;

_unitOfWork.ExpensesRepository.Update(expense);
_unitOfWork.AccountRepository.Update(selectedAccount);
await _unitOfWork.CommitAsync();

await Toast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ public partial class AddIncomePageViewModel(IUnitOfWork unitOfWork) : Observable
private readonly IIncomesRepository _incomesRepository = unitOfWork.IncomesRepository;
private readonly IUnitOfWork _unitOfWork = unitOfWork;

public decimal Amount { get; set; }
[ObservableProperty]
private Account _selectedAccount;

private Account selectedAccount;
public Account SelectedAccount
{
get => selectedAccount;
set => SetProperty(ref selectedAccount, value);
}
public decimal Amount { get; set; }
public Category SelectedCategory { get; set; }
public DateTime Date { get; set; }
public string Comment { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,10 @@ async Task EditIncome()
var difference = Amount - initialAmount;

if (difference > 0)
{
selectedAccount.Amount += difference;
}

else if (difference < 0)
{
selectedAccount.Amount -= Math.Abs(difference);
}

var income = await _incomesRepository.GetAsync(id);

Expand All @@ -100,6 +97,7 @@ async Task EditIncome()
income.Comment = Comment;

_incomesRepository.Update(income);
_unitOfWork.AccountRepository.Update(selectedAccount);
await _unitOfWork.CommitAsync();

await Toast
Expand Down

0 comments on commit a01b743

Please sign in to comment.