refactoring
This commit is contained in:
@@ -4,23 +4,23 @@
|
||||
using Core.Extensions;
|
||||
using Core.Helpers;
|
||||
using Data.Models.Accounts;
|
||||
using Data.Models.Motions;
|
||||
using Data.Models.Items;
|
||||
using Data.Repositories.Account;
|
||||
using Data.Repositories.Currency;
|
||||
using Data.Repositories.Motion;
|
||||
using Data.Repositories.Item;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MyOffice.Services.Account.Domain;
|
||||
|
||||
public class AccountService
|
||||
{
|
||||
private ILogger<AccountService> _logger;
|
||||
public readonly IAccountCategoryRepository _accountCategoryRepository;
|
||||
public readonly IAccountRepository _accountRepository;
|
||||
public readonly ICurrencyRepository _currencyRepository;
|
||||
public readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
|
||||
public readonly IAccountMotionRepository _accountMotionRepository;
|
||||
public readonly IMotionRepository _motionRepository;
|
||||
public readonly IMotionGlobalRepository _motionGlobalRepository;
|
||||
private readonly IAccountCategoryRepository _accountCategoryRepository;
|
||||
private readonly IAccountRepository _accountRepository;
|
||||
private readonly ICurrencyRepository _currencyRepository;
|
||||
private readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
|
||||
private readonly IMotionRepository _motionRepository;
|
||||
private readonly IItemRepository _itemRepository;
|
||||
private readonly IItemGlobalRepository _motionGlobalRepository;
|
||||
|
||||
public AccountService(
|
||||
ILogger<AccountService> logger,
|
||||
@@ -28,9 +28,9 @@
|
||||
IAccountRepository accountRepository,
|
||||
ICurrencyRepository currencyRepository,
|
||||
IAccountAccountCategoryRepository accountAccountCategoryRepository,
|
||||
IAccountMotionRepository accountMotionRepository,
|
||||
IMotionRepository motionRepository,
|
||||
IMotionGlobalRepository motionGlobalRepository
|
||||
IItemRepository itemRepository,
|
||||
IItemGlobalRepository motionGlobalRepository
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -38,8 +38,8 @@
|
||||
_accountRepository = accountRepository;
|
||||
_currencyRepository = currencyRepository;
|
||||
_accountAccountCategoryRepository = accountAccountCategoryRepository;
|
||||
_accountMotionRepository = accountMotionRepository;
|
||||
_motionRepository = motionRepository;
|
||||
_itemRepository = itemRepository;
|
||||
_motionGlobalRepository = motionGlobalRepository;
|
||||
}
|
||||
|
||||
@@ -279,84 +279,84 @@
|
||||
return result.Set(categories);
|
||||
}
|
||||
|
||||
public Exec<AccountMotion, AccountMotionAddResult> AccountMotionAdd(
|
||||
public Exec<Motion, MotionAddResult> MotionAdd(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
AccountMotionAdd accountMotion
|
||||
MotionAdd motion
|
||||
)
|
||||
{
|
||||
if (accountMotion == null)
|
||||
throw new ArgumentNullException(nameof(accountMotion));
|
||||
if (accountMotion.Motion == null)
|
||||
throw new ArgumentNullException(nameof(accountMotion.Motion));
|
||||
if (motion == null)
|
||||
throw new ArgumentNullException(nameof(motion));
|
||||
if (motion.Motion == null)
|
||||
throw new ArgumentNullException(nameof(motion.Motion));
|
||||
|
||||
var result = new Exec<AccountMotion, AccountMotionAddResult>(AccountMotionAddResult.success);
|
||||
var result = new Exec<Motion, MotionAddResult>(MotionAddResult.success);
|
||||
|
||||
var account = _accountRepository.Get(userId, accountId);
|
||||
if (account == null)
|
||||
{
|
||||
return result.Set(AccountMotionAddResult.account_not_found);
|
||||
return result.Set(MotionAddResult.account_not_found);
|
||||
}
|
||||
|
||||
var motionGlobal = _motionGlobalRepository.GetByName(accountMotion.Motion);
|
||||
var motionGlobal = _motionGlobalRepository.GetByName(motion.Motion);
|
||||
if (motionGlobal == null)
|
||||
{
|
||||
// add global motion
|
||||
motionGlobal = new MotionGlobal
|
||||
motionGlobal = new ItemGlobal
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = accountMotion.Motion.Trim(),
|
||||
Name = motion.Motion.Trim(),
|
||||
};
|
||||
if (!_motionGlobalRepository.Add(motionGlobal))
|
||||
{
|
||||
return result.Set(AccountMotionAddResult.failure);
|
||||
return result.Set(MotionAddResult.failure);
|
||||
}
|
||||
}
|
||||
|
||||
var motionAccount = _motionRepository.GetByGlobal(userId, motionGlobal.Id);
|
||||
if (motionAccount == null)
|
||||
var item = _itemRepository.GetByGlobal(userId, motionGlobal.Id);
|
||||
if (item == null)
|
||||
{
|
||||
// add motion account
|
||||
motionAccount = new MotionAccount
|
||||
// add item
|
||||
item = new Item
|
||||
{
|
||||
CategoryId = userId,
|
||||
MotionGlobalId = motionGlobal.Id,
|
||||
ItemGlobalId = motionGlobal.Id,
|
||||
};
|
||||
_motionRepository.Add(motionAccount);
|
||||
_itemRepository.Add(item);
|
||||
}
|
||||
|
||||
var accountMotionDb = new AccountMotion
|
||||
var motionDb = new Motion
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CreatedOn = DateTime.UtcNow,
|
||||
DateTime = accountMotion.Date,
|
||||
DateTime = motion.Date,
|
||||
AccountId = account.Id,
|
||||
MotionId = motionAccount.Id,
|
||||
AmountPlus = accountMotion.Plus,
|
||||
AmountMinus = accountMotion.Minus,
|
||||
Description = accountMotion.Description,
|
||||
ItemId = item.Id,
|
||||
AmountPlus = motion.Plus,
|
||||
AmountMinus = motion.Minus,
|
||||
Description = motion.Description,
|
||||
};
|
||||
|
||||
if (!_accountMotionRepository.Add(accountMotionDb))
|
||||
if (!_motionRepository.Add(motionDb))
|
||||
{
|
||||
return result.Set(AccountMotionAddResult.failure);
|
||||
return result.Set(MotionAddResult.failure);
|
||||
|
||||
}
|
||||
|
||||
motionAccount.MotionGlobal = motionGlobal;
|
||||
accountMotionDb.Motion = motionAccount;
|
||||
item.ItemGlobal = motionGlobal;
|
||||
motionDb.Item = item;
|
||||
|
||||
return result.Set(accountMotionDb);
|
||||
return result.Set(motionDb);
|
||||
}
|
||||
|
||||
public Exec<List<AccountMotion>, GeneralExecStatus> GetAccountMotion(
|
||||
public Exec<List<Motion>, GeneralExecStatus> GetMotions(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
DateTime dateFrom,
|
||||
DateTime dateTo
|
||||
)
|
||||
{
|
||||
var result = new Exec<List<AccountMotion>, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
var result = new Exec<List<Motion>, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var account = _accountRepository.Get(userId, accountId);
|
||||
if (account == null)
|
||||
@@ -364,7 +364,9 @@
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
return result.Set(_accountMotionRepository.GetByAccount(accountId, dateFrom.ToUtc(), dateTo.ToUtc()));
|
||||
var motions = _motionRepository.GetByAccount(accountId, dateFrom.ToUtc(), dateTo.ToUtc());
|
||||
|
||||
return result.Set(motions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace MyOffice.Services.Account.Domain
|
||||
{
|
||||
public class AccountMotionAdd
|
||||
public class MotionAdd
|
||||
{
|
||||
public DateTime Date { get; set; }
|
||||
public string Motion { get; set; } = null!;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace MyOffice.Services.Account.Domain;
|
||||
|
||||
public enum AccountMotionAddResult
|
||||
public enum MotionAddResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
Reference in New Issue
Block a user