public void Can_GetRoles()
        {
            var articleWithRolesDataResult = new ArticleWithRolesDataResult();

            articleWithRolesDataResult.Roles = "Role1, Role2";

            var actual = articleWithRolesDataResult.GetRoles();

            Assert.Equal(2, actual.Length);
            Assert.Equal("Role1", actual[0]);
            Assert.Equal("Role2", actual[1]);
        }
示例#2
0
        public async Task <ArticleWithRolesDataResult> GetArticleWithRolesAsync(Guid articleId)
        {
            using (NpgsqlConnection connection = new NpgsqlConnection(base.connectionString))
            {
                string sql = @"SELECT
                                    a.""Id"",
                                    a.""Title"",
	                                a.""Body"",
	                                a.""Timestamp"",
	                                string_agg(r.""Name"", ', ') AS ""Roles""
                                    FROM ""Articles"" a
                                    LEFT JOIN ""ArticleRoles"" ar ON ar.""ArticleId"" = a.""Id""
                                    LEFT JOIN ""AspNetRoles"" r ON r.""Id"" = ar.""RoleId""
                                    WHERE a.""Id"" = :ArticleId
                                    GROUP BY a.""Id"", a.""Title"", a.""Body"", a.""Timestamp"";
                            ";

                using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("ArticleId", articleId);

                    await connection.OpenAsync();

                    using (NpgsqlDataReader dataReader = await command.ExecuteReaderAsync())
                    {
                        while (await dataReader.ReadAsync())
                        {
                            var articleWithRolesDataResult = new ArticleWithRolesDataResult
                            {
                                Id        = (Guid)dataReader["Id"],
                                Title     = (string)dataReader["Title"],
                                Body      = (string)dataReader["Body"],
                                Timestamp = (DateTime)dataReader["Timestamp"],
                                Roles     = Convert.ToString(dataReader["Roles"])
                            };

                            return(articleWithRolesDataResult);
                        }
                    }
                }
            }
            return(null);
        }