This commit is contained in:
2023-07-21 21:06:45 +03:00
parent 2f108bef4f
commit fa5a8a9001
37 changed files with 2319 additions and 28 deletions
@@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Models.Currencies;
using MyOffice.Data.Repositories;
public class AccountRepository : AppRepository<Account>, IAccountRepository
@@ -94,8 +95,49 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public List<Account> FindAccounts(Guid userId, string term)
{
return _context.Accounts
.Where(x => x.AccessRights.Any(a => a.UserId == userId))
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Name.Contains(term))
.ToList();
}
public decimal GetPeriodIncome(Guid userId, DateTime from, DateTime to)
{
return _context.Motions
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(x => x.AmountPlus);
}
public decimal GetPeriodOutcome(Guid userId, DateTime from, DateTime to)
{
return _context.Motions
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(x => x.AmountMinus);
}
public List<AccountSimple> GetRestAtDate(Guid userId, DateTime date)
{
return _context.Motions
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime <= date)
.GroupBy(x => new
{
x.AccountId,
x.Account.Name,
CurrencyId = x.Account!.CurrencyGlobalId,
CurrencyName = x.Account!.CurrencyGlobal!.Name,
})
.Select(x => new AccountSimple
{
Id = x.Key.AccountId,
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyShortName = x.Key.CurrencyName,
Rest = x.Sum(g => (g.AmountPlus - g.AmountMinus))
})
.ToList();
}
}
@@ -13,4 +13,8 @@ public interface IAccountRepository
bool Update(Account account);
bool Remove(Account account);
List<Account> FindAccounts(Guid userId, string term);
decimal GetPeriodIncome(Guid userId, DateTime from, DateTime to);
decimal GetPeriodOutcome(Guid userId, DateTime from, DateTime to);
List<AccountSimple> GetRestAtDate(Guid userId, DateTime date);
}
@@ -1,5 +1,6 @@
namespace MyOffice.Data.Repositories.Currency;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRateRepository
@@ -14,6 +15,20 @@ public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRate
.ToList();
}
public List<CurrencyRate> GetLastRates(List<string> currencyIds)
{
return _context.CurrencyRates
.Include(x => x.Currency)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => new
{
x.CurrencyId,
//x.Currency!.Name,
//x.Currency!.CurrencyGlobalId,
}, (key, g) => g.OrderByDescending(x => x.DateTime).First())
.ToList();
}
public bool AddRate(CurrencyRate currencyRate)
{
return this.AddBase(currencyRate) > 0;
@@ -41,4 +41,9 @@ public class CurrencyRepository : AppRepository<Currency>, ICurrencyRepository
{
return RemoveBase(currency) > 0;
}
public List<Currency> GetPrimaries(Guid userId)
{
return _context.Currencies.Where(x => x.UserId == userId && x.IsPrimary).ToList();
}
}
@@ -5,6 +5,7 @@ using Models.Currencies;
public interface ICurrencyRateRepository
{
List<CurrencyRate> GetLastRates(Guid currencyId, int count = 1);
List<CurrencyRate> GetLastRates(List<string> currencyIds);
bool AddRate(CurrencyRate currencyRate);
List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date);
}
@@ -10,4 +10,5 @@ public interface ICurrencyRepository
bool Add(Currency currency);
bool Update(Currency currency);
bool Remove(Currency currency);
List<Currency> GetPrimaries(Guid userId);
}