示例#1
0
        public async Task <InviteFriendPayload> InviteFriendAsync(
            InviteFriendInput input,
            [GlobalState] string currentUserEmail,
            PersonByEmailDataLoader personByEmail,
            [Service] ChatDbContext dbContext,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(input.Email))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The email address cannot be empty.")
                          .SetCode("EMAIL_EMPTY")
                          .Build());
            }

            IReadOnlyList <Person> people =
                await dbContext.People.Where(t =>
                                             t.Email == input.Email || t.Email == currentUserEmail)
                .ToArrayAsync(cancellationToken);

            if (people.Count < 1)
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The provided friend email address is invalid.")
                          .SetCode("EMAIL_UNKNOWN")
                          .Build());
            }

            people[0].Friends.Add(new PersonToFriend
            {
                PersionId = people[0].Id,
                FriendId  = people[1].Id
            });

            people[1].Friends.Add(new PersonToFriend
            {
                PersionId = people[1].Id,
                FriendId  = people[0].Id
            });

            await dbContext.SaveChangesAsync(cancellationToken);

            return(new InviteFriendPayload(
                       people[1],
                       input.ClientMutationId));
        }
示例#2
0
        public async Task <InviteFriendPayload> InviteFriendAsync(
            InviteFriendInput input,
            [GlobalState] string currentUserEmail,
            PersonByEmailDataLoader personByEmail,
            [Service] IPersonRepository personRepository,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(input.Email))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The email address cannot be empty.")
                          .SetCode("EMAIL_EMPTY")
                          .Build());
            }

            IReadOnlyList <Person> people =
                await personByEmail.LoadAsync(
                    cancellationToken, input.Email, currentUserEmail)
                .ConfigureAwait(false);

            if (people[0] is null)
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The provided friend email address is invalid.")
                          .SetCode("EMAIL_UNKNOWN")
                          .Build());
            }

            await personRepository.AddFriendIdAsync(
                people[1].Id, people[0].Id, cancellationToken)
            .ConfigureAwait(false);

            await personRepository.AddFriendIdAsync(
                people[0].Id, people[1].Id, cancellationToken)
            .ConfigureAwait(false);

            return(new InviteFriendPayload(
                       people[1].AddFriendId(people[0].Id),
                       input.ClientMutationId));
        }