Ejemplo n.º 1
0
        public async Task <Result> ChangeRolePropValueAsync <T>(int roleId, string adminRolePropertyName, T propertyValue)
        {
            var allowedPropertyNames = new[] {
                nameof(AdminRole.RoleName),
                nameof(AdminRole.Powers)
            };

            if (!allowedPropertyNames.Contains(adminRolePropertyName))
            {
                return(new Failure("不允许修改该字段内容"));
            }

            var adminRole = await _db.AdminRoles.FindAsync(roleId);

            if (adminRole == null)
            {
                return(new Failure($"管理组#{roleId}不存在"));
            }

            typeof(AdminRole).GetProperty(adminRolePropertyName) !.SetValue(adminRole, propertyValue);

            var validationResult = ValidatorHelper.ValidateProperty(adminRole, QueryableHelper.Select <AdminRole, T>(adminRolePropertyName));

            if (!validationResult.Ok)
            {
                return(validationResult);
            }

            await _db.Normalize().SaveChangesAsync();

            return(new Success());
        }