This commit is contained in:
2023-07-28 21:18:18 +03:00
parent 5ecefe5756
commit 90f0386bfe
53 changed files with 3067 additions and 267 deletions
+144 -40
View File
@@ -13,6 +13,7 @@
using Item.Domain;
using Microsoft.Extensions.Logging;
using MyOffice.Services.Account.Domain;
using System.Collections.Generic;
public class AccountService
{
@@ -20,6 +21,7 @@
private readonly IMapper _mapper;
private readonly IAccountCategoryRepository _accountCategoryRepository;
private readonly IAccountAccessRepository _accountAccessRepository;
private readonly IAccountAccessInviteRepository _accountAccessInviteRepository;
private readonly IAccountRepository _accountRepository;
private readonly ICurrencyRepository _currencyRepository;
private readonly IAccountAccountCategoryRepository _accountAccountCategoryRepository;
@@ -33,6 +35,7 @@
IMapper mapper,
IAccountCategoryRepository accountCategoryRepository,
IAccountAccessRepository accountAccessRepository,
IAccountAccessInviteRepository accountAccessInviteRepository,
IAccountRepository accountRepository,
ICurrencyRepository currencyRepository,
IAccountAccountCategoryRepository accountAccountCategoryRepository,
@@ -46,6 +49,7 @@
_mapper = mapper;
_accountCategoryRepository = accountCategoryRepository;
_accountAccessRepository = accountAccessRepository;
_accountAccessInviteRepository = accountAccessInviteRepository;
_accountRepository = accountRepository;
_currencyRepository = currencyRepository;
_accountAccountCategoryRepository = accountAccountCategoryRepository;
@@ -55,14 +59,16 @@
_itemService = itemService;
}
public List<AccountCategory> GetAllCategories(Guid userId)
public List<AccountCategoryDto> GetAllCategories(Guid userId)
{
return _accountCategoryRepository.GetAll(userId);
var categories = _accountCategoryRepository.GetAll(userId);
return _mapper.Map<List<AccountCategoryDto>>(categories);
}
public Exec<AccountCategory, GeneralExecStatus> GetCategory(Guid userId, Guid id)
public Exec<AccountCategoryDto, GeneralExecStatus> GetCategory(Guid userId, Guid id)
{
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var category = _accountCategoryRepository.Get(userId, id);
@@ -71,15 +77,15 @@
return result.Set(GeneralExecStatus.not_found);
}
return result.Set(category);
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategory, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryAdd(Guid userId, AccountCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
category.Id = Guid.NewGuid();
category.UserId = userId;
@@ -89,15 +95,15 @@
return result.Set(GeneralExecStatus.failure);
}
return result.Set(category);
return result.Set(_mapper.Map<AccountCategoryDto>(category));
}
public Exec<AccountCategory, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategory category)
public Exec<AccountCategoryDto, GeneralExecStatus> CategoryUpdate(Guid userId, Guid id, AccountCategory category)
{
if (category == null)
throw new ArgumentNullException(nameof(category));
var result = new Exec<AccountCategory, GeneralExecStatus>(GeneralExecStatus.success);
var result = new Exec<AccountCategoryDto, GeneralExecStatus>(GeneralExecStatus.success);
var exists = _accountCategoryRepository.Get(userId, id);
if (exists == null)
@@ -112,12 +118,12 @@
return result.Set(GeneralExecStatus.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<AccountCategoryDto>(exists));
}
public Exec<AccountCategory, AccountCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
public Exec<AccountCategoryDto, AccountCategoryRemoveResult> CategoryRemove(Guid userId, Guid id)
{
var result = new Exec<AccountCategory, AccountCategoryRemoveResult>(AccountCategoryRemoveResult.success);
var result = new Exec<AccountCategoryDto, AccountCategoryRemoveResult>(AccountCategoryRemoveResult.success);
var exists = _accountCategoryRepository.Get(userId, id);
if (exists == null)
@@ -134,12 +140,14 @@
return result.Set(AccountCategoryRemoveResult.failure);
}
return result.Set(exists);
return result.Set(_mapper.Map<AccountCategoryDto>(exists));
}
public List<AccountDto> GetAllAccounts(Guid userId)
{
return _mapper.Map<List<AccountDto>>(_accountRepository.GetAll(userId));
var accounts = _accountRepository.GetAll(userId);
return _mapper.Map<List<AccountDto>>(accounts, o => o.Items.Add("UserId", userId));
}
public List<AccountDto> GetByCategory(Guid userId, Guid categoryId)
@@ -165,22 +173,22 @@
return result.Set(_mapper.Map<AccountDetailedDto>(account));
}
public Exec<Account, AccountAddResult> AccountAdd(Guid userId, AccountAdd input)
public Exec<Account, AccountAddStatus> AccountAdd(Guid userId, AccountAdd input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<Account, AccountAddResult>(AccountAddResult.success);
var result = new Exec<Account, AccountAddStatus>(AccountAddStatus.success);
var category = _accountCategoryRepository.Get(userId, input.CategoryId);
if (category == null)
{
return result.Set(AccountAddResult.category_not_found);
return result.Set(AccountAddStatus.category_not_found);
}
var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
if (currency == null)
{
return result.Set(AccountAddResult.currency_not_found);
return result.Set(AccountAddStatus.currency_not_found);
}
var account = new Account
@@ -188,6 +196,7 @@
Id = Guid.NewGuid(),
Name = input.Name,
CurrencyGlobalId = currency.CurrencyGlobalId,
OwnerId = userId,
};
account.Categories = new List<AccountAccountCategory>
@@ -204,6 +213,7 @@
{
AccountId = account.Id,
UserId = userId,
OwnerId = userId,
IsAllowManage = true,
IsAllowRead = true,
IsAllowWrite = true,
@@ -212,7 +222,7 @@
if (!_accountRepository.Add(account))
{
return result.Set(AccountAddResult.failure);
return result.Set(AccountAddStatus.failure);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
@@ -249,12 +259,12 @@
return result;
}
public Exec<Account, AccountEditResult> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)
public Exec<Account, AccountEditStatus> AccountUpdate(Guid userId, Guid accountId, AccountEdit input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
var result = new Exec<Account, AccountEditResult>(AccountEditResult.success);
var result = new Exec<Account, AccountEditStatus>(AccountEditStatus.success);
AccountCategory? category = null;
if (input.CategoryId.HasValue)
@@ -262,20 +272,20 @@
category = _accountCategoryRepository.Get(userId, input.CategoryId.Value);
if (category == null)
{
return result.Set(AccountEditResult.category_not_found);
return result.Set(AccountEditStatus.category_not_found);
}
}
var currency = _currencyRepository.GetByGlobalCurrency(userId, input.CurrencyId);
if (currency == null)
{
return result.Set(AccountEditResult.currency_not_found);
return result.Set(AccountEditStatus.currency_not_found);
}
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(AccountEditResult.not_found);
return result.Set(AccountEditStatus.not_found);
}
account.Name = input.Name;
@@ -298,7 +308,7 @@
if (!_accountRepository.Update(account))
{
return result.Set(AccountEditResult.failure);
return result.Set(AccountEditStatus.failure);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == userId);
@@ -332,7 +342,7 @@
return result.Set(categories);
}
public Exec<List<Motion>, MotionAddResult> MotionAdd(
public Exec<List<Motion>, MotionAddStatus> MotionAdd(
Guid userId,
Guid accountId,
MotionAddUpdate motion
@@ -343,18 +353,18 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<List<Motion>, MotionAddResult>(MotionAddResult.success);
var result = new Exec<List<Motion>, MotionAddStatus>(MotionAddStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(MotionAddResult.account_not_found);
return result.Set(MotionAddStatus.account_not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionAddResult.failure);
return result.Set(MotionAddStatus.failure);
}
var motionDb = new Motion
@@ -371,7 +381,7 @@
if (!_motionRepository.Add(motionDb))
{
return result.Set(MotionAddResult.failure);
return result.Set(MotionAddStatus.failure);
}
result.Set(new List<Motion>());
@@ -415,7 +425,7 @@
return result;
}
public Exec<Motion, MotionUpdateResult> MotionUpdate(
public Exec<Motion, MotionUpdateStatus> MotionUpdate(
Guid userId,
Guid accountId,
Guid motionId,
@@ -427,18 +437,18 @@
if (motion.Item == null)
throw new ArgumentNullException(nameof(motion.Item));
var result = new Exec<Motion, MotionUpdateResult>(MotionUpdateResult.success);
var result = new Exec<Motion, MotionUpdateStatus>(MotionUpdateStatus.success);
var exists = _motionRepository.Get(userId, motionId);
if (exists == null || exists.AccountId != accountId)
{
return result.Set(MotionUpdateResult.not_found);
return result.Set(MotionUpdateStatus.not_found);
}
var itemExec = _itemService.GetOrCreate(userId, motion.Item);
if (itemExec.Status != ItemGetOrAddResult.success)
{
return result.Set(MotionUpdateResult.failure);
return result.Set(MotionUpdateStatus.failure);
}
exists.Item.Id = itemExec.Result!.Id;
@@ -449,7 +459,7 @@
if (!_motionRepository.Update(exists))
{
return result.Set(MotionUpdateResult.failure);
return result.Set(MotionUpdateStatus.failure);
}
@@ -476,23 +486,23 @@
return result.Set(motions);
}
public Exec<Motion, MotionDeleteResult> MotionRemove(
public Exec<Motion, MotionDeleteStatus> MotionRemove(
Guid userId,
Guid accountId,
Guid motionId
)
{
var result = new Exec<Motion, MotionDeleteResult>(MotionDeleteResult.success);
var result = new Exec<Motion, MotionDeleteStatus>(MotionDeleteStatus.success);
var exists = _motionRepository.Get(userId, motionId);
if (exists == null || exists.AccountId != accountId)
{
return result.Set(MotionDeleteResult.not_found);
return result.Set(MotionDeleteStatus.not_found);
}
if (!_motionRepository.Remove(exists))
{
return result.Set(MotionDeleteResult.failure);
return result.Set(MotionDeleteStatus.failure);
}
return result.Set(exists);
@@ -502,5 +512,99 @@
{
return _accountRepository.FindAccounts(userId, term);
}
public Exec<AccountAccessInvite, AccessInviteStatus> AccessInvite(Guid userId, Guid accountId, string email, bool isAllowWrite)
{
if (email == null)
throw new ArgumentNullException(nameof(email));
var result = new Exec<AccountAccessInvite, AccessInviteStatus>(AccessInviteStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(AccessInviteStatus.account_not_found);
}
if (account.AccessRights!.Any(x => x.User!.Email.EqualsIgnoreCase(email)))
{
return result.Set(AccessInviteStatus.account_not_found);
}
var access = _accountAccessInviteRepository.Get(userId, email);
if (access != null)
{
return result.Set(AccessInviteStatus.access_exists);
}
var invite = new AccountAccessInvite
{
Id = Guid.NewGuid(),
CreatedOn = DateTime.UtcNow,
UserId = userId,
Email = email,
IsAllowWrite = isAllowWrite,
};
_accountAccessInviteRepository.Add(invite);
return result.Set(invite);
}
public Exec<AccountDto, GeneralExecStatus> AccessUpdate(Guid userId, Guid accountId, List<AccountAccessDto> accesses)
{
if (accesses == null)
throw new ArgumentNullException(nameof(accesses));
var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null)
{
return result.Set(GeneralExecStatus.not_found);
}
foreach (var access in account.AccessRights!)
{
var newAccess = accesses.FirstOrDefault(x => x.UserId == access.UserId);
if (newAccess != null && newAccess.IsAllowWrite != access.IsAllowWrite)
{
access.IsAllowWrite = newAccess.IsAllowWrite;
_accountAccessRepository.Update(access);
}
}
account = _accountRepository.Get(userId, accountId);
return result.Set(_mapper.Map<AccountDto>(account));
}
public Exec<AccountDto, GeneralExecStatus> AccessDelete(Guid userId, Guid accountId, Guid accessUserId)
{
var result = new Exec<AccountDto, GeneralExecStatus>(GeneralExecStatus.success);
var account = _accountRepository.Get(userId, accountId);
if (account == null || !account.AccessRights!.Any() || account.AccessRights!.Count() == 1)
{
return result.Set(GeneralExecStatus.not_found);
}
var access = account.AccessRights!.FirstOrDefault(x => x.UserId == accessUserId);
if (access == null)
{
return result.Set(GeneralExecStatus.not_found);
}
if (access.OwnerId == access.UserId)
{
return result.Set(GeneralExecStatus.not_found);
}
_accountAccessRepository.Delete(access);
account = _accountRepository.Get(userId, accountId);
return result.Set(_mapper.Map<AccountDto>(account));
}
}
}
@@ -1,30 +0,0 @@
using MyOffice.Services.Account.Domain;
namespace MyOffice.Services.Account;
using AutoMapper;
using Data.Models.Accounts;
using Data.Models.Users;
public class AccountServiceProfile : Profile
{
public AccountServiceProfile()
{
CreateMap<User, UserDto>();
CreateMap<Account, AccountDto>()
.ForMember(x => x.HasMotions, o => o.MapFrom(x => x.Motions.Any()))
;
CreateMap<AccountDetailed, AccountDetailedDto>();
CreateMap<AccountAccess, AccountAccessDto>();
CreateMap<AccountAccountCategory, AccountAccountCategoryDto>();
CreateMap<AccountCategory, AccountCategoryDto>()
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
;
}
}
@@ -0,0 +1,9 @@
namespace MyOffice.Services.Account.Domain;
public enum AccessInviteStatus
{
success,
account_not_found,
access_exists,
invite_exists,
}
@@ -5,10 +5,24 @@ using Data.Models.Users;
public class AccountAccessDto
{
/// <summary>
/// Current user
/// </summary>
public Guid CurrenUserId { get; set; }
public Guid AccountId { get; set; }
public AccountDto? Account { get; set; }
/// <summary>
/// User can access to account
/// </summary>
public Guid UserId { get; set; }
public User? User { get; set; }
/// <summary>
/// Who add access
/// </summary>
public Guid OwnerId { get; set; }
public User? Owner { get; set; }
public bool IsAllowRead { get; set; }
public bool IsAllowWrite { get; set; }
@@ -1,6 +1,6 @@
namespace MyOffice.Services.Account.Domain
{
public enum AccountAddResult
public enum AccountAddStatus
{
success,
failure,
@@ -1,6 +1,9 @@
public class AccountCategoryDto
namespace MyOffice.Services.Account.Domain;
public class AccountCategoryDto
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Name { get; set; } = null!;
public bool AllowDelete { get; set; }
}
+22 -21
View File
@@ -1,22 +1,23 @@
namespace MyOffice.Services.Account.Domain
{
using MyOffice.Data.Models.Currencies;
using System;
namespace MyOffice.Services.Account.Domain;
public class AccountDto
{
public Guid Id { get; set; }
public string CurrencyGlobalId { get; set; } = null!;
public CurrencyGlobal? CurrencyGlobal { get; set; }
public Guid CurrencyId { get; set; }
public Currency? Currency { get; set; }
public Guid UserId { get; set; }
public UserDto? User { get; set; }
public Guid OwnerId { get; set; }
public UserDto? Owner { get; set; }
public string Name { get; set; } = null!;
public bool HasMotions { get; set; }
public List<AccountAccessDto>? AccessRights { get; set; }
public List<AccountAccountCategoryDto>? Categories { get; set; }
}
}
using MyOffice.Data.Models.Currencies;
using System;
public class AccountDto
{
public Guid Id { get; set; }
public Guid CurrentUserId { get; set; }
public string CurrencyGlobalId { get; set; } = null!;
public CurrencyGlobal? CurrencyGlobal { get; set; }
public Guid CurrencyId { get; set; }
public Currency? Currency { get; set; }
public Guid UserId { get; set; }
public UserDto? User { get; set; }
public Guid OwnerId { get; set; }
public UserDto? Owner { get; set; }
public string Name { get; set; } = null!;
public bool HasMotions { get; set; }
public string Type { get; set; } = null!;
public List<AccountAccessDto>? AccessRights { get; set; }
public List<AccountAccountCategoryDto>? Categories { get; set; }
}
@@ -1,6 +1,6 @@
namespace MyOffice.Services.Account.Domain;
public enum AccountEditResult
public enum AccountEditStatus
{
success,
failure,
@@ -1,6 +1,6 @@
namespace MyOffice.Services.Account.Domain;
public enum MotionAddResult
public enum MotionAddStatus
{
success,
failure,
@@ -1,6 +1,6 @@
namespace MyOffice.Services.Account.Domain;
public enum MotionDeleteResult
public enum MotionDeleteStatus
{
success,
failure,
@@ -1,6 +1,6 @@
namespace MyOffice.Services.Account.Domain;
public enum MotionUpdateResult
public enum MotionUpdateStatus
{
success,
failure,
+1 -1
View File
@@ -12,6 +12,6 @@ public class UserDto
public string? FullName { get; set; }
public string? Phone { get; set; }
public string CurrencyId { get; set; }
public string CurrencyId { get; set; } = null!;
public CurrencyGlobal? Currency { get; set; }
}
@@ -13,7 +13,7 @@ public class DashboardData
public decimal? Balance { get; set; }
public decimal? BalanceDebit { get; set; }
public decimal? BalanceCredit { get; set; }
public List<DashboardRestData> BalanceRests { get; set; }
public List<DashboardRestData> BalanceRests { get; set; } = null!;
}
public class DashboardRestData
@@ -0,0 +1,31 @@
namespace MyOffice.Services.Mapper;
using Account.Domain;
using AutoMapper;
using Data.Models.Accounts;
using Data.Models.Users;
public class AccountServiceProfile : Profile
{
public AccountServiceProfile()
{
CreateMap<User, UserDto>();
CreateMap<Account, AccountDto>()
.ForMember(x => x.HasMotions, o => o.MapFrom(x => x.Motions!.Any()))
.ForMember(x => x.CurrentUserId, o => o.MapFrom<UserIdResolver>())
.ForMember(x => x.Type, o => o.MapFrom((src, dst) => src.AccessRights!.FirstOrDefault(x => x.UserId == dst.CurrentUserId)!.Type.ToString()))
;
CreateMap<AccountDetailed, AccountDetailedDto>();
CreateMap<AccountAccess, AccountAccessDto>();
CreateMap<AccountAccountCategory, AccountAccountCategoryDto>();
CreateMap<AccountCategory, AccountCategoryDto>()
.ForMember(x => x.Id, o => o.MapFrom(x => x.Id))
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.Accounts!.Any()))
;
}
}
@@ -0,0 +1,21 @@
namespace MyOffice.Services.Mapper;
using AutoMapper;
using Identity;
public class UserIdResolver : IValueResolver<object, object, Guid>
{
private readonly IContextProvider _contextProvider;
public UserIdResolver(
IContextProvider contextProvider
)
{
_contextProvider = contextProvider;
}
public Guid Resolve(object source, object destination, Guid member, ResolutionContext context)
{
return _contextProvider.UserId;
}
}