public void SetupMutationDefinitions(ObjectGraphType type)
        {
            type.FieldAsync <RequestFriendResponseType>(
                "requestFriend",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = SchemaConstants.UserId
            }
                    ),
                resolve: async context =>
            {
                var target = await context.UserRecord(context.GetArgument <string>(SchemaConstants.UserId));
                if (target == null)
                {
                    return(context.Error(GraphQLErrors.UnknownUser()));
                }

                var response = new RequestFriendResponse
                {
                    RequesterId = context.User().GetUserId(),
                    RequestedId = target.Uid,
                    Time        = DateTime.Now
                };

                if (await _repository.ByUsers(response.RequesterId, response.RequestedId) != null)
                {
                    return(context.Error(GraphQLErrors.FriendRequestActive));
                }

                await _repository.AddFriendRequest(response.RequesterId, response.RequestedId, response.Time);
                // TODO Send push notification
                return(response);
            }
                ).RequiresAuthentication();
        }
Пример #2
0
        private Task ResolveSenders(AnyTimerInputType args, AnyTimer anyTimer,
                                    ResolveFieldContext <object> context)
        {
            return(Task.WhenAll(args.Senders.Select(async senderInput =>
            {
                if (anyTimer.ReceiverId.Equals(senderInput.User))
                {
                    context.Error(GraphQLErrors.ReceiverSameAsSender);
                    return;
                }

                if (!senderInput.User.Equals(anyTimer.CreatorId))
                {
                    var sender = await context.UserRecord(senderInput.User);
                    if (sender == null)
                    {
                        context.Error(GraphQLErrors.UnknownUser(senderInput.User));
                        return;
                    }

                    if ((await _friendRepository.ByUsers(anyTimer.CreatorId, sender.Uid))?.Status !=
                        FriendRequestStatus.Accepted)
                    {
                        context.Error(GraphQLErrors.NotFriends(sender.Uid));
                        return;
                    }
                }

                anyTimer.Senders.Add(new AnyTimerSender
                {
                    SenderId = senderInput.User,
                    Amount = senderInput.Amount,
                    AnyTimerId = anyTimer.Id
                });
            })));
        }
Пример #3
0
        public void SetupMutationDefinitions(ObjectGraphType type)
        {
            type.FieldAsync <AnyTimerType>(
                "newAnyTimer",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <NewAnyTimerInputType> > {
                Name = SchemaConstants.Args
            }
                    ),
                resolve: async context =>
            {
                var args = context.GetArgument <NewAnyTimerInputType>(SchemaConstants.Args);
                if (args.Senders.Count == 0)
                {
                    return((AnyTimer)context.Error(GraphQLErrors.NoSenders));
                }

                var user = await context.UserRecord();
                if (string.Equals(user.Uid, args.Receiver))
                {
                    return(context.Error(GraphQLErrors.ReceiverSameAsSender));
                }
                var anytimer = new AnyTimer
                {
                    Id          = Guid.NewGuid().ToString(),
                    CreatorId   = user.Uid,
                    CreatedTime = DateTime.Now,
                    LastUpdated = DateTime.Now,
                    Status      = AnyTimerStatus.Requested,
                    Reason      = args.Reason,
                };
                var receiver = await context.UserRecord(args.Receiver);
                if (receiver == null)
                {
                    return((AnyTimer)context.Error(GraphQLErrors.UnknownUser(args.Receiver)));
                }
                if ((await _friendRepository.ByUsers(user.Uid, receiver.Uid))?.Status !=
                    FriendRequestStatus.Accepted)
                {
                    return((AnyTimer)context.Error(GraphQLErrors.NotFriends(receiver.Uid)));
                }
                anytimer.ReceiverId = receiver.Uid;

                await ResolveSenders(args, anytimer, context);

                if (context.Errors.Count > 0)
                {
                    return(null);
                }

                UpdateStatus(anytimer, AnyTimerStatus.Requested);

                await _repository.Add(anytimer);
                // TODO Send push notifications

                return(anytimer);
            }
                ).RequiresAuthentication();

            type.FieldAsync <AnyTimerType>(
                "editAnyTimer",
                arguments: new QueryArguments(
                    new QueryArgument <IdGraphType> {
                Name = SchemaConstants.Id
            },
                    new QueryArgument <AnyTimerInputType> {
                Name = SchemaConstants.Args
            }
                    ),
                resolve: async context =>
            {
                var args = context.GetArgument <AnyTimerInputType>(SchemaConstants.Args);
                if (args.Senders.Count == 0)
                {
                    return((AnyTimer)context.Error(GraphQLErrors.NoSenders));
                }

                var anytimer = await _repository.ById(context.GetArgument <string>(SchemaConstants.Id));
                if (anytimer == null)
                {
                    return(context.Error(GraphQLErrors.UnknownAnyTimer));
                }
                var user = await context.UserRecord();
                if (!anytimer.CreatorId.Equals(user.Uid))
                {
                    return(context.Error(GraphQLErrors.NotCreator));
                }
                if (anytimer.Status == AnyTimerStatus.Cancelled || anytimer.Status == AnyTimerStatus.Accepted)
                {
                    return(context.Error(GraphQLErrors.NotEditable));
                }

                anytimer.Reason = args.Reason;
                UpdateStatus(anytimer, AnyTimerStatus.Edited);

                await ResolveSenders(args, anytimer, context);

                if (context.Errors.Count > 0)
                {
                    return(null);
                }

                await _repository.Update(anytimer);

                return(anytimer);
            }
                ).RequiresAuthentication();
        }