public async Task <TotalEarningForCustomerInMonth> CalculateAndSaveCustomerEarningsForMonthAsync(int customerId, Month month) { var customer = await _customerReadRepository.GetAsync(customerId); var earnings = customer.Earnings.Where(x => x.MonthInYear == month).Sum(s => s.Value); var result = new TotalEarningForCustomerInMonth { CustomerId = customer.Id, Month = month, Name = customer.Name, TotalEarning = earnings }; await _totalEarningWriteRepository.AddAsync(result); return(result); }
public async Task CalculateCustomerEarningsForMonthAsync(int customerId, Month month, Func <int, Task <Customer> > read, Func <TotalEarningForCustomerInMonth, Task> write, Action <TotalEarningForCustomerInMonth> returnValue) { var customer = await read?.Invoke(customerId); var earnings = customer.Earnings.Where(x => x.MonthInYear == month).Sum(s => s.Value); var result = new TotalEarningForCustomerInMonth { CustomerId = customer.Id, Month = month, Name = customer.Name, TotalEarning = earnings }; await write?.Invoke(result); returnValue?.Invoke(result); }