This commit is contained in:
2023-06-17 14:16:09 +03:00
parent 62178f1f32
commit 7ae5d3bc81
180 changed files with 12932 additions and 192 deletions
@@ -0,0 +1,21 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
public class AccountAccountCategoryRepository : AppRepository<AccountAccountCategory>, IAccountAccountCategoryRepository
{
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 bool Remove(AccountAccountCategory accountAccountCategory)
{
return RemoveBase(accountAccountCategory) > 0;
}
}
@@ -0,0 +1,38 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Repositories;
public class AccountCategoryRepository : AppRepository<AccountCategory>, IAccountCategoryRepository
{
public List<AccountCategory> GetAll(Guid userId)
{
return _context.AccountCategories
.Include(x => x.Accounts)
.Where(x => x.UserId == userId)
.ToList();
}
public bool Add(AccountCategory accountCategory)
{
return AddBase(accountCategory) > 0;
}
public AccountCategory? Get(Guid userId, Guid id)
{
return _context.AccountCategories
.Include(x => x.Accounts)
.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public bool Update(AccountCategory accountCategory)
{
return UpdateBase(accountCategory) > 0;
}
public bool Remove(AccountCategory accountCategory)
{
return RemoveBase(accountCategory) > 0;
}
}
@@ -0,0 +1,11 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public class AccountMotionRepository : AppRepository<AccountMotion>, IAccountMotionRepository
{
public bool Add(AccountMotion accountMotion)
{
return AddBase(accountMotion) > 0;
}
}
@@ -0,0 +1,75 @@
namespace MyOffice.Data.Repositories.Account;
using Microsoft.EntityFrameworkCore;
using Models.Accounts;
using MyOffice.Data.Repositories;
public class AccountRepository : AppRepository<Account>, IAccountRepository
{
public List<Account> GetAll(Guid userId)
{
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.AccessRights!.Any(a => a.UserId == userId))
.ToList();
}
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)
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
}
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.AmountPlus),
})
.ToList();
}
public bool Add(Account account)
{
return AddBase(account) > 0;
}
public Account? Get(Guid userId, Guid id)
{
return _context.Accounts!
.Include(x => x.Categories)!
.ThenInclude(x => x.Category)
.Include(x => x.AccessRights)!
.ThenInclude(x => x.User)
.FirstOrDefault(x => x.Id == id && x.AccessRights!.Any(r => r.UserId == userId));
}
public bool Update(Account account)
{
return UpdateBase(account) > 0;
}
public bool Remove(Account account)
{
return RemoveBase(account) > 0;
}
}
@@ -0,0 +1,9 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountAccountCategoryRepository
{
List<AccountAccountCategory> Get(Guid userId, Guid accountId, Guid categoryId);
bool Remove(AccountAccountCategory accountAccountCategory);
}
@@ -0,0 +1,12 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountCategoryRepository
{
List<AccountCategory> GetAll(Guid userId);
bool Add(AccountCategory accountCategory);
AccountCategory? Get(Guid userId, Guid id);
bool Update(AccountCategory accountCategory);
bool Remove(AccountCategory accountCategory);
}
@@ -0,0 +1,8 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountMotionRepository
{
bool Add(AccountMotion accountMotion);
}
@@ -0,0 +1,14 @@
namespace MyOffice.Data.Repositories.Account;
using Models.Accounts;
public interface IAccountRepository
{
List<Account> GetAll(Guid userId);
List<Account> GetByCategory(Guid userId, Guid categoryId);
List<AccountDetailed> GetByCategoryDetailed(Guid userId, Guid categoryId);
bool Add(Account account);
Account? Get(Guid userId, Guid id);
bool Update(Account account);
bool Remove(Account account);
}
@@ -0,0 +1,11 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public class CurrencyGlobalRepository : AppRepository<CurrencyGlobal>, ICurrencyGlobalRepository
{
public List<CurrencyGlobal> GetAll()
{
return _context.CurrencyGlobals.ToList();
}
}
@@ -0,0 +1,29 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRateRepository
{
public List<CurrencyRate> GetLastRates(Guid currencyId, int count = 1)
{
return _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId)
.OrderByDescending(x => x.DateTime)
.ThenByDescending(x => x.Id)
.Take(count)
.ToList();
}
public bool AddRate(CurrencyRate currencyRate)
{
return this.AddBase(currencyRate) > 0;
}
public List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date)
{
return _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId && x.DateTime == date)
.ToList();
}
}
@@ -0,0 +1,41 @@
namespace MyOffice.Data.Repositories.Currency;
using Microsoft.EntityFrameworkCore;
using Models.Currencies;
using MyOffice.Data.Repositories;
public class CurrencyRepository : AppRepository<Currency>, ICurrencyRepository
{
public List<Currency> GetAll(Guid userId)
{
return _context.Currencies
.Include(x => x.CurrencyGlobal)
.Where(x => x.UserId == userId)
.ToList();
}
public Currency? Get(Guid userId, Guid id)
{
return _context.Currencies.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public Currency? GetByGlobalCurrency(Guid userId, string globalCurrencyId)
{
return _context.Currencies.FirstOrDefault(x => x.UserId == userId && x.CurrencyGlobalId == globalCurrencyId);
}
public bool Add(Currency currency)
{
return AddBase(currency) > 0;
}
public bool Update(Currency currency)
{
return UpdateBase(currency) > 0;
}
public bool Remove(Currency currency)
{
return RemoveBase(currency) > 0;
}
}
@@ -0,0 +1,8 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyGlobalRepository
{
List<CurrencyGlobal> GetAll();
}
@@ -0,0 +1,10 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyRateRepository
{
List<CurrencyRate> GetLastRates(Guid currencyId, int count = 1);
bool AddRate(CurrencyRate currencyRate);
List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date);
}
@@ -0,0 +1,13 @@
namespace MyOffice.Data.Repositories.Currency;
using Models.Currencies;
public interface ICurrencyRepository
{
List<Currency> GetAll(Guid userId);
Currency? Get(Guid userId, Guid id);
Currency? GetByGlobalCurrency(Guid userId, string globalCurrencyId);
bool Add(Currency currency);
bool Update(Currency currency);
bool Remove(Currency currency);
}
@@ -0,0 +1,12 @@
namespace MyOffice.Data.Repositories.Motion;
using Models.Motions;
public interface IMotionCategoryRepository
{
List<MotionCategory> GetAll(Guid userId);
bool Add(MotionCategory accountCategory);
MotionCategory? Get(Guid userId, Guid id);
bool Update(MotionCategory accountCategory);
bool Remove(MotionCategory accountCategory);
}
@@ -0,0 +1,10 @@
namespace MyOffice.Data.Repositories.Motion;
using Models.Motions;
public interface IMotionGlobalRepository
{
MotionGlobal? Get(Guid id);
MotionGlobal? GetByName(string name);
bool Add(MotionGlobal motionGlobal);
}
@@ -0,0 +1,13 @@
namespace MyOffice.Data.Repositories.Motion;
using Models.Motions;
public interface IMotionRepository
{
List<MotionAccount> GetAll(Guid userId);
List<MotionAccount> GetByCategory(Guid userId, Guid categoryId);
MotionAccount? GetByGlobal(Guid userId, Guid globalMotionId);
bool Add(MotionAccount motionAccount);
bool Update(MotionAccount motionAccount);
}
@@ -0,0 +1,37 @@
namespace MyOffice.Data.Repositories.Motion;
using Microsoft.EntityFrameworkCore;
using Models.Motions;
public class MotionCategoryRepository : AppRepository<MotionCategory>, IMotionCategoryRepository
{
public List<MotionCategory> GetAll(Guid userId)
{
return _context.MotionCategories
.Include(x => x.Motions)
.Where(x => x.UserId == userId)
.ToList();
}
public bool Add(MotionCategory motionCategory)
{
return AddBase(motionCategory) > 0;
}
public MotionCategory? Get(Guid userId, Guid id)
{
return _context.MotionCategories
.Include(x => x.Motions)
.FirstOrDefault(x => x.Id == id && x.UserId == userId);
}
public bool Update(MotionCategory motionCategory)
{
return UpdateBase(motionCategory) > 0;
}
public bool Remove(MotionCategory motionCategory)
{
return RemoveBase(motionCategory) > 0;
}
}
@@ -0,0 +1,21 @@
namespace MyOffice.Data.Repositories.Motion;
using Models.Motions;
public class MotionGlobalRepository : AppRepository<MotionGlobal>, IMotionGlobalRepository
{
public MotionGlobal? Get(Guid id)
{
return _context.GlobalMotions.FirstOrDefault(x => x.Id == id);
}
public MotionGlobal? GetByName(string name)
{
return _context.GlobalMotions.FirstOrDefault(x => x.Name == name);
}
public bool Add(MotionGlobal motionGlobal)
{
return AddBase(motionGlobal) > 0;
}
}
@@ -0,0 +1,46 @@
namespace MyOffice.Data.Repositories.Motion;
using Microsoft.EntityFrameworkCore;
using Models.Motions;
public class MotionRepository : AppRepository<MotionAccount>, IMotionRepository
{
public List<MotionAccount> GetAll(Guid userId)
{
return _context.Motions
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.MotionGlobal)
.Where(x => x.Category.UserId == userId)
.ToList();
}
public List<MotionAccount> GetByCategory(Guid userId, Guid categoryId)
{
return _context.Motions
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.MotionGlobal)
.Where(x => x.Category.UserId == userId && x.CategoryId == categoryId)
.ToList();
}
public MotionAccount? GetByGlobal(Guid userId, Guid globalMotionId)
{
return _context.Motions
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.MotionGlobal)
.FirstOrDefault(x => x.Category.UserId == userId && x.MotionGlobalId == globalMotionId);
}
public bool Update(MotionAccount motionAccount)
{
return base.UpdateBase(motionAccount) > 0;
}
public bool Add(MotionAccount motionAccount)
{
return base.AddBase(motionAccount) > 0;
}
}
+14 -8
View File
@@ -39,30 +39,34 @@ public class RepositoryBase<TEntity> where TEntity : class
}
protected async Task<TEntity?> GetAsync(Guid id)
protected async Task<TEntity?> GetBaseAsync(Guid id)
{
return await _context.Set<TEntity>().FindAsync(id);
}
protected async Task<int> AddAsync(TEntity entity)
protected async Task<int> AddBaseAsync(TEntity entity)
{
await _context.Set<TEntity>().AddAsync(entity);
//await _context.Set<TEntity>().AddAsync(entity);
await _context.AddAsync(entity);
return await SaveChangesAsync();
}
protected int Add(TEntity entity)
protected int AddBase(TEntity entity)
{
_context.Set<TEntity>().Add(entity);
//_context.Set<TEntity>().Add(entity);
_context.Add(entity);
return SaveChanges();
}
protected int Remove(TEntity entity)
protected int RemoveBase(TEntity entity)
{
_context.Set<TEntity>().Remove(entity);
_context.Entry(entity).State = EntityState.Deleted;
return SaveChanges();
}
protected async Task<int> UpdateAsync(TEntity entity)
protected async Task<int> UpdateBaseAsync(TEntity entity)
{
_context.Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
@@ -70,9 +74,11 @@ public class RepositoryBase<TEntity> where TEntity : class
return await SaveChangesAsync();
}
protected int Update(TEntity entity)
protected int UpdateBase(TEntity entity)
{
_context.Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
return SaveChanges();
}
}
@@ -8,12 +8,12 @@ public class UserExternalRepository : AppRepository<UserExternal>, IUserExternal
{
public async Task<int> AddUserExternalAsync(UserExternal userExternal)
{
return await AddAsync(userExternal);
return await AddBaseAsync(userExternal);
}
public int AddUserExternal(UserExternal userExternal)
{
return Add(userExternal);
return AddBase(userExternal);
}
public async Task<int> AddUserExternalsAsync(IEnumerable<UserExternal> claims)
@@ -93,6 +93,6 @@ public class UserExternalRepository : AppRepository<UserExternal>, IUserExternal
public void RemoveUserExternal(UserExternal userExternal)
{
Remove(userExternal);
RemoveBase(userExternal);
}
}
@@ -8,7 +8,7 @@ public class UserRepository : AppRepository<User>, IUserRepository
{
public async Task<User?> GetUserAsync(Guid id)
{
return await GetAsync(id);
return await GetBaseAsync(id);
}
public async Task<User?> GetByUserUserNameAsync(string userName)
@@ -23,11 +23,11 @@ public class UserRepository : AppRepository<User>, IUserRepository
public async Task<int> AddUserAsync(User user)
{
return await AddAsync(user);
return await AddBaseAsync(user);
}
public async Task<int> UpdateUserAsync(User user)
{
return await UpdateAsync(user);
return await UpdateBaseAsync(user);
}
}