This commit is contained in:
2023-07-28 21:18:18 +03:00
parent 5ecefe5756
commit 90f0386bfe
53 changed files with 3067 additions and 267 deletions
+144 -40
View File
@@ -13,6 +13,7 @@
using Item.Domain;
using Microsoft.Extensions.Logging;
using MyOffice.Services.Account.Domain;
using System.Collections.Generic;
public class AccountService
{
@@ -20,6 +21,7 @@
private readonly IMapper _mapper;
private readonly IAccountCategoryRepository _accountCategoryRepository;
private readonly IAccountAccessRepository _accountAccessRepository;
private readonly IAccountAccessInviteRepository _accountAccessInviteRepository;
private readonly IAccountRepository _accountRepository;
private readonly ICurrencyRepository _currencyRepository;
private readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
@@ -33,6 +35,7 @@
IMapper mapper,
IAccountCategoryRepository accountCategoryRepository,
IAccountAccessRepository accountAccessRepository,
IAccountAccessInviteRepository accountAccessInviteRepository,
IAccountRepository accountRepository,
ICurrencyRepository currencyRepository,
IAccountAccountCategoryRepository accountAccountCategoryRepository,
@@ -46,6 +49,7 @@
_mapper = mapper;
_accountCategoryRepository = accountCategoryRepository;
_accountAccessRepository = accountAccessRepository;
_accountAccessInviteRepository = accountAccessInviteRepository;
_accountRepository = accountRepository;
_currencyRepository = currencyRepository;
_accountAccountCategoryRepository = accountAccountCategoryRepository;
@@ -55,14 +59,16 @@
_itemService = itemService;
}
public List<AccountCategory> GetAllCategories(Guid userId)
public List<AccountCategoryDto> GetAllCategories(Guid userId)
{
return _accountCategoryRepository.GetAll(userId);
var categories = _accountCategoryRepository.GetAll(userId);
return _mapper.Map<List<AccountCategoryDto>>(categories);
}
public Exec<AccountCategory, GeneralExecStatus> GetCategory(Guid userId, Guid id)
public Exec<AccountCategoryDto, GeneralExecStatus> GetCategory(Guid userId, Guid id)
{
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var category = _accountCategoryRepository.Get(userId, id);
@@ -71,15 +77,15 @@
return result.Set(GeneralExecStatus.not_found);
}
return result.Set(category);
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategory, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
@@ -89,15 +95,15 @@
return result.Set(GeneralExecStatus.failure);
}
return result.Set(category);
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var exists = _accountCategoryRepository.Get(userId, id);
if (exists == null)
@@ -112,12 +118,12 @@
return result.Set(GeneralExecStatus.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<AccountCategoryDto>(exists));
}
public Exec<AccountCategory, AccountCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
public Exec<AccountCategoryDto, AccountCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
{
var result = new Exec<AccountCategory, AccountCategoryRemoveResult>(AccountCategoryRemoveResult.success);
var result = new Exec<AccountCategoryDto, AccountCategoryRemoveResult>(AccountCategoryRemoveResult.success);
var exists = _accountCategoryRepository.Get(userId, id);
if (exists == null)
@@ -134,12 +140,14 @@
return result.Set(AccountCategoryRemoveResult.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<AccountCategoryDto>(exists));
}
public List<AccountDto> GetAllAccounts(Guid userId)
{
return _mapper.Map<List<AccountDto>>(_accountRepository.GetAll(userId));
var accounts = _accountRepository.GetAll(userId);
return _mapper.Map<List<AccountDto>>(accounts, o => o.Items.Add("UserId", userId));
}
public List<AccountDto> GetByCategory(Guid userId, Guid categoryId)
@@ -165,22 +173,22 @@
return result.Set(_mapper.Map<AccountDetailedDto>(account));
}
public Exec<Account, AccountAddResult> AccountAdd(Guid userId, AccountAdd input)
public Exec<Account, AccountAddStatus> AccountAdd(Guid userId, AccountAdd input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<Account, AccountAddResult>(AccountAddResult.success);
var result = new Exec<Account, AccountAddStatus>(AccountAddStatus.success);
var category = _accountCategoryRepository.Get(userId, input.CategoryId);
if (category == null)
{
return result.Set(AccountAddResult.category_not_found);
return result.Set(AccountAddStatus.category_not_found);
}
var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
if (currency == null)
{
return result.Set(AccountAddResult.currency_not_found);
return result.Set(AccountAddStatus.currency_not_found);
}
var account = new Account
@@ -188,6 +196,7 @@
Id = Guid.NewGuid(),
Name = input.Name,
CurrencyGlobalId = currency.CurrencyGlobalId,
OwnerId = userId,
};
account.Categories = new List<AccountAccountCategory>
@@ -204,6 +213,7 @@
{
AccountId = account.Id,
UserId = userId,
OwnerId = userId,
IsAllowManage = true,
IsAllowRead = true,
IsAllowWrite = true,
@@ -212,7 +222,7 @@
if (!_accountRepository.Add(account))
{
return result.Set(AccountAddResult.failure);
return result.Set(AccountAddStatus.failure);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
@@ -249,12 +259,12 @@
return result;
}
public Exec<Account, AccountEditResult> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)
public Exec<Account, AccountEditStatus> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<Account, AccountEditResult>(AccountEditResult.success);
var result = new Exec<Account, AccountEditStatus>(AccountEditStatus.success);
AccountCategory? category = null;
if (input.CategoryId.HasValue)
@@ -262,20 +272,20 @@
category = _accountCategoryRepository.Get(userId, input.CategoryId.Value);
if (category == null)
{
return result.Set(AccountEditResult.category_not_found);
return result.Set(AccountEditStatus.category_not_found);
}
}
var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
if (currency == null)
{
return result.Set(AccountEditResult.currency_not_found);
return result.Set(AccountEditStatus.currency_not_found);
}
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(AccountEditResult.not_found);
return result.Set(AccountEditStatus.not_found);
}
account.Name = input.Name;
@@ -298,7 +308,7 @@
if (!_accountRepository.Update(account))
{
return result.Set(AccountEditResult.failure);
return result.Set(AccountEditStatus.failure);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
@@ -332,7 +342,7 @@
return result.Set(categories);
}
public Exec<List<Motion>, MotionAddResult> MotionAdd(
public Exec<List<Motion>, MotionAddStatus> MotionAdd(
Guid userId,
Guid accountId,
MotionAddUpdate motion
@@ -343,18 +353,18 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<List<Motion>, MotionAddResult>(MotionAddResult.success);
var result = new Exec<List<Motion>, MotionAddStatus>(MotionAddStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(MotionAddResult.account_not_found);
return result.Set(MotionAddStatus.account_not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionAddResult.failure);
return result.Set(MotionAddStatus.failure);
}
var motionDb = new Motion
@@ -371,7 +381,7 @@
if (!_motionRepository.Add(motionDb))
{
return result.Set(MotionAddResult.failure);
return result.Set(MotionAddStatus.failure);
}
result.Set(new List<Motion>());
@@ -415,7 +425,7 @@
return result;
}
public Exec<Motion, MotionUpdateResult> MotionUpdate(
public Exec<Motion, MotionUpdateStatus> MotionUpdate(
Guid userId,
Guid accountId,
Guid motionId,
@@ -427,18 +437,18 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<Motion, MotionUpdateResult>(MotionUpdateResult.success);
var result = new Exec<Motion, MotionUpdateStatus>(MotionUpdateStatus.success);
var exists = _motionRepository.Get(userId, motionId);
if (exists == null || exists.AccountId != accountId)
{
return result.Set(MotionUpdateResult.not_found);
return result.Set(MotionUpdateStatus.not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionUpdateResult.failure);
return result.Set(MotionUpdateStatus.failure);
}
exists.Item.Id = itemExec.Result!.Id;
@@ -449,7 +459,7 @@
if (!_motionRepository.Update(exists))
{
return result.Set(MotionUpdateResult.failure);
return result.Set(MotionUpdateStatus.failure);
}
@@ -476,23 +486,23 @@
return result.Set(motions);
}
public Exec<Motion, MotionDeleteResult> MotionRemove(
public Exec<Motion, MotionDeleteStatus> MotionRemove(
Guid userId,
Guid accountId,
Guid motionId
)
{
var result = new Exec<Motion, MotionDeleteResult>(MotionDeleteResult.success);
var result = new Exec<Motion, MotionDeleteStatus>(MotionDeleteStatus.success);
var exists = _motionRepository.Get(userId, motionId);
if (exists == null || exists.AccountId != accountId)
{
return result.Set(MotionDeleteResult.not_found);
return result.Set(MotionDeleteStatus.not_found);
}
if (!_motionRepository.Remove(exists))
{
return result.Set(MotionDeleteResult.failure);
return result.Set(MotionDeleteStatus.failure);
}
return result.Set(exists);
@@ -502,5 +512,99 @@
{
return _accountRepository.FindAccounts(userId, term);
}
public Exec<AccountAccessInvite, AccessInviteStatus> AccessInvite(Guid userId, Guid accountId, string email, bool isAllowWrite)
{
if (email == null)
throw new ArgumentNullException(nameof(email));
var result = new Exec<AccountAccessInvite, AccessInviteStatus>(AccessInviteStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(AccessInviteStatus.account_not_found);
}
if (account.AccessRights!.Any(x => x.User!.Email.EqualsIgnoreCase(email)))
{
return result.Set(AccessInviteStatus.account_not_found);
}
var access = _accountAccessInviteRepository.Get(userId, email);
if (access != null)
{
return result.Set(AccessInviteStatus.access_exists);
}
var invite = new AccountAccessInvite
{
Id = Guid.NewGuid(),
CreatedOn = DateTime.UtcNow,
UserId = userId,
Email = email,
IsAllowWrite = isAllowWrite,
};
_accountAccessInviteRepository.Add(invite);
return result.Set(invite);
}
public Exec<AccountDto, GeneralExecStatus> AccessUpdate(Guid userId, Guid accountId, List<AccountAccessDto> accesses)
{
if (accesses == null)
throw new ArgumentNullException(nameof(accesses));
var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(GeneralExecStatus.not_found);
}
foreach (var access in account.AccessRights!)
{
var newAccess = accesses.FirstOrDefault(x => x.UserId == access.UserId);
if (newAccess != null && newAccess.IsAllowWrite != access.IsAllowWrite)
{
access.IsAllowWrite = newAccess.IsAllowWrite;
_accountAccessRepository.Update(access);
}
}
account = _accountRepository.Get(userId, accountId);
return result.Set(_mapper.Map<AccountDto>(account));
}
public Exec<AccountDto, GeneralExecStatus> AccessDelete(Guid userId, Guid accountId, Guid accessUserId)
{
var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null || !account.AccessRights!.Any() || account.AccessRights!.Count() == 1)
{
return result.Set(GeneralExecStatus.not_found);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == accessUserId);
if (access == null)
{
return result.Set(GeneralExecStatus.not_found);
}
if (access.OwnerId == access.UserId)
{
return result.Set(GeneralExecStatus.not_found);
}
_accountAccessRepository.Delete(access);
account = _accountRepository.Get(userId, accountId);
return result.Set(_mapper.Map<AccountDto>(account));
}
}
}