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 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 CategoryAdd(Guid userId, ItemCategory category) { if (category == null) throw new ArgumentNullException(nameof(category)); var result = new Exec(GeneralExecStatus.success); category.Id = Guid.NewGuid(); category.UserId = userId; if (!_motionCategoryRepository.Add(category)) { return result.Set(GeneralExecStatus.failure); } return result.Set(category); } public Exec CategoryUpdate(Guid userId, Guid id, ItemCategory category) { if (category == null) throw new ArgumentNullException(nameof(category)); var result = new Exec(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 CategoryRemove(Guid userId, Guid id) { var result = new Exec(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 GetAll(Guid userId) { return _motionRepository.GetAll(userId); } public List GetByCategory(Guid userId, Guid categoryId) { return _motionRepository.GetByCategory(userId, categoryId); } public Exec Update(Guid userId, Guid motionId, Guid categoryId) { var result = new Exec(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); } } }