This commit is contained in:
2023-08-04 17:53:15 +03:00
parent cf89dd6a4d
commit 70edf34cf6
17 changed files with 236 additions and 86 deletions
@@ -11,6 +11,7 @@ using Core.Extensions;
using Data.Repositories.Account;
using Data.Repositories.Currency;
using MyOffice.Data.Models.Accounts;
using System.Xml.Linq;
public class DashboardService
{
@@ -70,13 +71,44 @@ public class DashboardService
? _accountRepository.GetIncomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
: _accountRepository.GetIncomeByCategories(userId, from.ToUtc(), to.ToUtc());
data = data
.Where(x => x.Amount != 0)
.ToList();
var currencies = data
.Where(x => x.Amount != 0)
.Select(x => x.CurrencyId)
.Distinct()
.ToList();
var rates = _currencyRateRepository
.GetLastRates(userId, currencies, to.ToUtc())
.ToDictionary(x => x.Key, x => x.Value.Rate * x.Value.Quantity);
return new DashboardIncomeData
{
Data = data.Select(x => new DashboardIncomeDataItem
Data = data.Select(x => new
{
Id = x.Id?.ToShort(),
Id = x.Id.ToShort(),
Name = x.Name,
Value = x.Amount,
Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
})
.GroupBy(x => new { x.Id, x.Name })
.Select( x => new DashboardIncomeDataItem
{
Id = x.Key.Id,
Name = x.Key.Name,
Value = x.Sum(s => s.Value),
})
.ToList(),
Details = data.Select(x => new DashboardIncomeDataItem
{
Id = x.Id.ToShort(),
Currency = x.CurrencyId,
Name = x.Name,
ValueRaw = x.Amount,
Value = (x.Amount * rates.FirstOrDefault(r => r.Key == x.CurrencyId).Value),
}).ToList()
};
}
@@ -87,6 +119,12 @@ public class DashboardService
? _accountRepository.GetOutcomeByCategory(userId, category.Value, from.ToUtc(), to.ToUtc())
: _accountRepository.GetOutcomeByCategories(userId, from.ToUtc(), to.ToUtc());
var currencies = data
.Where(x => x.Amount != 0)
.Select(x => x.CurrencyId)
.Distinct()
.ToList();
return new DashboardIncomeData
{
Data = data.Select(x => new DashboardIncomeDataItem
@@ -32,11 +32,14 @@ public class DashboardRestData
public class DashboardIncomeData
{
public List<DashboardIncomeDataItem> Data { get; set; } = null!;
public List<DashboardIncomeDataItem> Details { get; set; } = null!;
}
public class DashboardIncomeDataItem
{
public string? Id { get; set; } = null!;
public string? Id { get; set; }
public string Currency { get; set; } = null!;
public string Name { get; set; } = null!;
public decimal Value { get; set; }
public decimal ValueRaw { get; set; }
}