示例#1
0
        private async Task <List <Guid> > GetFollowersIds(GetFollowersQuery request)
        {
            var query = "SELECT pp.TargetPersonId from PersonsPersons pp\n" +
                        "WHERE pp.FollowerPersonId=@followerPersonId";
            var queryData = new
            {
                followerPersonId = request.FollowerPersonId
            };

            await using var connection = new SqlConnection(this.readConnectionString);
            var queryAsyncRaw = await connection.QueryAsync <Guid>(query, queryData);

            var followersIds = queryAsyncRaw.ToList();

            return(followersIds);
        }
示例#2
0
        public async Task <List <SubscribedReaderViewModel> > Handle(GetFollowersQuery request, CancellationToken cancellationToken)
        {
            var followersIds = await GetFollowersIds(request);

            if (!followersIds.Any())
            {
                return(new List <SubscribedReaderViewModel>());
            }

            var confidentialClientApplication = ConfidentialClientApplicationBuilder
                                                .Create(this.graphApiSettings.ClientId)
                                                .WithClientSecret(this.graphApiSettings.ClientSecret)
                                                .WithTenantId(this.graphApiSettings.TenantId)
                                                .Build();

            var scopes = new string[] { "https://graph.microsoft.com/.default" };

            var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(
                                                                async requestMessage =>
            {
                var authResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
                requestMessage.Headers.Authorization =
                    new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            })
                                                            );

            var quotedIds = followersIds.Select(x => $"'{x}'");
            var filter    = "id eq '" + followersIds.First() + "'";

            if (quotedIds.Count() > 1)
            {
                var ids = string.Join(string.Empty, quotedIds.Skip(1).Select(x => $" or id eq {x}"));
                filter += ids;
            }

            var req = await graphServiceClient.Users
                      .Request()
                      .Select("displayName,id")
                      .Filter(filter)
                      .GetAsync();

            var results = req.Select(res => new SubscribedReaderViewModel(res.Id, res.DisplayName)).ToList();

            return(results);
        }