This commit is contained in:
2023-07-23 20:27:23 +03:00
parent d0cdaa85b6
commit 96eca795ab
62 changed files with 2934 additions and 847 deletions
@@ -1,9 +1,7 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Models.Accounts;
using MyOffice.Data.Models.Currencies;
using MyOffice.Data.Repositories;
public class AccountRepository : AppRepository<Account>, IAccountRepository
@@ -16,6 +14,7 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Include(x => x.Motions)!
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
}
@@ -28,6 +27,7 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Include(x => x.Motions)!
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
}
@@ -73,6 +73,11 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
return AddBase(account) > 0;
}
public bool Delete(Account account)
{
return RemoveBase(account) > 0;
}
public Account? Get(Guid userId, Guid id)
{
return _context.Accounts!
@@ -80,6 +85,7 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Include(x => x.Motions)!
.FirstOrDefault(x => x.Id == id && x.AccessRights!.Any(r => r.UserId == userId));
}
@@ -141,27 +147,42 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public List<AccountSimple> GetRestAtDate(Guid userId, DateTime date)
{
return _context.Accounts
.Include(x => x.CurrencyGlobal!)
return _context.AccountAccesses!
.Where(x => x.UserId == userId)
.Include(x => x.Account!)
.ThenInclude(x => x.CurrencyGlobal!)
.ThenInclude(x => x.Currencies!)
.ThenInclude(x => x.CurrentRate!)
.Where(x => x.AccessRights!.Any(u => u.UserId == userId))
.Select(x => new {
Id = x.Id,
Name = x.Name,
Currency = x.CurrencyGlobal!.Currencies!.FirstOrDefault(x => x.UserId == userId),
Rest = x.Motions!.Sum(m => (m.AmountPlus - m.AmountMinus))
.Include(x => x.Account)
.ThenInclude(x => x!.Motions)
.Select(x => new
{
Id = x.AccountId,
Name = x.Account!.Name,
Type = x.Type,
Currency = x.Account!.CurrencyGlobal!.Currencies!.FirstOrDefault(c => c.UserId == userId),
CurrentRate = x.Account!.CurrencyGlobal!.Currencies!.FirstOrDefault(c => c.UserId == userId)!.CurrentRate,
Plus = x.Account.Motions!
.Where(x => !x.DeletedOn.HasValue)
.Sum(m => m.AmountPlus),
Minus = x.Account.Motions!
.Where(x => !x.DeletedOn.HasValue)
.Sum(m => m.AmountMinus),
})
.ToList()
.Select(x => new AccountSimple
{
Id = x.Id,
Name = x.Name,
Balance = x.Rest,
CurrencyName = x.Currency?.Name,
CurrencyShortName = x.Currency?.ShortName,
CurrencyRate = x.Currency?.CurrentRate?.Rate,
CurrencyQuantity = x.Currency?.CurrentRate?.Quantity,
}).ToList();
Type = x.Type,
CurrencyName = x.Currency!.Name,
CurrencyShortName = x.Currency!.ShortName,
CurrencyRate = x.CurrentRate?.Rate,
CurrencyQuantity = x.CurrentRate?.Quantity,
TotalMinus = x.Minus,
TotalPlus = x.Plus,
Balance = x.Plus - x.Minus,
})
.ToList();
}
}