public async Task Demote(string userId)
        {
            DappUser user = await userManager.FindByIdAsync(userId);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            try
            {
                UserRole staff = roleManager.Roles.FirstOrDefault(x => x.Name == "staff");
                List <IdentityUserRole <Guid> > list   = userRoleRepo.GetAll();
                IdentityUserRole <Guid>         result = list.FirstOrDefault(x => x.UserId.ToString() == userId);
                userRoleRepo.Remove(result);
                userRoleRepo.Add(new IdentityUserRole <Guid>()
                {
                    RoleId = staff.Id,
                    UserId = user.Id
                });
                await work.SaveAsync();
            }
            catch
            {
                throw new DataSaveException("Failed demotion");
            }
        }
        public async Task <string> CreateUser(RegisterViewModel model)
        {
            try
            {
                DappUser user = userRepo.FirstOrDefault(x => x.PublicAddress == model.PublicAddress);
                if (user != null)
                {
                    throw new Exception("Registration failed");
                }
                user = mapper.Map <RegisterViewModel, DappUser>(model);
                userRepo.Add(user);
                Random random = new Random();
                user.Nonce = random.Next(10000, 100000);
                await work.SaveAsync();

                UserRole staff = roleManager.Roles.FirstOrDefault(x => x.Name == "staff");
                userRoleRepo.Add(new IdentityUserRole <Guid>()
                {
                    RoleId = staff.Id,
                    UserId = user.Id
                });
                await work.SaveAsync();

                return(user.Id.ToString());
            }
            catch (Exception e)
            {
                throw new DataSaveException("Registration failed");
            }
        }
        public UserDataViewModel GetUserWithPublicAddress(string publicAddress)
        {
            DappUser          user   = userRepo.FirstOrDefault(x => x.PublicAddress == publicAddress);
            UserDataViewModel result = mapper.Map <DappUser, UserDataViewModel>(user);

            result.Role = GetUserRoles(user.Id.ToString()).GetAwaiter().GetResult().LastOrDefault();
            return(result);
        }
        public long GetNonce(string publicAddress)
        {
            DappUser user = userRepo.FirstOrDefault(x => x.PublicAddress == publicAddress);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            return(user.Nonce);
        }
        public async Task <List <string> > GetUserRoles(string userId)
        {
            DappUser user = await userManager.FindByIdAsync(userId);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            var roles = await userManager.GetRolesAsync(user);

            return(roles.ToList());
        }
        public async Task <UserDataViewModel> GetUserInfo(string userId)
        {
            DappUser user = await userManager.FindByIdAsync(userId);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            UserDataViewModel result = mapper.Map <DappUser, UserDataViewModel>(user);

            result.Role = GetUserRoles(user.Id.ToString()).GetAwaiter().GetResult().LastOrDefault();
            return(result);
        }
        public async Task UnlockUser(string userId)
        {
            DappUser user = await userManager.FindByIdAsync(userId);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            user.IsActive = true;
            try
            {
                await work.SaveAsync();
            }
            catch
            {
                throw new DataSaveException("Failed to unlock user");
            }
        }
        public async Task <CapitalDataViewModel> CreateCapital(CreateCapitalViewModel request)
        {
            DappUser user = userRepo.FirstOrDefault(x => x.PublicAddress == request.CreatorPublicAddress);

            if (user is null)
            {
                return(null);
            }
            Capital capital = mapper.Map <CreateCapitalViewModel, Capital>(request);

            capital.Creator      = user;
            capital.CreationDate = DateTime.Today;
            capital.Status       = CapitalStatus.Finished;
            capitalRepo.Add(capital);
            await work.SaveAsync();

            CapitalDataViewModel result = mapper.Map <Capital, CapitalDataViewModel>(capital);

            return(result);
        }
        public async Task <long> ChangeNonce(string publicAddress)
        {
            DappUser user = userRepo.FirstOrDefault(x => x.PublicAddress == publicAddress);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            Random random = new Random();

            user.Nonce = random.Next(10000, 100000);
            try
            {
                await work.SaveAsync();
            }
            catch
            {
                throw new DataSaveException("Nonce change failed");
            }
            return(user.Nonce);
        }
        public async Task UpdateUser(UpdateAccountViewModel model)
        {
            DappUser user = userRepo.FirstOrDefault(x => x.PublicAddress == model.PublicAddress);

            if (user is null)
            {
                throw new NotFoundException("User not found");
            }
            user.FullName    = model.FullName;
            user.Address     = model.Address;
            user.Email       = model.Email;
            user.PhoneNumber = model.PhoneNumber;
            try
            {
                await work.SaveAsync();
            }
            catch
            {
                throw new DataSaveException("User information update failed");
            }
        }