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,
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MyOffice.Services.Item.Domain
|
||||
{
|
||||
public enum ItemCategoryRemoveResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
accounts_exists,
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,21 @@
|
||||
namespace MyOffice.Services.Motion
|
||||
namespace MyOffice.Services.Item
|
||||
{
|
||||
using MyOffice.Data.Repositories.Motion;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Data.Models.Motions;
|
||||
using Domain;
|
||||
using MyOffice.Core;
|
||||
using MyOffice.Services.Motion.Domain;
|
||||
using MyOffice.Data.Models.Items;
|
||||
using MyOffice.Data.Repositories.Item;
|
||||
|
||||
public class MotionService
|
||||
public class ItemService
|
||||
{
|
||||
private readonly IMotionCategoryRepository _motionCategoryRepository;
|
||||
private readonly IMotionRepository _motionRepository;
|
||||
private readonly IItemCategoryRepository _motionCategoryRepository;
|
||||
private readonly IItemRepository _motionRepository;
|
||||
|
||||
public MotionService(
|
||||
IMotionCategoryRepository motionCategoryRepository,
|
||||
IMotionRepository motionRepository
|
||||
public ItemService(
|
||||
IItemCategoryRepository motionCategoryRepository,
|
||||
IItemRepository motionRepository
|
||||
)
|
||||
{
|
||||
_motionCategoryRepository = motionCategoryRepository;
|
||||
@@ -25,12 +23,12 @@
|
||||
|
||||
}
|
||||
|
||||
public List<MotionCategory> GetAllCategories(Guid userId)
|
||||
public List<ItemCategory> GetAllCategories(Guid userId)
|
||||
{
|
||||
var result = _motionCategoryRepository.GetAll(userId);
|
||||
if (result.All(x => x.Id != userId))
|
||||
{
|
||||
var category = new MotionCategory
|
||||
var category = new ItemCategory
|
||||
{
|
||||
Id = userId,
|
||||
UserId = userId,
|
||||
@@ -43,12 +41,12 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
public Exec<MotionCategory, GeneralExecStatus> CategoryAdd(Guid userId, MotionCategory category)
|
||||
public Exec<ItemCategory, GeneralExecStatus> CategoryAdd(Guid userId, ItemCategory category)
|
||||
{
|
||||
if (category == null)
|
||||
throw new ArgumentNullException(nameof(category));
|
||||
|
||||
var result = new Exec<MotionCategory, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
category.Id = Guid.NewGuid();
|
||||
category.UserId = userId;
|
||||
@@ -61,12 +59,12 @@
|
||||
return result.Set(category);
|
||||
}
|
||||
|
||||
public Exec<MotionCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, MotionCategory category)
|
||||
public Exec<ItemCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, ItemCategory category)
|
||||
{
|
||||
if (category == null)
|
||||
throw new ArgumentNullException(nameof(category));
|
||||
|
||||
var result = new Exec<MotionCategory, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var exists = _motionCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
@@ -84,41 +82,41 @@
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public Exec<MotionCategory, MotionCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
|
||||
public Exec<ItemCategory, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
|
||||
{
|
||||
var result = new Exec<MotionCategory, MotionCategoryRemoveResult>(MotionCategoryRemoveResult.success);
|
||||
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
|
||||
|
||||
var exists = _motionCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
{
|
||||
return result.Set(MotionCategoryRemoveResult.not_found);
|
||||
return result.Set(ItemCategoryRemoveResult.not_found);
|
||||
}
|
||||
|
||||
if (exists.Motions.Any())
|
||||
if (exists.Items.Any())
|
||||
{
|
||||
return result.Set(MotionCategoryRemoveResult.accounts_exists);
|
||||
return result.Set(ItemCategoryRemoveResult.accounts_exists);
|
||||
}
|
||||
if (!_motionCategoryRepository.Remove(exists))
|
||||
{
|
||||
return result.Set(MotionCategoryRemoveResult.failure);
|
||||
return result.Set(ItemCategoryRemoveResult.failure);
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public List<MotionAccount> GetAll(Guid userId)
|
||||
public List<Item> GetAll(Guid userId)
|
||||
{
|
||||
return _motionRepository.GetAll(userId);
|
||||
}
|
||||
|
||||
public List<MotionAccount> GetByCategory(Guid userId, Guid categoryId)
|
||||
public List<Item> GetByCategory(Guid userId, Guid categoryId)
|
||||
{
|
||||
return _motionRepository.GetByCategory(userId, categoryId);
|
||||
}
|
||||
|
||||
public Exec<MotionAccount, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
|
||||
public Exec<Item, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
|
||||
{
|
||||
var result = new Exec<MotionAccount, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
var result = new Exec<Item, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var motion = _motionRepository.GetByGlobal(userId, motionId);
|
||||
if (motion == null)
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace MyOffice.Services.Motion.Domain
|
||||
{
|
||||
public enum MotionCategoryRemoveResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
accounts_exists,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user