Ejemplo n.º 1
0
        public async Task <OperationResult> SaveRoles(List <Role_User_Authorize_Dto> listRoleUser, string create_By)
        {
            var user_Account = listRoleUser[0].User_Account;
            var factory_ID   = listRoleUser[0].Factory_ID;

            if (!await IsUserExists(factory_ID, user_Account))
            {
                operationResult = new OperationResult {
                    Success = false, Message = "User not found."
                };
                return(operationResult);
            }

            // Remove current roles
            var userCurrentRoles = _roleUserRepository.FindAll(u => u.user_account == user_Account).ToList();

            _roleUserRepository.RemoveMultiple(userCurrentRoles);

            // Filter active roles
            listRoleUser = listRoleUser.FindAll(r => r.Status == true);

            // Create model to save
            var userRoleToSaveList = listRoleUser
                                     .Select(x => new RoleUser
            {
                user_account = x.User_Account,
                role_unique  = x.Role_Unique,
                create_by    = create_By,
                create_time  = DateTime.Now
            })
                                     .ToList();

            // Save model
            _roleUserRepository.AddMultiple(userRoleToSaveList);
            try
            {
                await _roleUserRepository.Save();
            }
            catch (System.Exception)
            {
                operationResult = new OperationResult {
                    Success = false, Message = "Updating user authorization failed on save."
                };
            }

            // Update User's Update_By & Update_Time
            var user = await _userRepository.FindSingle(x => x.Factory_ID == factory_ID && x.User_Account == user_Account);

            user.Update_By   = create_By;
            user.Update_Time = DateTime.Now;
            _userRepository.Update(user);
            try
            {
                await _userRepository.Save();

                operationResult = new OperationResult {
                    Success = true, Message = "User was successfully authorized."
                };
            }
            catch (System.Exception)
            {
                operationResult = new OperationResult {
                    Success = false, Message = "Updating user authorization failed on save."
                };
            }

            return(operationResult);
        }