Ejemplo n.º 1
0
        public async Task <Question> FindByIdAsync([Required] FindByIdParams parameters)
        {
            // Check if user is authenticated
            await _authentication.AuthenticateAsync(parameters.AuthToken);

            // Returns the given id
            return(await _repository.Questions.FindAsync(parameters.QuestionId));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <Answer> > FindByQuestionAsync([Required] FindByQuestionParams parameters)
        {
            // Check if player is authenticated
            await _authentication.AuthenticateAsync(parameters.AuthToken);

            if (!await _repository.Questions.AnyAsync(x => x.Id == parameters.QuestionId))
            {
                throw new SystemException("Question doesn't exist.");
            }

            // Select all the answers for the provided question
            return(await _repository.Answers.Where(x => x.QuestionId == parameters.QuestionId).ToListAsync());
        }
Ejemplo n.º 3
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var isMasterCommand  = CreateInitialIsMasterCommand(connection.Settings.Authenticators);
            var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand);
            var isMasterResult   = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol();
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, isMasterResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);

            try
            {
                var getLastErrorProtocol = CreateGetLastErrorProtocol();
                var getLastErrorResult   = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
Ejemplo n.º 4
0
        public async Task <Topic> FindByIdAsync([Required] FindByIdParams parameters)
        {
            // Check if user is authenticated
            await _authentication.AuthenticateAsync(parameters.AuthToken);

            return(await _repository.Topics.FindAsync(parameters.TopicId));
        }
        public async Task <ConnectionDescription> AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            Ensure.IsNotNull(description, nameof(description));

            var authenticators = GetAuthenticators(connection.Settings);
            await AuthenticationHelper.AuthenticateAsync(connection, description, authenticators, cancellationToken).ConfigureAwait(false);

            var connectionIdServerValue = description.HelloResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = await getLastErrorProtocol
                                               .ExecuteAsync(connection, cancellationToken)
                                               .ConfigureAwait(false);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }
Ejemplo n.º 6
0
        public async Task <IEnumerable <Comment> > ListAsync([Required] ListParams parameters)
        {
            // Check if player is authenticated
            await _authentication.AuthenticateAsync(parameters.AuthToken);

            return(await _repository.Comments.ToListAsync());
        }
Ejemplo n.º 7
0
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));

            var isMasterCommand  = new BsonDocument("isMaster", 1);
            var isMasterProtocol = new CommandWireProtocol <BsonDocument>(
                DatabaseNamespace.Admin,
                isMasterCommand,
                true,
                BsonDocumentSerializer.Instance,
                null);
            var isMasterResult = new IsMasterResult(await isMasterProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var buildInfoCommand  = new BsonDocument("buildInfo", 1);
            var buildInfoProtocol = new CommandWireProtocol <BsonDocument>(
                DatabaseNamespace.Admin,
                buildInfoCommand,
                true,
                BsonDocumentSerializer.Instance,
                null);
            var buildInfoResult = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var connectionId = connection.ConnectionId;
            var description  = new ConnectionDescription(connectionId, isMasterResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);

            try
            {
                var getLastErrorCommand  = new BsonDocument("getLastError", 1);
                var getLastErrorProtocol = new CommandWireProtocol <BsonDocument>(
                    DatabaseNamespace.Admin,
                    getLastErrorCommand,
                    true,
                    BsonDocumentSerializer.Instance,
                    null);
                var getLastErrorResult = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);

                BsonValue connectionIdBsonValue;
                if (getLastErrorResult.TryGetValue("connectionId", out connectionIdBsonValue))
                {
                    connectionId = connectionId.WithServerValue(connectionIdBsonValue.ToInt32());
                    description  = description.WithConnectionId(connectionId);
                }
            }
            catch
            {
                // if we couldn't get the server's connection id, so be it.
            }

            return(description);
        }
Ejemplo n.º 8
0
        public async Task UpdateAsync([Required] UpdateParams parameters)
        {
            // Check if player is authenticated
            await _authentication.AuthenticateAsync(parameters.AuthToken);

            var player = await _repository.Players.FirstAsync(x => x.Id == parameters.PlayerId);

            player.Name      = parameters.Name;
            player.Gender    = parameters.Gender;
            player.Birthdate = parameters.Birthdate;
            player.Edited    = DateTime.UtcNow;

            // The sql statement is being executed
            await _repository.SaveChangesAsync();
        }
        public async Task <ConnectionDescription> InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            var authenticators = connection.Settings.AuthenticatorFactories.Select(f => f.Create()).ToList();
            var helloCommand   = CreateInitialHelloCommand(authenticators);
            var helloProtocol  = HelloHelper.CreateProtocol(helloCommand, _serverApi);
            var helloResult    = await HelloHelper.GetResultAsync(connection, helloProtocol, cancellationToken).ConfigureAwait(false);

            var buildInfoProtocol = CreateBuildInfoProtocol(_serverApi);
            var buildInfoResult   = new BuildInfoResult(await buildInfoProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false));

            var description = new ConnectionDescription(connection.ConnectionId, helloResult, buildInfoResult);

            await AuthenticationHelper.AuthenticateAsync(connection, description, authenticators, cancellationToken).ConfigureAwait(false);

            var connectionIdServerValue = helloResult.ConnectionIdServerValue;

            if (connectionIdServerValue.HasValue)
            {
                description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
            }
            else
            {
                try
                {
                    var getLastErrorProtocol = CreateGetLastErrorProtocol(_serverApi);
                    var getLastErrorResult   = await getLastErrorProtocol
                                               .ExecuteAsync(connection, cancellationToken)
                                               .ConfigureAwait(false);

                    description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
                }
                catch
                {
                    // if we couldn't get the server's connection id, so be it.
                }
            }

            return(description);
        }