Ejemplo n.º 1
0
        public async Task <Role> GetRole(int id)
        {
            var query = $@"SELECT 
                            R.`id`     AS {nameof(Role.Id)},
                            R.`name`   AS {nameof(Role.Name)},
                            A.id       AS {nameof(User.Id)},
                            A.name     AS {nameof(User.Name)},   
                            C.id       AS {nameof(Claim.Id)},
                            C.name     AS {nameof(Claim.Name)}                            
                            FROM Vanlune.Roles AS R
                            LEFT JOIN `Vanlune`.RoleUser AS AR ON AR.idRoles = R.id
                            LEFT JOIN `Vanlune`.`Accounts` AS A ON A.id = AR.idUser
                            LEFT JOIN `Vanlune`.ClaimRole AS CR ON CR.idRole = R.id
                            LEFT JOIN `Vanlune`.Claim AS C ON C.id = CR.idClaim
                            WHERE R.`id` = @id";

            using var connection = _mySqlConnHelper.MySqlConnection();

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }

            Role currRole = default;

            var result = await connection.QueryAsync <Role, User, Claim, Role>(query,
                                                                               (role, user, claim) =>
            {
                if (currRole == null)
                {
                    currRole = role;
                }
                if (claim != null && claim.Id > 0)
                {
                    currRole.AddClaimToRole(claim);
                }

                return(role);
            },
                                                                               new
            {
                id
            }, splitOn : $"{nameof(Role.Id)},{nameof(User.Id)},{nameof(Claim.Id)}");

            return(currRole);
        }