public async Task OptimizeForSettling(long groupId, long creditorId, long debtorId) { DebtMatrix debtMatrix = await LoadDebtMatrix(groupId); debtMatrix.SetDebt(debtorId, creditorId, 0); debtMatrix.Optimize(); await SaveDebtMatrix(groupId, debtMatrix); }
public async Task OptimizeForNewSpending(long userId, NewSpending newSpending) { DebtMatrix debtMatrix = await LoadDebtMatrix(newSpending.GroupId); foreach (var debtor in newSpending.Debtors) { debtMatrix.UpdateDebt(debtor.DebtorId, userId, debtor.Debt); } debtMatrix.Optimize(); await SaveDebtMatrix(newSpending.GroupId, debtMatrix); }
public async Task OptimizeForUpdateSpending(long groupId, long creditorId, Dictionary <long, long> oldDebts, Dictionary <long, long> newDebts) { DebtMatrix debtMatrix = await LoadDebtMatrix(groupId); Dictionary <long, long> deltaDebts = CalculateDeltaDebts(oldDebts, newDebts); foreach (var deltaDebt in deltaDebts) { debtMatrix.UpdateDebt(deltaDebt.Key, creditorId, deltaDebt.Value); } debtMatrix.Optimize(); await SaveDebtMatrix(groupId, debtMatrix); }
private async Task OptimizeHelper(long groupId) { var daoGroup = await Context.Groups .Include(x => x.Members).ThenInclude(x => x.User) .Include(x => x.CreatorUser) .SingleOrDefaultAsync(x => x.Id == groupId); var daoSpendings = await Context.Spendings .Include(x => x.Creditor) .Include(x => x.Debtors).ThenInclude(x => x.Debtor) .Where(x => x.GroupId == groupId) .ToListAsync(); var daoSettlements = Context.Settlements .Where(x => x.GroupId == groupId); var userIds = daoGroup.Members.Select(x => x.UserId).ToArray(); DebtMatrix debtMatrix = new DebtMatrix(userIds); foreach (var userId in userIds) { var userSpendings = daoSpendings.Where(x => x.CreditorUserId == userId); foreach (var userSpending in userSpendings) { foreach (var daoDebtor in userSpending.Debtors) { debtMatrix.UpdateDebt(daoDebtor.DebtorUserId, userId, daoDebtor.Debt); } } } foreach (var daoSettlement in daoSettlements) { if (daoGroup.Members.Any(x => x.UserId == daoSettlement.From) && daoGroup.Members.Any(x => x.UserId == daoSettlement.To)) { debtMatrix.UpdateDebt(daoSettlement.To, daoSettlement.From, daoSettlement.Amount); } } debtMatrix.Optimize(); await SaveDebtMatrix(groupId, debtMatrix); }
private async Task <DebtMatrix> LoadDebtMatrix(long groupId) { var daoGroup = await Context.Groups .SingleOrDefaultAsync(x => x.Id == groupId); var userIds = daoGroup.Members.Select(x => x.UserId).ToArray(); DebtMatrix debtMatrix = new DebtMatrix(userIds); var optimizedDebts = await Context.OptimizedDebt .Where(x => x.GroupId == groupId) .ToListAsync(); foreach (var optimizedDebt in optimizedDebts) { debtMatrix.UpdateDebt(optimizedDebt.UserOwesId, optimizedDebt.UserOwedId, optimizedDebt.OweAmount); } return(debtMatrix); }
private async Task SaveDebtMatrix(long groupId, DebtMatrix debtMatrix) { var oldOptimizedDebts = await Context.OptimizedDebt .Where(x => x.GroupId == groupId) .ToListAsync(); Context.OptimizedDebt.RemoveRange(oldOptimizedDebts); IList <DebtMatrix.OptimizedDebt> optimizedDebts = debtMatrix.GetOptimizedDebts(); IList <DaoOptimizedDebt> daoOptimizedDebts = optimizedDebts.Select(x => new DaoOptimizedDebt() { GroupId = groupId, UserOwesId = x.debtorId, UserOwedId = x.creditorId, OweAmount = x.amount }).ToList(); await Context.AddRangeAsync(daoOptimizedDebts); }