Publish from private repository

This commit is contained in:
Gitea Actions
2026-08-01 11:48:55 +00:00
commit 814c39f5ce
617 changed files with 65073 additions and 0 deletions
@@ -0,0 +1,81 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Repositories;
using MyOffice.DbContext;
public class AccountAccessInviteRepository : AppRepository<AccountAccessInvite>, IAccountAccessInviteRepository
{
public AccountAccessInviteRepository(AppDbContext context) : base(context)
{
}
public AccountAccessInvite? Get(Guid userId, string email)
{
return _context.AccountAccessInvites
.FirstOrDefault(x => x.UserId == userId
&& x.Email == email
&& !x.AcceptedOn.HasValue
&& !x.RejectedOn.HasValue
);
}
public async Task<AccountAccessInvite?> GetAsync(Guid userId, string email, CancellationToken cancellationToken = default)
{
return await _context.AccountAccessInvites
.FirstOrDefaultAsync(x => x.UserId == userId
&& x.Email == email
&& !x.AcceptedOn.HasValue
&& !x.RejectedOn.HasValue,
cancellationToken);
}
public bool Add(AccountAccessInvite invite)
{
return AddBase(invite) > 0;
}
public async Task<bool> AddAsync(AccountAccessInvite invite, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(invite, cancellationToken) > 0;
}
public bool Update(AccountAccessInvite invite)
{
return UpdateBase(invite) > 0;
}
public async Task<bool> UpdateAsync(AccountAccessInvite invite, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(invite, cancellationToken) > 0;
}
public IEnumerable<AccountAccessInvite> GetActive(string email)
{
return _context
.AccountAccessInvites
.Include(x => x.Account)
.Where(x => x.Email == email && !x.AcceptedOn.HasValue && !x.RejectedOn.HasValue);
}
public async Task<List<AccountAccessInvite>> GetActiveAsync(string email, CancellationToken cancellationToken = default)
{
return await _context
.AccountAccessInvites
.Include(x => x.Account)
.Where(x => x.Email == email && !x.AcceptedOn.HasValue && !x.RejectedOn.HasValue)
.ToListAsync(cancellationToken);
}
public AccountAccessInvite? Get(Guid id)
{
return _context.AccountAccessInvites.FirstOrDefault(x => x.Id == id);
}
public async Task<AccountAccessInvite?> GetAsync(Guid id, CancellationToken cancellationToken = default)
{
return await _context.AccountAccessInvites.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
}
}
@@ -0,0 +1,42 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
using MyOffice.Data.Repositories;
using MyOffice.DbContext;
public class AccountAccessRepository : AppRepository<AccountAccess>, IAccountAccessRepository
{
public AccountAccessRepository(AppDbContext context) : base(context)
{
}
public bool Add(AccountAccess accountAccess)
{
return AddBase(accountAccess) > 0;
}
public async Task<bool> AddAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(accountAccess, cancellationToken) > 0;
}
public bool Update(AccountAccess accountAccess)
{
return UpdateBase(accountAccess) > 0;
}
public async Task<bool> UpdateAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(accountAccess, cancellationToken) > 0;
}
public bool Delete(AccountAccess accountAccess)
{
return RemoveBase(accountAccess) > 0;
}
public async Task<bool> DeleteAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(accountAccess, cancellationToken) > 0;
}
}
@@ -0,0 +1,55 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.DbContext;
public class AccountAccountCategoryRepository : AppRepository<AccountAccountCategory>, IAccountAccountCategoryRepository
{
public AccountAccountCategoryRepository(AppDbContext context) : base(context)
{
}
public List<AccountAccountCategory> Get(Guid userId, Guid accountId, Guid categoryId)
{
return _context.AccountAccountCategories
.Include(x => x.Account)
.Include(x => x.Category)
.Where(x => x.AccountId == accountId && x.CategoryId == categoryId && x.Category.UserId == userId)
.ToList();
}
public async Task<List<AccountAccountCategory>> GetAsync(
Guid userId,
Guid accountId,
Guid categoryId,
CancellationToken cancellationToken = default
)
{
return await _context.AccountAccountCategories
.Include(x => x.Account)
.Include(x => x.Category)
.Where(x => x.AccountId == accountId && x.CategoryId == categoryId && x.Category.UserId == userId)
.ToListAsync(cancellationToken);
}
public bool Remove(AccountAccountCategory accountAccountCategory)
{
return RemoveBase(accountAccountCategory) > 0;
}
public async Task<bool> RemoveAsync(AccountAccountCategory accountAccountCategory, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(accountAccountCategory, cancellationToken) > 0;
}
public bool Add(AccountAccountCategory accountAccountCategory)
{
return AddBase(accountAccountCategory) > 0;
}
public async Task<bool> AddAsync(AccountAccountCategory accountAccountCategory, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(accountAccountCategory, cancellationToken) > 0;
}
}
@@ -0,0 +1,73 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Repositories;
using MyOffice.DbContext;
public class AccountCategoryRepository : AppRepository<AccountCategory>, IAccountCategoryRepository
{
public AccountCategoryRepository(AppDbContext context) : base(context)
{
}
public List<AccountCategory> GetAll(Guid userId)
{
return _context.AccountCategories
.Include(x => x.Accounts)
.Where(x => x.UserId == userId)
.ToList();
}
public async Task<List<AccountCategory>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.AccountCategories
.Include(x => x.Accounts)
.Where(x => x.UserId == userId)
.ToListAsync(cancellationToken);
}
public bool Add(AccountCategory accountCategory)
{
return AddBase(accountCategory) > 0;
}
public async Task<bool> AddAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(accountCategory, cancellationToken) > 0;
}
public AccountCategory? Get(Guid userId, Guid id)
{
return _context.AccountCategories
.Include(x => x.Accounts)
.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public async Task<AccountCategory?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
{
return await _context.AccountCategories
.Include(x => x.Accounts)
.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, cancellationToken);
}
public bool Update(AccountCategory accountCategory)
{
return UpdateBase(accountCategory) > 0;
}
public async Task<bool> UpdateAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(accountCategory, cancellationToken) > 0;
}
public bool Remove(AccountCategory accountCategory)
{
return RemoveBase(accountCategory) > 0;
}
public async Task<bool> RemoveAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(accountCategory, cancellationToken) > 0;
}
}
@@ -0,0 +1,641 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Models.Accounts;
using MyOffice.Data.Models.Accounts.Domain;
using MyOffice.Data.Models.Currencies;
using MyOffice.Data.Repositories;
using MyOffice.DbContext;
public class AccountRepository : AppRepository<Account>, IAccountRepository
{
private readonly ILogger<AccountRepository> _logger;
public AccountRepository(
AppDbContext context,
ILogger<AccountRepository> logger
) : base(context)
{
_logger = logger;
}
public List<Account> GetAll(Guid userId)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
// categories created with current user
.Include(x => x.Categories!.Where(c => c.Category.UserId == userId))!
.ThenInclude(x => x.Category)
// access rights created with current user
.Include(x => x.AccessRights!.Where(a => a.OwnerId == userId))!
.ThenInclude(x => x.User)
// require only one motions to check if can to delete account
.Include(x => x.Motions!.Take(1))!
// only accounts with access to current user
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
}
public async Task<List<Account>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.Accounts!
.Include(x => x.CurrencyGlobal)
// categories created with current user
.Include(x => x.Categories!.Where(c => c.Category.UserId == userId))!
.ThenInclude(x => x.Category)
// access rights created with current user
.Include(x => x.AccessRights!.Where(a => a.OwnerId == userId))!
.ThenInclude(x => x.User)
// require only one motions to check if can to delete account
.Include(x => x.Motions!.Take(1))!
// only accounts with access to current user
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToListAsync(cancellationToken);
}
public List<Account> GetByCategory(Guid userId, Guid categoryId)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.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();
}
public async Task<List<Account>> GetByCategoryAsync(Guid userId, Guid categoryId, CancellationToken cancellationToken = default)
{
return await _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.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))
.ToListAsync(cancellationToken);
}
public List<AccountDetailed> GetByCategoryDetailed(Guid userId, Guid categoryId)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
.Select(x => new AccountDetailed
{
Account = x,
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
})
.ToList();
}
public Task<List<AccountDetailed>> GetByCategoryDetailedAsync(
Guid userId,
Guid categoryId,
CancellationToken cancellationToken = default
)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
.Select(x => new AccountDetailed
{
Account = x,
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
})
.ToListAsync(cancellationToken);
}
public AccountDetailed? GetByIdDetailed(Guid userId, Guid id)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Where(x => x.Id == id && x.AccessRights!.Any(a => a.UserId == userId))
.Select(x => new AccountDetailed
{
Account = x,
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
})
.FirstOrDefault();
}
public Task<AccountDetailed?> GetByIdDetailedAsync(
Guid userId,
Guid id,
CancellationToken cancellationToken = default
)
{
return _context.Accounts!
.Include(x => x.CurrencyGlobal)
.Include(x => x.Categories)!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.Where(x => x.Id == id && x.AccessRights!.Any(a => a.UserId == userId))
.Select(x => new AccountDetailed
{
Account = x,
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
})
.FirstOrDefaultAsync(cancellationToken);
}
public bool Add(Account account)
{
return AddBase(account) > 0;
}
public async Task<bool> AddAsync(Account account, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(account, cancellationToken) > 0;
}
public bool Delete(Account account)
{
return RemoveBase(account) > 0;
}
public async Task<bool> DeleteAsync(Account account, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(account, cancellationToken) > 0;
}
public Account? Get(Guid userId, Guid id)
{
return _context.Accounts!
.Include(x => x.Categories!.Where(c => c.Category.UserId == userId))!
.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));
}
public Task<Account?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
{
return _context.Accounts!
.Include(x => x.Categories!.Where(c => c.Category.UserId == userId))!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.FirstOrDefaultAsync(
x => x.Id == id && x.AccessRights!.Any(r => r.UserId == userId),
cancellationToken);
}
public bool Update(Account account)
{
return UpdateBase(account) > 0;
}
public async Task<bool> UpdateAsync(Account account, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(account, cancellationToken) > 0;
}
public bool Remove(Account account)
{
return RemoveBase(account) > 0;
}
public List<Account> FindAccounts(Guid userId, string term)
{
return _context.Accounts
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Name.ToLower().Contains(term.ToLower()))
.ToList();
}
public async Task<List<Account>> FindAccountsAsync(Guid userId, string term, CancellationToken cancellationToken = default)
{
return await _context.Accounts
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Name.ToLower().Contains(term.ToLower()))
.ToListAsync(cancellationToken);
}
public List<AccountWithRate> GetAccountsWithRate(Guid userId, DateTime date)
{
var accounts = _context.Accounts
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
var currencyIds = accounts
.Select(x => x.CurrencyGlobalId)
.ToList();
var rates = _context.CurrencyRates
.Include(x => x.Currency)
.Where(x => x.Currency!.UserId == userId)
.Where(x => x.DateTime <= date)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => x.Currency!.CurrencyGlobalId)
.Select(x => x.OrderByDescending(r => r.DateTime).First())
.ToList();
return accounts
.Select(account =>
{
var rate = rates.FirstOrDefault(rate => rate.Currency!.CurrencyGlobalId == account.CurrencyGlobalId);
var result = new AccountWithRate
{
Account = account,
Currency = rate?.Currency,
Rate = rate,
};
return result;
})
.ToList();
}
public List<AccountSimple> GetRestAtDate(Guid userId, DateTime date)
{
return _context.AccountAccesses!
.Where(x => x.UserId == userId)
.Include(x => x.Account!)
.ThenInclude(x => x.CurrencyGlobal!)
.ThenInclude(x => x.Currencies!)
.ThenInclude(x => x.CurrentRate!)
.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,
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();
}
public List<MotionTotalSimple> GetIncomeByCategories(Guid userId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.ToList()
.Select(x => new MotionTotalSimple
{
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountPlus),
})
.ToList();
}
public List<MotionTotalSimple> GetIncomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to)
{
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => x.Item.Category!.UserId == userId)
.Where(x => x.Item.CategoryId == categoryId)
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Item.Category!.UserId == userId)
.GroupBy(x => new
{
Id = x.Item.ItemGlobalId,
Name = x.Item.ItemGlobal!.Name,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountPlus),
})
.ToList();
}
public List<MotionTotalSimple> GetOutcomeByCategories(Guid userId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.IsUserCategory ? x.Key.Name : "UnCategorized",
//Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountMinus),
}).ToList();
}
public List<MotionTotalSimple> GetOutcomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Item.CategoryId == categoryId)
.GroupBy(x => new
{
Id = x.Item.ItemGlobalId,
Name = x.Item.ItemGlobal!.Name,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Name = x.Key.Name,
Amount = x.Sum(a => a.AmountMinus),
})
.ToList();
}
public async Task<List<AccountWithRate>> GetAccountsWithRateAsync(
Guid userId,
DateTime date,
CancellationToken cancellationToken = default
)
{
var accounts = await _context.Accounts
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToListAsync(cancellationToken);
var currencyIds = accounts
.Select(x => x.CurrencyGlobalId)
.ToList();
var rates = await _context.CurrencyRates
.Include(x => x.Currency)
.Where(x => x.Currency!.UserId == userId)
.Where(x => x.DateTime <= date)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => x.Currency!.CurrencyGlobalId)
.Select(x => x.OrderByDescending(r => r.DateTime).First())
.ToListAsync(cancellationToken);
return accounts
.Select(account =>
{
var rate = rates.FirstOrDefault(r => r.Currency!.CurrencyGlobalId == account.CurrencyGlobalId);
return new AccountWithRate
{
Account = account,
Currency = rate?.Currency,
Rate = rate,
};
})
.ToList();
}
public async Task<List<AccountSimple>> GetRestAtDateAsync(
Guid userId,
DateTime date,
CancellationToken cancellationToken = default
)
{
var rows = await _context.AccountAccesses!
.Where(x => x.UserId == userId)
.Include(x => x.Account!)
.ThenInclude(x => x.CurrencyGlobal!)
.ThenInclude(x => x.Currencies!)
.ThenInclude(x => x.CurrentRate!)
.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(m => !m.DeletedOn.HasValue)
.Sum(m => m.AmountPlus),
Minus = x.Account.Motions!
.Where(m => !m.DeletedOn.HasValue)
.Sum(m => m.AmountMinus),
})
.ToListAsync(cancellationToken);
return rows
.Select(x => new AccountSimple
{
Id = x.Id,
Name = x.Name,
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();
}
public async Task<List<MotionTotalSimple>> GetIncomeByCategoriesAsync(
Guid userId,
DateTime from,
DateTime to,
CancellationToken cancellationToken = default
)
{
var accounts = await GetAccountsWithRateAsync(userId, to, cancellationToken);
var accountIds = accounts.Select(x => x.Account.Id).ToList();
var groups = await _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.ToListAsync(cancellationToken);
return groups
.Select(x => new MotionTotalSimple
{
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountPlus),
})
.ToList();
}
public Task<List<MotionTotalSimple>> GetIncomeByCategoryAsync(
Guid userId,
Guid categoryId,
DateTime from,
DateTime to,
CancellationToken cancellationToken = default
)
{
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => x.Item.Category!.UserId == userId)
.Where(x => x.Item.CategoryId == categoryId)
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Item.Category!.UserId == userId)
.GroupBy(x => new
{
Id = x.Item.ItemGlobalId,
Name = x.Item.ItemGlobal!.Name,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountPlus),
})
.ToListAsync(cancellationToken);
}
public async Task<List<MotionTotalSimple>> GetOutcomeByCategoriesAsync(
Guid userId,
DateTime from,
DateTime to,
CancellationToken cancellationToken = default
)
{
var accounts = await GetAccountsWithRateAsync(userId, to, cancellationToken);
var accountIds = accounts.Select(x => x.Account.Id).ToList();
return await _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.IsUserCategory ? x.Key.Name : "UnCategorized",
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountMinus),
})
.ToListAsync(cancellationToken);
}
public async Task<List<MotionTotalSimple>> GetOutcomeByCategoryAsync(
Guid userId,
Guid categoryId,
DateTime from,
DateTime to,
CancellationToken cancellationToken = default
)
{
var accounts = await GetAccountsWithRateAsync(userId, to, cancellationToken);
var accountIds = accounts.Select(x => x.Account.Id).ToList();
return await _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Item.CategoryId == categoryId)
.GroupBy(x => new
{
Id = x.Item.ItemGlobalId,
Name = x.Item.ItemGlobal!.Name,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Name = x.Key.Name,
Amount = x.Sum(a => a.AmountMinus),
})
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,17 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountAccessInviteRepository
{
AccountAccessInvite? Get(Guid userId, string email);
Task<AccountAccessInvite?> GetAsync(Guid userId, string email, CancellationToken cancellationToken = default);
IEnumerable<AccountAccessInvite> GetActive(string email);
Task<List<AccountAccessInvite>> GetActiveAsync(string email, CancellationToken cancellationToken = default);
AccountAccessInvite? Get(Guid id);
Task<AccountAccessInvite?> GetAsync(Guid id, CancellationToken cancellationToken = default);
bool Add(AccountAccessInvite invite);
Task<bool> AddAsync(AccountAccessInvite invite, CancellationToken cancellationToken = default);
bool Update(AccountAccessInvite invite);
Task<bool> UpdateAsync(AccountAccessInvite invite, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,13 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountAccessRepository
{
bool Add(AccountAccess accountAccess);
Task<bool> AddAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default);
bool Update(AccountAccess accountAccess);
Task<bool> UpdateAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default);
bool Delete(AccountAccess accountAccess);
Task<bool> DeleteAsync(AccountAccess accountAccess, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,13 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountAccountCategoryRepository
{
List<AccountAccountCategory> Get(Guid userId, Guid accountId, Guid categoryId);
Task<List<AccountAccountCategory>> GetAsync(Guid userId, Guid accountId, Guid categoryId, CancellationToken cancellationToken = default);
bool Remove(AccountAccountCategory accountAccountCategory);
Task<bool> RemoveAsync(AccountAccountCategory accountAccountCategory, CancellationToken cancellationToken = default);
bool Add(AccountAccountCategory accountAccountCategory);
Task<bool> AddAsync(AccountAccountCategory accountAccountCategory, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,17 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountCategoryRepository
{
List<AccountCategory> GetAll(Guid userId);
Task<List<AccountCategory>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default);
bool Add(AccountCategory accountCategory);
Task<bool> AddAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default);
AccountCategory? Get(Guid userId, Guid id);
Task<AccountCategory?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
bool Update(AccountCategory accountCategory);
Task<bool> UpdateAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default);
bool Remove(AccountCategory accountCategory);
Task<bool> RemoveAsync(AccountCategory accountCategory, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,37 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountRepository
{
List<Account> GetAll(Guid userId);
Task<List<Account>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default);
List<Account> GetByCategory(Guid userId, Guid categoryId);
Task<List<Account>> GetByCategoryAsync(Guid userId, Guid categoryId, CancellationToken cancellationToken = default);
List<AccountDetailed> GetByCategoryDetailed(Guid userId, Guid categoryId);
Task<List<AccountDetailed>> GetByCategoryDetailedAsync(Guid userId, Guid categoryId, CancellationToken cancellationToken = default);
AccountDetailed? GetByIdDetailed(Guid userId, Guid id);
Task<AccountDetailed?> GetByIdDetailedAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
bool Add(Account account);
Task<bool> AddAsync(Account account, CancellationToken cancellationToken = default);
bool Delete(Account account);
Task<bool> DeleteAsync(Account account, CancellationToken cancellationToken = default);
Account? Get(Guid userId, Guid id);
Task<Account?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
bool Update(Account account);
Task<bool> UpdateAsync(Account account, CancellationToken cancellationToken = default);
bool Remove(Account account);
List<Account> FindAccounts(Guid userId, string term);
Task<List<Account>> FindAccountsAsync(Guid userId, string term, CancellationToken cancellationToken = default);
List<AccountSimple> GetRestAtDate(Guid userId, DateTime date);
Task<List<AccountSimple>> GetRestAtDateAsync(Guid userId, DateTime date, CancellationToken cancellationToken = default);
List<MotionTotalSimple> GetIncomeByCategories(Guid userId, DateTime from, DateTime to);
Task<List<MotionTotalSimple>> GetIncomeByCategoriesAsync(Guid userId, DateTime from, DateTime to, CancellationToken cancellationToken = default);
List<MotionTotalSimple> GetIncomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to);
Task<List<MotionTotalSimple>> GetIncomeByCategoryAsync(Guid userId, Guid categoryId, DateTime from, DateTime to, CancellationToken cancellationToken = default);
List<MotionTotalSimple> GetOutcomeByCategories(Guid userId, DateTime from, DateTime to);
Task<List<MotionTotalSimple>> GetOutcomeByCategoriesAsync(Guid userId, DateTime from, DateTime to, CancellationToken cancellationToken = default);
List<MotionTotalSimple> GetOutcomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to);
Task<List<MotionTotalSimple>> GetOutcomeByCategoryAsync(Guid userId, Guid categoryId, DateTime from, DateTime to, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,12 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IMotionRepository
{
Task<bool> AddAsync(Motion motion, CancellationToken cancellationToken = default);
Task<List<Motion>> GetByAccountAsync(Guid accountId, DateTime dateFrom, DateTime dateTo, CancellationToken cancellationToken = default);
Task<Motion?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
Task<bool> UpdateAsync(Motion motion, CancellationToken cancellationToken = default);
Task<bool> RemoveAsync(Motion motion, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,55 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.DbContext;
public class MotionRepository : AppRepository<Motion>, IMotionRepository
{
public MotionRepository(AppDbContext context) : base(context)
{
}
public async Task<bool> AddAsync(Motion motion, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(motion, cancellationToken) > 0;
}
public async Task<List<Motion>> GetByAccountAsync(
Guid accountId,
DateTime dateFrom,
DateTime dateTo,
CancellationToken cancellationToken = default
)
{
return await _context
.Motions
.Include(x => x.Item)
.ThenInclude(x => x.ItemGlobal)
.Where(x => x.AccountId == accountId && x.DateTime >= dateFrom && x.DateTime <= dateTo)
.OrderByDescending(x => x.DateTime)
.ThenByDescending(x => x.CreatedOn)
.ToListAsync(cancellationToken);
}
public async Task<Motion?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
{
return await _context
.Motions
.Include(x => x.Item)
.ThenInclude(x => x.ItemGlobal)
.FirstOrDefaultAsync(
x => x.Account!.AccessRights!.Any(a => a.UserId == userId) && x.Id == id,
cancellationToken);
}
public async Task<bool> UpdateAsync(Motion motion, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(motion, cancellationToken) > 0;
}
public async Task<bool> RemoveAsync(Motion motion, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(motion, cancellationToken) > 0;
}
}
@@ -0,0 +1,10 @@
namespace MyOffice.Data.Repositories;
using DbContext;
public class AppRepository<TEntity> : RepositoryBase<TEntity> where TEntity : class
{
public AppRepository(AppDbContext context) : base(context)
{
}
}
@@ -0,0 +1,22 @@
namespace MyOffice.Data.Repositories.Currency;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
using MyOffice.DbContext;
public class CurrencyGlobalRepository : AppRepository<CurrencyGlobal>, ICurrencyGlobalRepository
{
public CurrencyGlobalRepository(AppDbContext context) : base(context)
{
}
public List<CurrencyGlobal> GetAll()
{
return _context.CurrencyGlobals.ToList();
}
public async Task<List<CurrencyGlobal>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _context.CurrencyGlobals.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,115 @@
namespace MyOffice.Data.Repositories.Currency;
using System.Xml.Schema;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
using MyOffice.DbContext;
public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRateRepository
{
public CurrencyRateRepository(AppDbContext context) : base(context)
{
}
public List<CurrencyRate> GetLastRates(Guid currencyId, DateTime? before = null, int count = 1)
{
before = before ?? DateTime.UtcNow;
return _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId)
.Where(x => x.DateTime <= before)
.OrderByDescending(x => x.DateTime)
.ThenByDescending(x => x.Id)
.Take(count)
.ToList();
}
public async Task<List<CurrencyRate>> GetLastRatesAsync(
Guid currencyId,
DateTime? before = null,
int count = 1,
CancellationToken cancellationToken = default)
{
before ??= DateTime.UtcNow;
return await _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId)
.Where(x => x.DateTime <= before)
.OrderByDescending(x => x.DateTime)
.ThenByDescending(x => x.Id)
.Take(count)
.ToListAsync(cancellationToken);
}
public Dictionary<string, CurrencyRate> GetLastRates(Guid userId, List<string> currencyIds, DateTime? before = null)
{
return _context.CurrencyRates
.Include(x => x.Currency)
.Include(x => x.Currency!.CurrencyGlobal)
.Where(x => x.DateTime <= before)
.Where(x => x.Currency!.UserId == userId)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => new
{
x.Currency!.CurrencyGlobalId
}, (key, g) => new
{
key.CurrencyGlobalId,
rate = g.OrderByDescending(x => x.DateTime).First()
})
.ToDictionary(x => x.CurrencyGlobalId, x => x.rate);
}
public async Task<Dictionary<string, CurrencyRate>> GetLastRatesAsync(
Guid userId,
List<string> currencyIds,
DateTime? before = null,
CancellationToken cancellationToken = default
)
{
var rows = await _context.CurrencyRates
.Include(x => x.Currency)
.Include(x => x.Currency!.CurrencyGlobal)
.Where(x => x.DateTime <= before)
.Where(x => x.Currency!.UserId == userId)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => new
{
x.Currency!.CurrencyGlobalId
}, (key, g) => new
{
key.CurrencyGlobalId,
rate = g.OrderByDescending(x => x.DateTime).First()
})
.ToListAsync(cancellationToken);
return rows.ToDictionary(x => x.CurrencyGlobalId, x => x.rate);
}
public bool AddRate(CurrencyRate currencyRate)
{
return this.AddBase(currencyRate) > 0;
}
public async Task<bool> AddRateAsync(CurrencyRate currencyRate, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(currencyRate, cancellationToken) > 0;
}
public List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date)
{
return _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId && x.DateTime == date)
.ToList();
}
public async Task<List<CurrencyRate>> GetAtDateAsync(
Guid currencyId,
DateTime date,
CancellationToken cancellationToken = default)
{
return await _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId && x.DateTime == date)
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,102 @@
namespace MyOffice.Data.Repositories.Currency;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
using MyOffice.Data.Repositories;
using MyOffice.DbContext;
public class CurrencyRepository : AppRepository<Currency>, ICurrencyRepository
{
public CurrencyRepository(AppDbContext context) : base(context)
{
}
public List<Currency> GetAll(Guid userId)
{
return _context.Currencies
.Include(x => x.CurrencyGlobal)
.Where(x => x.UserId == userId)
.ToList();
}
public async Task<List<Currency>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.Currencies
.Include(x => x.CurrencyGlobal)
.Where(x => x.UserId == userId)
.ToListAsync(cancellationToken);
}
public Currency? Get(Guid userId, Guid id)
{
return _context
.Currencies
.Include(x => x.CurrencyGlobal)
.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public async Task<Currency?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
{
return await _context
.Currencies
.Include(x => x.CurrencyGlobal)
.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, cancellationToken);
}
public Currency? GetByGlobalCurrency(Guid userId, string globalCurrencyId)
{
return _context.Currencies.FirstOrDefault(x => x.UserId == userId && x.CurrencyGlobalId == globalCurrencyId);
}
public async Task<Currency?> GetByGlobalCurrencyAsync(
Guid userId,
string globalCurrencyId,
CancellationToken cancellationToken = default)
{
return await _context.Currencies.FirstOrDefaultAsync(
x => x.UserId == userId && x.CurrencyGlobalId == globalCurrencyId,
cancellationToken);
}
public bool Add(Currency currency)
{
return AddBase(currency) > 0;
}
public async Task<bool> AddAsync(Currency currency, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(currency, cancellationToken) > 0;
}
public bool Update(Currency currency)
{
return UpdateBase(currency) > 0;
}
public async Task<bool> UpdateAsync(Currency currency, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(currency, cancellationToken) > 0;
}
public bool Remove(Currency currency)
{
return RemoveBase(currency) > 0;
}
public async Task<bool> RemoveAsync(Currency currency, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(currency, cancellationToken) > 0;
}
public List<Currency> GetPrimaries(Guid userId)
{
return _context.Currencies.Where(x => x.UserId == userId && x.IsPrimary).ToList();
}
public async Task<List<Currency>> GetPrimariesAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.Currencies
.Where(x => x.UserId == userId && x.IsPrimary)
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,9 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyGlobalRepository
{
List<CurrencyGlobal> GetAll();
Task<List<CurrencyGlobal>> GetAllAsync(CancellationToken cancellationToken = default);
}
@@ -0,0 +1,19 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyRateRepository
{
List<CurrencyRate> GetLastRates(Guid currencyId, DateTime? before = null, int count = 1);
Task<List<CurrencyRate>> GetLastRatesAsync(Guid currencyId, DateTime? before = null, int count = 1, CancellationToken cancellationToken = default);
Dictionary<string, CurrencyRate> GetLastRates(Guid userId, List<string> currencyIds, DateTime? before = null);
Task<Dictionary<string, CurrencyRate>> GetLastRatesAsync(
Guid userId,
List<string> currencyIds,
DateTime? before = null,
CancellationToken cancellationToken = default);
bool AddRate(CurrencyRate currencyRate);
Task<bool> AddRateAsync(CurrencyRate currencyRate, CancellationToken cancellationToken = default);
List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date);
Task<List<CurrencyRate>> GetAtDateAsync(Guid currencyId, DateTime date, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,21 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyRepository
{
List<Currency> GetAll(Guid userId);
Task<List<Currency>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default);
Currency? Get(Guid userId, Guid id);
Task<Currency?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
Currency? GetByGlobalCurrency(Guid userId, string globalCurrencyId);
Task<Currency?> GetByGlobalCurrencyAsync(Guid userId, string globalCurrencyId, CancellationToken cancellationToken = default);
bool Add(Currency currency);
Task<bool> AddAsync(Currency currency, CancellationToken cancellationToken = default);
bool Update(Currency currency);
Task<bool> UpdateAsync(Currency currency, CancellationToken cancellationToken = default);
bool Remove(Currency currency);
Task<bool> RemoveAsync(Currency currency, CancellationToken cancellationToken = default);
List<Currency> GetPrimaries(Guid userId);
Task<List<Currency>> GetPrimariesAsync(Guid userId, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,17 @@
namespace MyOffice.Data.Repositories.Item;
using MyOffice.Data.Models.Items;
public interface IItemCategoryRepository
{
List<ItemCategory> GetAll(Guid userId);
Task<List<ItemCategory>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default);
bool Add(ItemCategory accountCategory);
Task<bool> AddAsync(ItemCategory accountCategory, CancellationToken cancellationToken = default);
ItemCategory? Get(Guid userId, Guid id);
Task<ItemCategory?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default);
bool Update(ItemCategory accountCategory);
Task<bool> UpdateAsync(ItemCategory accountCategory, CancellationToken cancellationToken = default);
bool Remove(ItemCategory accountCategory);
Task<bool> RemoveAsync(ItemCategory accountCategory, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,14 @@
namespace MyOffice.Data.Repositories.Item;
using MyOffice.Data.Models.Items;
public interface IItemGlobalRepository
{
ItemGlobal? Get(Guid id);
ItemGlobal? GetByName(string name);
Task<ItemGlobal?> GetByNameAsync(string name, CancellationToken cancellationToken = default);
bool Add(ItemGlobal itemGlobal);
Task<bool> AddAsync(ItemGlobal itemGlobal, CancellationToken cancellationToken = default);
List<ItemGlobal> GetAvailableToUser(Guid userId);
Task<List<ItemGlobal>> GetAvailableToUserAsync(Guid userId, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,19 @@
namespace MyOffice.Data.Repositories.Item;
using MyOffice.Data.Models.Items;
public interface IItemRepository
{
List<Item> GetAll(Guid userId);
Task<List<Item>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default);
List<Item> GetByCategory(Guid userId, Guid categoryId);
Task<List<Item>> GetByCategoryAsync(Guid userId, Guid categoryId, CancellationToken cancellationToken = default);
Item? GetByGlobal(Guid userId, Guid globalItemId);
Task<Item?> GetByGlobalAsync(Guid userId, Guid globalItemId, CancellationToken cancellationToken = default);
bool Add(Item item);
Task<bool> AddAsync(Item item, CancellationToken cancellationToken = default);
bool Update(Item item);
Task<bool> UpdateAsync(Item item, CancellationToken cancellationToken = default);
List<Item> Find(Guid userId, string term, int limit);
Task<List<Item>> FindAsync(Guid userId, string term, int limit, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,72 @@
namespace MyOffice.Data.Repositories.Item;
using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Items;
using MyOffice.DbContext;
public class ItemCategoryRepository : AppRepository<ItemCategory>, IItemCategoryRepository
{
public ItemCategoryRepository(AppDbContext context) : base(context)
{
}
public List<ItemCategory> GetAll(Guid userId)
{
return _context.ItemCategories
.Include(x => x.Items)
.Where(x => x.UserId == userId)
.ToList();
}
public async Task<List<ItemCategory>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.ItemCategories
.Include(x => x.Items)
.Where(x => x.UserId == userId)
.ToListAsync(cancellationToken);
}
public bool Add(ItemCategory itemCategory)
{
return AddBase(itemCategory) > 0;
}
public async Task<bool> AddAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(itemCategory, cancellationToken) > 0;
}
public ItemCategory? Get(Guid userId, Guid id)
{
return _context.ItemCategories
.Include(x => x.Items)
.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public async Task<ItemCategory?> GetAsync(Guid userId, Guid id, CancellationToken cancellationToken = default)
{
return await _context.ItemCategories
.Include(x => x.Items)
.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, cancellationToken);
}
public bool Update(ItemCategory itemCategory)
{
return UpdateBase(itemCategory) > 0;
}
public async Task<bool> UpdateAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(itemCategory, cancellationToken) > 0;
}
public bool Remove(ItemCategory itemCategory)
{
return RemoveBase(itemCategory) > 0;
}
public async Task<bool> RemoveAsync(ItemCategory itemCategory, CancellationToken cancellationToken = default)
{
return await RemoveBaseAsync(itemCategory, cancellationToken) > 0;
}
}
@@ -0,0 +1,69 @@
namespace MyOffice.Data.Repositories.Item;
using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Items;
using MyOffice.DbContext;
public class ItemGlobalRepository : AppRepository<ItemGlobal>, IItemGlobalRepository
{
public ItemGlobalRepository(AppDbContext context) : base(context)
{
}
public ItemGlobal? Get(Guid id)
{
return _context.ItemGlobals.FirstOrDefault(x => x.Id == id);
}
public ItemGlobal? GetByName(string name)
{
return _context.ItemGlobals.FirstOrDefault(x => x.Name == name);
}
public Task<ItemGlobal?> GetByNameAsync(string name, CancellationToken cancellationToken = default)
{
return _context.ItemGlobals.FirstOrDefaultAsync(x => x.Name == name, cancellationToken);
}
public bool Add(ItemGlobal itemGlobal)
{
return AddBase(itemGlobal) > 0;
}
public async Task<bool> AddAsync(ItemGlobal itemGlobal, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(itemGlobal, cancellationToken) > 0;
}
public List<ItemGlobal> GetAvailableToUser(Guid userId)
{
return _context.Motions
.Include(x => x.Item)
.ThenInclude(x => x.ItemGlobal)
.ThenInclude(x => x.Items)
.ThenInclude(x => x.Category)
.Where(x => x.Account.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Item.ItemGlobal.Items.All(g => g.Category!.UserId != userId))
.Select(x => x.Item.ItemGlobal)
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
}
public async Task<List<ItemGlobal>> GetAvailableToUserAsync(
Guid userId,
CancellationToken cancellationToken = default)
{
return await _context.Motions
.Include(x => x.Item)
.ThenInclude(x => x.ItemGlobal)
.ThenInclude(x => x.Items)
.ThenInclude(x => x.Category)
.Where(x => x.Account.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Item.ItemGlobal.Items.All(g => g.Category!.UserId != userId))
.Select(x => x.Item.ItemGlobal)
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,121 @@
namespace MyOffice.Data.Repositories.Item;
using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Items;
using MyOffice.DbContext;
public class ItemRepository : AppRepository<Item>, IItemRepository
{
public ItemRepository(AppDbContext context) : base(context)
{
}
public List<Item> GetAll(Guid userId)
{
return _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId)
.ToList();
}
public async Task<List<Item>> GetAllAsync(Guid userId, CancellationToken cancellationToken = default)
{
return await _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId)
.ToListAsync(cancellationToken);
}
public List<Item> GetByCategory(Guid userId, Guid categoryId)
{
return _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId && x.CategoryId == categoryId)
.ToList();
}
public async Task<List<Item>> GetByCategoryAsync(
Guid userId,
Guid categoryId,
CancellationToken cancellationToken = default)
{
return await _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId && x.CategoryId == categoryId)
.ToListAsync(cancellationToken);
}
public Item? GetByGlobal(Guid userId, Guid globalItemId)
{
return _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.FirstOrDefault(x => x.Category!.UserId == userId && x.ItemGlobalId == globalItemId);
}
public Task<Item?> GetByGlobalAsync(Guid userId, Guid globalItemId, CancellationToken cancellationToken = default)
{
return _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.FirstOrDefaultAsync(
x => x.Category!.UserId == userId && x.ItemGlobalId == globalItemId,
cancellationToken);
}
public bool Update(Item item)
{
return base.UpdateBase(item) > 0;
}
public async Task<bool> UpdateAsync(Item item, CancellationToken cancellationToken = default)
{
return await UpdateBaseAsync(item, cancellationToken) > 0;
}
public bool Add(Item item)
{
return base.AddBase(item) > 0;
}
public async Task<bool> AddAsync(Item item, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(item, cancellationToken) > 0;
}
public List<Item> Find(Guid userId, string term, int limit)
{
return _context
.Items
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId && x.ItemGlobal.Name.ToLower().Contains(term.ToLower()))
.OrderByDescending(x => x!.Motions!.Count())
.Take(limit)
.ToList();
}
public async Task<List<Item>> FindAsync(
Guid userId,
string term,
int limit,
CancellationToken cancellationToken = default)
{
return await _context
.Items
.Include(x => x.ItemGlobal)
.Where(x => x.Category!.UserId == userId && x.ItemGlobal.Name.ToLower().Contains(term.ToLower()))
.OrderByDescending(x => x!.Motions!.Count())
.Take(limit)
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="AppDbContext.cs" />
<Compile Remove="AppDbContextFactory.cs" />
<Compile Remove="RepositoryInitializer.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyOffice.Data.Models\MyOffice.Data.Models.csproj" />
<ProjectReference Include="..\MyOffice.DbContext\MyOffice.DbContext.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,17 @@
namespace MyOffice.Data.Repositories.Item;
using MyOffice.Data.Models.Notifications;
using MyOffice.DbContext;
public class EmailTemplateRepository : AppRepository<EmailTemplate>, IEmailTemplateRepository
{
public EmailTemplateRepository(AppDbContext context) : base(context)
{
}
public EmailTemplate? Get(EmailTemplateEnum emailTemplate)
{
var emailTemplateStr = emailTemplate.ToString();
return _context.EmailTemplates.FirstOrDefault(x => x.Id == emailTemplateStr);
}
}
@@ -0,0 +1,8 @@
namespace MyOffice.Data.Repositories.Item;
using MyOffice.Data.Models.Notifications;
public interface IEmailTemplateRepository
{
EmailTemplate? Get(EmailTemplateEnum emailTemplate);
}
@@ -0,0 +1,108 @@
namespace MyOffice.Data.Repositories;
using Microsoft.EntityFrameworkCore;
using DbContext;
public class RepositoryBase<TEntity> where TEntity : class
{
protected readonly AppDbContext _context;
protected RepositoryBase(
AppDbContext context
)
{
_context = context;
}
protected async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
try
{
return await _context.SaveChangesAsync(cancellationToken);
}
catch (DbUpdateException updateException)
{
throw updateException;
}
}
protected int SaveChanges()
{
try
{
return _context.SaveChanges();
}
catch (DbUpdateException updateException)
{
throw updateException;
}
}
protected async Task<TEntity?> GetBaseAsync(Guid id)
{
return await _context.Set<TEntity>().FindAsync(id);
}
protected TEntity? GetBase(Guid id)
{
return _context.Set<TEntity>().Find(id);
}
protected int AddBase(TEntity entity)
{
_context.Entry(entity).State = EntityState.Added;
var result = SaveChanges();
_context.Entry(entity).State = EntityState.Detached;
return result;
}
protected async Task<int> AddBaseAsync(TEntity entity, CancellationToken cancellationToken = default)
{
_context.Entry(entity).State = EntityState.Added;
var result = await SaveChangesAsync(cancellationToken);
_context.Entry(entity).State = EntityState.Detached;
return result;
}
protected int RemoveBase(TEntity entity)
{
_context.Set<TEntity>().Remove(entity);
_context.Entry(entity).State = EntityState.Deleted;
var result = SaveChanges();
_context.Entry(entity).State = EntityState.Detached;
return result;
}
protected async Task<int> RemoveBaseAsync(TEntity entity, CancellationToken cancellationToken = default)
{
_context.Set<TEntity>().Remove(entity);
_context.Entry(entity).State = EntityState.Deleted;
var result = await SaveChangesAsync(cancellationToken);
_context.Entry(entity).State = EntityState.Detached;
return result;
}
protected int UpdateBase(TEntity entity)
{
_context.Entry(entity).State = EntityState.Modified;
var result = SaveChanges();
_context.Entry(entity).State = EntityState.Detached;
return result;
}
protected async Task<int> UpdateBaseAsync(TEntity entity, CancellationToken cancellationToken = default)
{
_context.Entry(entity).State = EntityState.Modified;
var result = await SaveChangesAsync(cancellationToken);
_context.Entry(entity).State = EntityState.Detached;
return result;
}
}
@@ -0,0 +1,20 @@
namespace MyOffice.Data.Repositories.Users;
using Models.Users;
public interface IUserExternalRepository
{
int AddUserExternal(UserExternal external);
Task<int> AddUserExternalAsync(UserExternal external, CancellationToken cancellationToken = default);
Task<List<UserExternal>> GetUserExternalsByUserIdAsync(Guid userId);
Task<UserExternal?> GetByUserIdAsync(Guid userId, string provider);
UserExternal? GetByUserId(Guid userId, string provider);
UserExternal? GetByExternalId(string externalId, string provider);
Task<UserExternal?> GetByExternalIdAsync(string externalId, string provider);
void RemoveUserExternal(UserExternal userExternal);
Task RemoveUserExternalAsync(UserExternal userExternal, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,15 @@
namespace MyOffice.Data.Repositories.Users;
using MyOffice.Data.Models.Users;
public interface IUserRepository
{
Task<User?> GetUserAsync(Guid id);
User? GetUser(Guid id);
Task<User?> GetByUserUserNameAsync(string userName);
User? GetByUserUserName(string userName);
int AddUser(User user);
int UpdateUser(User user);
}
@@ -0,0 +1,108 @@
namespace MyOffice.Data.Repositories.Users;
using MyOffice.Data.Models.Users;
using Repositories;
using Microsoft.EntityFrameworkCore;
using MyOffice.DbContext;
public class UserExternalRepository : AppRepository<UserExternal>, IUserExternalRepository
{
public UserExternalRepository(AppDbContext context) : base(context)
{
}
public int AddUserExternal(UserExternal userExternal)
{
return AddBase(userExternal);
}
public async Task<int> AddUserExternalAsync(UserExternal userExternal, CancellationToken cancellationToken = default)
{
return await AddBaseAsync(userExternal, cancellationToken);
}
public async Task<int> AddUserExternalsAsync(IEnumerable<UserExternal> claims)
{
_context.AddRange(claims);
return await SaveChangesAsync();
}
public async Task<List<UserExternal>> GetUserExternalsByUserNameAsync(string userName)
{
return await _context
.UserClaims
.Where(x => x.User.UserName == userName)
.Include(x => x.User)
.ToListAsync();
}
public async Task<List<UserExternal>> GetUserExternalsByUserIdAsync(Guid userId)
{
return await _context
.UserClaims
.Where(x => x.UserId == userId)
.Include(x => x.User)
.ToListAsync();
}
public List<UserExternal> GetUserExternalsByUser(Guid userId)
{
return _context
.UserClaims
.Where(x => x.UserId == userId)
.Include(x => x.User)
.ToList();
}
public List<UserExternal> GetUserExternalsByUserName(string userName)
{
return _context
.UserClaims
.Where(x => x.User.UserName == userName)
.Include(x => x.User)
.ToList();
}
public async Task<UserExternal?> GetByUserIdAsync(Guid userId, string provider)
{
return await _context
.UserClaims
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.UserId == userId && x.Provider == provider);
}
public UserExternal? GetByUserId(Guid userId, string provider)
{
return _context
.UserClaims
.Include(x => x.User)
.FirstOrDefault(x => x.UserId == userId && x.Provider == provider);
}
public UserExternal? GetByExternalId(string externalId, string provider)
{
return _context
.UserClaims
.Include(x => x.User)
.FirstOrDefault(x => x.ExternalId == externalId && x.Provider == provider);
}
public Task<UserExternal?> GetByExternalIdAsync(string externalId, string provider)
{
return _context
.UserClaims
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.ExternalId == externalId && x.Provider == provider);
}
public void RemoveUserExternal(UserExternal userExternal)
{
RemoveBase(userExternal);
}
public async Task RemoveUserExternalAsync(UserExternal userExternal, CancellationToken cancellationToken = default)
{
await RemoveBaseAsync(userExternal, cancellationToken);
}
}
@@ -0,0 +1,43 @@
namespace MyOffice.Data.Repositories.Users;
using MyOffice.Data.Models.Users;
using Repositories;
using Microsoft.EntityFrameworkCore;
using MyOffice.DbContext;
public class UserRepository : AppRepository<User>, IUserRepository
{
public UserRepository(AppDbContext context) : base(context)
{
}
public async Task<User?> GetUserAsync(Guid id)
{
return await GetBaseAsync(id);
}
public User? GetUser(Guid id)
{
return GetBase(id);
}
public async Task<User?> GetByUserUserNameAsync(string userName)
{
return await _context.Users.FirstOrDefaultAsync(x => x.UserName == userName);
}
public User? GetByUserUserName(string userName)
{
return _context.Users.FirstOrDefault(x => x.UserName == userName);
}
public int AddUser(User user)
{
return AddBase(user);
}
public int UpdateUser(User user)
{
return UpdateBase(user);
}
}
@@ -0,0 +1,11 @@
using MyOffice.Data.Models.Verifications;
namespace MyOffice.Data.Repositories.Item;
public interface IVerificationCodeRepository
{
VerificationCode? GetByCode(string code);
bool Add(VerificationCode entity);
bool Update(VerificationCode entity);
}
@@ -0,0 +1,27 @@
namespace MyOffice.Data.Repositories.Item;
using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Verifications;
using MyOffice.DbContext;
public class VerificationCodeRepository : AppRepository<VerificationCode>, IVerificationCodeRepository
{
public VerificationCodeRepository(AppDbContext context) : base(context)
{
}
public bool Add(VerificationCode entity)
{
return AddBase(entity) > 0;
}
public bool Update(VerificationCode entity)
{
return UpdateBase(entity) > 0;
}
public VerificationCode? GetByCode(string code)
{
return _context.Verifications.FirstOrDefault(x => x.Code == code);
}
}