namespace MyOffice.Services.Account; using MyOffice.Core; using MyOffice.Core.Extensions; using MyOffice.Data.Models.Accounts; using MyOffice.Services.Account.Domain; public partial class AccountService { public List GetAllAccounts(Guid userId) { return _accountRepository .GetAll(userId) .ToDto(_mapper); } public List GetByCategory(Guid userId, Guid categoryId) { return _accountRepository .GetByCategory(userId, categoryId) .ToDto(_mapper); } public List FindAccounts(Guid userId, string term) { return _accountRepository .FindAccounts(userId, term) .ToDto(_mapper); } public List GetByCategoryDetailed(Guid userId, Guid categoryId) { return _accountRepository .GetByCategoryDetailed(userId, categoryId) .ToDto(_mapper); } public Exec GetByIdDetailed(Guid userId, Guid id) { var result = new Exec(GeneralExecStatus.success); var account = _accountRepository.GetByIdDetailed(userId, id); if (account == null) { return result.Set(GeneralExecStatus.not_found); } return result.Set(account.ToDto(_mapper));; } public Exec AccountAdd(Guid userId, AccountAdd input) { if (input == null) throw new ArgumentNullException(nameof(input)); var result = new Exec(AccountAddStatus.success); var category = _accountCategoryRepository.Get(userId, input.CategoryId); if (category == null) { return result.Set(AccountAddStatus.category_not_found); } var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId); if (currency == null) { return result.Set(AccountAddStatus.currency_not_found); } var account = new Account { Id = Guid.NewGuid(), Name = input.Name, CurrencyGlobalId = currency.CurrencyGlobalId, OwnerId = userId, }; account.Categories = new List { new() { AccountId = account.Id, CategoryId = category.Id, } }; account.AccessRights = new List { new() { AccountId = account.Id, UserId = userId, OwnerId = userId, IsAllowManage = true, IsAllowRead = true, IsAllowWrite = true, } }; if (!_accountRepository.Add(account)) { return result.Set(AccountAddStatus.failure); } // TODO: Add category refactoring if (!_accountAccountCategoryRepository.Add(account.Categories.FirstOrDefault()!)) { return result.Set(AccountAddStatus.failure); } // TODO: Add access refactoring if (!_accountAccessRepository.Add(account.AccessRights.FirstOrDefault()!)) { return result.Set(AccountAddStatus.failure); } // TODO: Add account update AccessRights ??? var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId); if (access != null && access.Type.ToString() != input.Type) { if (Enum.TryParse(input.Type, out var enumType)) { access.Type = enumType; _accountAccessRepository.Update(access); } } return result.Set(account.ToDto(_mapper)); } public Exec AccountDelete(Guid userId, string id) { var result = new Exec(GeneralExecStatus.success); var account = _accountRepository.Get(userId, id.AsGuid()); if (account == null) { return result.Set(GeneralExecStatus.not_found); } if (account.Motions!.Any()) { return result.Set(GeneralExecStatus.not_found); } _accountRepository.Delete(account); result.Set(account.ToDto(_mapper)); return result; } public Exec AccountUpdate(Guid userId, Guid accountId, AccountEdit input) { if (input == null) throw new ArgumentNullException(nameof(input)); var result = new Exec(AccountEditStatus.success); AccountCategory? category = null; if (input.CategoryId.HasValue) { category = _accountCategoryRepository.Get(userId, input.CategoryId.Value); if (category == null) { return result.Set(AccountEditStatus.category_not_found); } } var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId); if (currency == null) { return result.Set(AccountEditStatus.currency_not_found); } var account = _accountRepository.Get(userId, accountId); if (account == null) { return result.Set(AccountEditStatus.not_found); } account.Name = input.Name; if (account.CurrencyGlobalId != currency.CurrencyGlobalId) { account.CurrencyGlobalId = currency.CurrencyGlobalId; } if (category != null && account.Categories!.All(x => x.CategoryId != category.Id)) { _accountAccountCategoryRepository.Add(new AccountAccountCategory { AccountId = account.Id, CategoryId = category.Id, }); } if (!_accountRepository.Update(account)) { return result.Set(AccountEditStatus.failure); } var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId); if (access != null && access.Type.ToString() != input.Type) { if (Enum.TryParse(input.Type, out var enumType)) { access.Type = enumType; _accountAccessRepository.Update(access); } } return result.Set(account.ToDto(_mapper)); } }