This commit is contained in:
2023-06-23 09:03:00 +03:00
parent d549877567
commit 4bac59f322
27 changed files with 376 additions and 126 deletions
+75 -18
View File
@@ -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);
}
}
}