This commit is contained in:
2023-07-20 21:38:05 +03:00
parent 4bac59f322
commit 2f108bef4f
35 changed files with 1863 additions and 909 deletions
+11 -6
View File
@@ -292,8 +292,8 @@
{
if (motion == null)
throw new ArgumentNullException(nameof(motion));
if (motion.Motion == null)
throw new ArgumentNullException(nameof(motion.Motion));
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<Motion, MotionAddResult>(MotionAddResult.success);
@@ -303,7 +303,7 @@
return result.Set(MotionAddResult.account_not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Motion);
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionAddResult.failure);
@@ -341,8 +341,8 @@
{
if (motion == null)
throw new ArgumentNullException(nameof(motion));
if (motion.Motion == null)
throw new ArgumentNullException(nameof(motion.Motion));
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<Motion, MotionUpdateResult>(MotionUpdateResult.success);
@@ -352,7 +352,7 @@
return result.Set(MotionUpdateResult.not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Motion);
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionUpdateResult.failure);
@@ -414,5 +414,10 @@
return result.Set(exists);
}
public List<Account> FindAccounts(Guid userId, string term)
{
return _accountRepository.FindAccounts(userId, term);
}
}
}
@@ -3,7 +3,7 @@
public class MotionAddUpdate
{
public DateTime Date { get; set; }
public string Motion { get; set; } = null!;
public string Item { get; set; } = null!;
public string? Description { get; set; }
public decimal Plus { get; set; }
public decimal Minus { get; set; }
+163 -153
View File
@@ -1,191 +1,201 @@
namespace MyOffice.Services.Item
namespace MyOffice.Services.Item;
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
{
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;
private readonly IItemCategoryRepository _itemCategoryRepository;
private readonly IItemRepository _itemRepository;
private readonly IItemGlobalRepository _itemGlobalRepository;
public class ItemService
public ItemService(
IItemCategoryRepository itemCategoryRepository,
IItemRepository itemRepository,
IItemGlobalRepository itemGlobalRepository
)
{
private readonly IItemCategoryRepository _itemCategoryRepository;
private readonly IItemRepository _itemRepository;
private readonly IItemGlobalRepository _itemGlobalRepository;
_itemCategoryRepository = itemCategoryRepository;
_itemRepository = itemRepository;
_itemGlobalRepository = itemGlobalRepository;
public ItemService(
IItemCategoryRepository itemCategoryRepository,
IItemRepository itemRepository,
IItemGlobalRepository itemGlobalRepository
)
}
public List<ItemCategory> GetAllCategories(Guid userId)
{
var result = _itemCategoryRepository.GetAll(userId);
if (result.All(x => x.Id != userId))
{
_itemCategoryRepository = itemCategoryRepository;
_itemRepository = itemRepository;
_itemGlobalRepository = itemGlobalRepository;
var category = new ItemCategory
{
Id = userId,
UserId = userId,
Name = "UnCategorized",
};
_itemCategoryRepository.Add(category);
result.Add(_itemCategoryRepository.Get(userId, userId)!);
}
return result;
}
public Exec<ItemCategory, GeneralExecStatus> CategoryAdd(Guid userId, ItemCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
category.IsInternal = category.IsInternal;
category.Items = new List<Item>();
if (!_itemCategoryRepository.Add(category))
{
return result.Set(GeneralExecStatus.failure);
}
public List<ItemCategory> GetAllCategories(Guid userId)
return result.Set(category);
}
public Exec<ItemCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, ItemCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
{
var result = _itemCategoryRepository.GetAll(userId);
if (result.All(x => x.Id != userId))
{
var category = new ItemCategory
{
Id = userId,
UserId = userId,
Name = "UnCategorized",
};
_itemCategoryRepository.Add(category);
result.Add(_itemCategoryRepository.Get(userId, userId)!);
}
return result;
return result.Set(GeneralExecStatus.not_found);
}
public Exec<ItemCategory, GeneralExecStatus> CategoryAdd(Guid userId, ItemCategory category)
exists.Name = category.Name;
exists.IsInternal = category.IsInternal;
exists.Items = new List<Item>();
if (!_itemCategoryRepository.Update(exists))
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
if (!_itemCategoryRepository.Add(category))
{
return result.Set(GeneralExecStatus.failure);
}
return result.Set(category);
return result.Set(GeneralExecStatus.failure);
}
public Exec<ItemCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, ItemCategory category)
return result.Set(exists);
}
public Exec<ItemCategory, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
{
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<ItemCategory, GeneralExecStatus>(GeneralExecStatus.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
{
return result.Set(GeneralExecStatus.not_found);
}
exists.Name = category.Name;
if (!_itemCategoryRepository.Update(exists))
{
return result.Set(GeneralExecStatus.failure);
}
return result.Set(exists);
return result.Set(ItemCategoryRemoveResult.not_found);
}
public Exec<ItemCategory, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
if (exists.Items.Any())
{
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
var exists = _itemCategoryRepository.Get(userId, id);
if (exists == null)
{
return result.Set(ItemCategoryRemoveResult.not_found);
}
if (exists.Items.Any())
{
return result.Set(ItemCategoryRemoveResult.accounts_exists);
}
if (!_itemCategoryRepository.Remove(exists))
{
return result.Set(ItemCategoryRemoveResult.failure);
}
return result.Set(exists);
return result.Set(ItemCategoryRemoveResult.accounts_exists);
}
if (!_itemCategoryRepository.Remove(exists))
{
return result.Set(ItemCategoryRemoveResult.failure);
}
public List<Item> GetAll(Guid userId)
return result.Set(exists);
}
public List<Item> GetAll(Guid userId)
{
return _itemRepository.GetAll(userId);
}
public List<Item> GetByCategory(Guid userId, Guid 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 = _itemRepository.GetByGlobal(userId, motionId);
if (motion == null)
{
return _itemRepository.GetAll(userId);
return result.Set(GeneralExecStatus.not_found);
}
public List<Item> GetByCategory(Guid userId, Guid categoryId)
{
return _itemRepository.GetByCategory(userId, categoryId);
}
motion.Category!.Id = categoryId;
_itemRepository.Update(motion);
public Exec<Item, GeneralExecStatus> Update(Guid userId, Guid motionId, Guid categoryId)
{
var result = new Exec<Item, GeneralExecStatus>(GeneralExecStatus.success);
return result.Set(motion);
}
var motion = _itemRepository.GetByGlobal(userId, motionId);
if (motion == null)
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
{
return result.Set(GeneralExecStatus.not_found);
}
Id = Guid.NewGuid(),
Name = name.Trim(),
};
motion.Category.Id = categoryId;
//motion.CategoryId = categoryId;
_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)
if (!_itemGlobalRepository.Add(itemGlobal))
{
// 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 result.Set(ItemGetOrAddResult.failure);
}
return GetOrCreate(userId, itemGlobal);
}
private Exec<Item, ItemGetOrAddResult> GetOrCreate(Guid userId, ItemGlobal 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)
{
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
{
// add item to UnCategorized category
item = new Item
{
CategoryId = userId,
ItemGlobalId = itemGlobal.Id,
};
_itemRepository.Add(item);
}
item.ItemGlobal = itemGlobal;
return result.Set(item);
CategoryId = userId,
ItemGlobalId = itemGlobal.Id,
};
_itemRepository.Add(item);
}
item.ItemGlobal = itemGlobal;
return result.Set(item);
}
public List<Item> FindItems(Guid userId, string term, int limit = 15)
{
if (term == null)
throw new ArgumentNullException(nameof(term));
return _itemRepository.Find(userId, term, limit);
}
}
@@ -11,4 +11,8 @@
<ProjectReference Include="..\MyOffice.Data.Repositories\MyOffice.Data.Repositories.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Dashboard\" />
</ItemGroup>
</Project>