update
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
using Data.Repositories.Account;
|
||||
using Data.Repositories.Currency;
|
||||
using Data.Repositories.Item;
|
||||
using Item;
|
||||
using Item.Domain;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MyOffice.Services.Account.Domain;
|
||||
|
||||
@@ -20,7 +22,8 @@
|
||||
private readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
|
||||
private readonly IMotionRepository _motionRepository;
|
||||
private readonly IItemRepository _itemRepository;
|
||||
private readonly IItemGlobalRepository _motionGlobalRepository;
|
||||
private readonly IItemGlobalRepository _itemGlobalRepository;
|
||||
private readonly ItemService _itemService;
|
||||
|
||||
public AccountService(
|
||||
ILogger<AccountService> logger,
|
||||
@@ -30,7 +33,8 @@
|
||||
IAccountAccountCategoryRepository accountAccountCategoryRepository,
|
||||
IMotionRepository motionRepository,
|
||||
IItemRepository itemRepository,
|
||||
IItemGlobalRepository motionGlobalRepository
|
||||
IItemGlobalRepository itemGlobalRepository,
|
||||
ItemService itemService
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -40,7 +44,8 @@
|
||||
_accountAccountCategoryRepository = accountAccountCategoryRepository;
|
||||
_motionRepository = motionRepository;
|
||||
_itemRepository = itemRepository;
|
||||
_motionGlobalRepository = motionGlobalRepository;
|
||||
_itemGlobalRepository = itemGlobalRepository;
|
||||
_itemService = itemService;
|
||||
}
|
||||
|
||||
public List<AccountCategory> GetAllCategories(Guid userId)
|
||||
@@ -282,7 +287,7 @@
|
||||
public Exec<Motion, MotionAddResult> MotionAdd(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
MotionAdd motion
|
||||
MotionAddUpdate motion
|
||||
)
|
||||
{
|
||||
if (motion == null)
|
||||
@@ -298,31 +303,10 @@
|
||||
return result.Set(MotionAddResult.account_not_found);
|
||||
}
|
||||
|
||||
var motionGlobal = _motionGlobalRepository.GetByName(motion.Motion);
|
||||
if (motionGlobal == null)
|
||||
var itemExec = _itemService.GetOrCreate(userId, motion.Motion);
|
||||
if (itemExec.Status != ItemGetOrAddResult.success)
|
||||
{
|
||||
// add global motion
|
||||
motionGlobal = new ItemGlobal
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = motion.Motion.Trim(),
|
||||
};
|
||||
if (!_motionGlobalRepository.Add(motionGlobal))
|
||||
{
|
||||
return result.Set(MotionAddResult.failure);
|
||||
}
|
||||
}
|
||||
|
||||
var item = _itemRepository.GetByGlobal(userId, motionGlobal.Id);
|
||||
if (item == null)
|
||||
{
|
||||
// add item
|
||||
item = new Item
|
||||
{
|
||||
CategoryId = userId,
|
||||
ItemGlobalId = motionGlobal.Id,
|
||||
};
|
||||
_itemRepository.Add(item);
|
||||
return result.Set(MotionAddResult.failure);
|
||||
}
|
||||
|
||||
var motionDb = new Motion
|
||||
@@ -331,7 +315,7 @@
|
||||
CreatedOn = DateTime.UtcNow,
|
||||
DateTime = motion.Date,
|
||||
AccountId = account.Id,
|
||||
ItemId = item.Id,
|
||||
ItemId = itemExec.Result!.Id,
|
||||
AmountPlus = motion.Plus,
|
||||
AmountMinus = motion.Minus,
|
||||
Description = motion.Description,
|
||||
@@ -343,12 +327,52 @@
|
||||
|
||||
}
|
||||
|
||||
item.ItemGlobal = motionGlobal;
|
||||
motionDb.Item = item;
|
||||
motionDb.Item = itemExec.Result!;
|
||||
|
||||
return result.Set(motionDb);
|
||||
}
|
||||
|
||||
public Exec<Motion, MotionUpdateResult> MotionUpdate(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
Guid motionId,
|
||||
MotionAddUpdate motion
|
||||
)
|
||||
{
|
||||
if (motion == null)
|
||||
throw new ArgumentNullException(nameof(motion));
|
||||
if (motion.Motion == null)
|
||||
throw new ArgumentNullException(nameof(motion.Motion));
|
||||
|
||||
var result = new Exec<Motion, MotionUpdateResult>(MotionUpdateResult.success);
|
||||
|
||||
var exists = _motionRepository.Get(userId, motionId);
|
||||
if (exists == null || exists.AccountId != accountId)
|
||||
{
|
||||
return result.Set(MotionUpdateResult.not_found);
|
||||
}
|
||||
|
||||
var itemExec = _itemService.GetOrCreate(userId, motion.Motion);
|
||||
if (itemExec.Status != ItemGetOrAddResult.success)
|
||||
{
|
||||
return result.Set(MotionUpdateResult.failure);
|
||||
}
|
||||
|
||||
exists.Item.Id = itemExec.Result!.Id;
|
||||
exists.DateTime = motion.Date;
|
||||
exists.Description = motion.Description;
|
||||
exists.AmountPlus = motion.Plus;
|
||||
exists.AmountMinus = motion.Minus;
|
||||
|
||||
if (!_motionRepository.Update(exists))
|
||||
{
|
||||
return result.Set(MotionUpdateResult.failure);
|
||||
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public Exec<List<Motion>, GeneralExecStatus> GetMotions(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
@@ -368,5 +392,27 @@
|
||||
|
||||
return result.Set(motions);
|
||||
}
|
||||
|
||||
public Exec<Motion, MotionDeleteResult> MotionRemove(
|
||||
Guid userId,
|
||||
Guid accountId,
|
||||
Guid motionId
|
||||
)
|
||||
{
|
||||
var result = new Exec<Motion, MotionDeleteResult>(MotionDeleteResult.success);
|
||||
|
||||
var exists = _motionRepository.Get(userId, motionId);
|
||||
if (exists == null || exists.AccountId != accountId)
|
||||
{
|
||||
return result.Set(MotionDeleteResult.not_found);
|
||||
}
|
||||
|
||||
if (!_motionRepository.Remove(exists))
|
||||
{
|
||||
return result.Set(MotionDeleteResult.failure);
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ public enum MotionAddResult
|
||||
success,
|
||||
failure,
|
||||
account_not_found,
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace MyOffice.Services.Account.Domain
|
||||
{
|
||||
public class MotionAdd
|
||||
public class MotionAddUpdate
|
||||
{
|
||||
public DateTime Date { get; set; }
|
||||
public string Motion { get; set; } = null!;
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace MyOffice.Services.Account.Domain;
|
||||
|
||||
public enum MotionDeleteResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace MyOffice.Services.Account.Domain;
|
||||
|
||||
public enum MotionUpdateResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MyOffice.Services.Item.Domain
|
||||
{
|
||||
public enum ItemGetOrAddResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
accounts_exists,
|
||||
}
|
||||
}
|
||||
@@ -3,29 +3,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Domain;
|
||||
using MyOffice.Core;
|
||||
using MyOffice.Data.Models.Accounts;
|
||||
using MyOffice.Data.Models.Items;
|
||||
using MyOffice.Data.Repositories.Item;
|
||||
using MyOffice.Services.Account.Domain;
|
||||
|
||||
public class ItemService
|
||||
{
|
||||
private readonly IItemCategoryRepository _motionCategoryRepository;
|
||||
private readonly IItemRepository _motionRepository;
|
||||
private readonly IItemCategoryRepository _itemCategoryRepository;
|
||||
private readonly IItemRepository _itemRepository;
|
||||
private readonly IItemGlobalRepository _itemGlobalRepository;
|
||||
|
||||
public ItemService(
|
||||
IItemCategoryRepository motionCategoryRepository,
|
||||
IItemRepository motionRepository
|
||||
IItemCategoryRepository itemCategoryRepository,
|
||||
IItemRepository itemRepository,
|
||||
IItemGlobalRepository itemGlobalRepository
|
||||
)
|
||||
{
|
||||
_motionCategoryRepository = motionCategoryRepository;
|
||||
_motionRepository = motionRepository;
|
||||
_itemCategoryRepository = itemCategoryRepository;
|
||||
_itemRepository = itemRepository;
|
||||
_itemGlobalRepository = itemGlobalRepository;
|
||||
|
||||
}
|
||||
|
||||
public List<ItemCategory> GetAllCategories(Guid userId)
|
||||
{
|
||||
var result = _motionCategoryRepository.GetAll(userId);
|
||||
var result = _itemCategoryRepository.GetAll(userId);
|
||||
if (result.All(x => x.Id != userId))
|
||||
{
|
||||
var category = new ItemCategory
|
||||
@@ -34,9 +40,9 @@
|
||||
UserId = userId,
|
||||
Name = "UnCategorized",
|
||||
};
|
||||
_motionCategoryRepository.Add(category);
|
||||
_itemCategoryRepository.Add(category);
|
||||
|
||||
result.Add(_motionCategoryRepository.Get(userId, userId)!);
|
||||
result.Add(_itemCategoryRepository.Get(userId, userId)!);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -51,7 +57,7 @@
|
||||
category.Id = Guid.NewGuid();
|
||||
category.UserId = userId;
|
||||
|
||||
if (!_motionCategoryRepository.Add(category))
|
||||
if (!_itemCategoryRepository.Add(category))
|
||||
{
|
||||
return result.Set(GeneralExecStatus.failure);
|
||||
}
|
||||
@@ -66,7 +72,7 @@
|
||||
|
||||
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var exists = _motionCategoryRepository.Get(userId, id);
|
||||
var exists = _itemCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
@@ -74,7 +80,7 @@
|
||||
|
||||
exists.Name = category.Name;
|
||||
|
||||
if (!_motionCategoryRepository.Update(exists))
|
||||
if (!_itemCategoryRepository.Update(exists))
|
||||
{
|
||||
return result.Set(GeneralExecStatus.failure);
|
||||
}
|
||||
@@ -86,7 +92,7 @@
|
||||
{
|
||||
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
|
||||
|
||||
var exists = _motionCategoryRepository.Get(userId, id);
|
||||
var exists = _itemCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.not_found);
|
||||
@@ -96,7 +102,7 @@
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.accounts_exists);
|
||||
}
|
||||
if (!_motionCategoryRepository.Remove(exists))
|
||||
if (!_itemCategoryRepository.Remove(exists))
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.failure);
|
||||
}
|
||||
@@ -106,19 +112,19 @@
|
||||
|
||||
public List<Item> GetAll(Guid userId)
|
||||
{
|
||||
return _motionRepository.GetAll(userId);
|
||||
return _itemRepository.GetAll(userId);
|
||||
}
|
||||
|
||||
public List<Item> GetByCategory(Guid userId, Guid categoryId)
|
||||
{
|
||||
return _motionRepository.GetByCategory(userId, categoryId);
|
||||
return _itemRepository.GetByCategory(userId, categoryId);
|
||||
}
|
||||
|
||||
public Exec<Item, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
|
||||
{
|
||||
var result = new Exec<Item, GeneralExecStatus>(GeneralExecStatus.success);
|
||||
|
||||
var motion = _motionRepository.GetByGlobal(userId, motionId);
|
||||
var motion = _itemRepository.GetByGlobal(userId, motionId);
|
||||
if (motion == null)
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
@@ -126,9 +132,60 @@
|
||||
|
||||
motion.Category.Id = categoryId;
|
||||
//motion.CategoryId = categoryId;
|
||||
_motionRepository.Update(motion);
|
||||
_itemRepository.Update(motion);
|
||||
|
||||
return result.Set(motion);
|
||||
}
|
||||
|
||||
public Exec<Item, ItemGetOrAddResult> GetOrCreate(Guid userId, string name)
|
||||
{
|
||||
if (name == null)
|
||||
throw new ArgumentNullException(nameof(name));
|
||||
|
||||
var result = new Exec<Item, ItemGetOrAddResult>(ItemGetOrAddResult.success);
|
||||
|
||||
var itemGlobal = _itemGlobalRepository.GetByName(name);
|
||||
if (itemGlobal == null)
|
||||
{
|
||||
// add global motion
|
||||
itemGlobal = new ItemGlobal
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = name.Trim(),
|
||||
};
|
||||
|
||||
if (!_itemGlobalRepository.Add(itemGlobal))
|
||||
{
|
||||
return result.Set(ItemGetOrAddResult.failure);
|
||||
}
|
||||
|
||||
return GetOrCreate(userId, itemGlobal);
|
||||
}
|
||||
|
||||
return GetOrCreate(userId, itemGlobal);
|
||||
}
|
||||
|
||||
private Exec<Item, ItemGetOrAddResult> GetOrCreate(Guid userId, ItemGlobal itemGlobal)
|
||||
{
|
||||
if (itemGlobal == null)
|
||||
throw new ArgumentNullException(nameof(itemGlobal));
|
||||
|
||||
var result = new Exec<Item, ItemGetOrAddResult>(ItemGetOrAddResult.success);
|
||||
|
||||
var item = _itemRepository.GetByGlobal(userId, itemGlobal.Id);
|
||||
if (item == null)
|
||||
{
|
||||
// add item to UnCategorized category
|
||||
item = new Item
|
||||
{
|
||||
CategoryId = userId,
|
||||
ItemGlobalId = itemGlobal.Id,
|
||||
};
|
||||
_itemRepository.Add(item);
|
||||
}
|
||||
|
||||
item.ItemGlobal = itemGlobal;
|
||||
return result.Set(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user