refactoring
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace MyOffice.Services.Item.Domain
|
||||
{
|
||||
public enum ItemCategoryRemoveResult
|
||||
{
|
||||
success,
|
||||
failure,
|
||||
not_found,
|
||||
accounts_exists,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
namespace MyOffice.Services.Item
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Domain;
|
||||
using MyOffice.Core;
|
||||
using MyOffice.Data.Models.Items;
|
||||
using MyOffice.Data.Repositories.Item;
|
||||
|
||||
public class ItemService
|
||||
{
|
||||
private readonly IItemCategoryRepository _motionCategoryRepository;
|
||||
private readonly IItemRepository _motionRepository;
|
||||
|
||||
public ItemService(
|
||||
IItemCategoryRepository motionCategoryRepository,
|
||||
IItemRepository motionRepository
|
||||
)
|
||||
{
|
||||
_motionCategoryRepository = motionCategoryRepository;
|
||||
_motionRepository = motionRepository;
|
||||
|
||||
}
|
||||
|
||||
public List<ItemCategory> GetAllCategories(Guid userId)
|
||||
{
|
||||
var result = _motionCategoryRepository.GetAll(userId);
|
||||
if (result.All(x => x.Id != userId))
|
||||
{
|
||||
var category = new ItemCategory
|
||||
{
|
||||
Id = userId,
|
||||
UserId = userId,
|
||||
Name = "UnCategorized",
|
||||
};
|
||||
_motionCategoryRepository.Add(category);
|
||||
|
||||
result.Add(_motionCategoryRepository.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;
|
||||
|
||||
if (!_motionCategoryRepository.Add(category))
|
||||
{
|
||||
return result.Set(GeneralExecStatus.failure);
|
||||
}
|
||||
|
||||
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 = _motionCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
exists.Name = category.Name;
|
||||
|
||||
if (!_motionCategoryRepository.Update(exists))
|
||||
{
|
||||
return result.Set(GeneralExecStatus.failure);
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public Exec<ItemCategory, ItemCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
|
||||
{
|
||||
var result = new Exec<ItemCategory, ItemCategoryRemoveResult>(ItemCategoryRemoveResult.success);
|
||||
|
||||
var exists = _motionCategoryRepository.Get(userId, id);
|
||||
if (exists == null)
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.not_found);
|
||||
}
|
||||
|
||||
if (exists.Items.Any())
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.accounts_exists);
|
||||
}
|
||||
if (!_motionCategoryRepository.Remove(exists))
|
||||
{
|
||||
return result.Set(ItemCategoryRemoveResult.failure);
|
||||
}
|
||||
|
||||
return result.Set(exists);
|
||||
}
|
||||
|
||||
public List<Item> GetAll(Guid userId)
|
||||
{
|
||||
return _motionRepository.GetAll(userId);
|
||||
}
|
||||
|
||||
public List<Item> GetByCategory(Guid userId, Guid categoryId)
|
||||
{
|
||||
return _motionRepository.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);
|
||||
if (motion == null)
|
||||
{
|
||||
return result.Set(GeneralExecStatus.not_found);
|
||||
}
|
||||
|
||||
motion.Category.Id = categoryId;
|
||||
//motion.CategoryId = categoryId;
|
||||
_motionRepository.Update(motion);
|
||||
|
||||
return result.Set(motion);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user