Ejemplo n.º 1
0
        public async Task ExecuteAsync(PostId postId, QueueId queueId)
        {
            postId.AssertNotNull("postId");
            queueId.AssertNotNull("queueId");

            var nextLiveDate = await this.getLiveDateOfNewQueuedPost.ExecuteAsync(queueId);

            var post = new Post(postId.Value)
            {
                QueueId  = queueId.Value,
                LiveDate = nextLiveDate
            };

            var parameters = new SqlGenerationParameters <Post, Post.Fields>(post)
            {
                UpdateMask = Post.Fields.QueueId | Post.Fields.LiveDate,
                Conditions = new[]
                {
                    WherePostLiveDateUniqueToQueue, // Perform locks in 'descending supersets' to avoid deadlock.
                    WherePostNotInQueue
                }
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                var failedConditionIndex = await connection.UpdateAsync(parameters);

                var concurrencyFailure = failedConditionIndex == 0;

                if (concurrencyFailure)
                {
                    throw new OptimisticConcurrencyException(string.Format("Failed to optimistically queue post with queue {0}", queueId));
                }
            }
        }
Ejemplo n.º 2
0
        public async Task ExecuteAsync(
            UserId userId,
            Username username,
            Email email,
            string exampleWork,
            Password password,
            DateTime timeStamp)
        {
            userId.AssertNotNull("userId");
            username.AssertNotNull("username");
            email.AssertNotNull("email");
            password.AssertNotNull("password");

            var passwordHash = this.userManager.PasswordHasher.HashPassword(password.Value);

            var user = new FifthweekUser
            {
                Id                  = userId.Value,
                UserName            = username.Value,
                Email               = email.Value,
                ExampleWork         = exampleWork,
                RegistrationDate    = timeStamp,
                LastSignInDate      = SqlDateTime.MinValue.Value,
                LastAccessTokenDate = SqlDateTime.MinValue.Value,
                SecurityStamp       = Guid.NewGuid().ToString(),
                PasswordHash        = passwordHash,
            };

            var parameters = new SqlGenerationParameters <FifthweekUser, FifthweekUser.Fields>(user)
            {
                Conditions = new[]
                {
                    WhereUsernameNotTaken,
                    WhereEmailNotTaken
                }
            };

            using (var transaction = TransactionScopeBuilder.CreateAsync())
            {
                using (var connection = this.connectionFactory.CreateConnection())
                {
                    var result = await connection.InsertAsync(parameters);

                    switch (result)
                    {
                    case 0: throw new RecoverableException("The username '" + username.Value + "' is already taken.");

                    case 1: throw new RecoverableException("The email address '" + email.Value + "' is already taken.");
                    }
                }

                await this.requestSnapshot.ExecuteAsync(userId, SnapshotType.Subscriber);

                transaction.Complete();
            }
        }
Ejemplo n.º 3
0
        public async Task ExecuteAsync(PostId postId, DateTime newTime, DateTime now)
        {
            postId.AssertNotNull("postId");
            newTime.AssertUtc("newTime");
            now.AssertUtc("now");

            var post = new Post(postId.Value)
            {
                LiveDate = this.scheduledDateClipping.Apply(now, newTime),
                QueueId  = null
            };

            var parameters = new SqlGenerationParameters <Post, Post.Fields>(post)
            {
                UpdateMask = Post.Fields.LiveDate | Post.Fields.QueueId
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                await connection.UpdateAsync(parameters);
            }
        }
        public async Task QueuePostAsync(Post post, IReadOnlyList <PostFile> postFiles)
        {
            post.AssertNotNull("post");

            if (!post.QueueId.HasValue)
            {
                throw new ArgumentException("Queue ID required", "post");
            }

            var queueId      = new QueueId(post.QueueId.Value);
            var nextLiveDate = await this.getLiveDateOfNewQueuedPost.ExecuteAsync(queueId);

            post.LiveDate = nextLiveDate;

            var parameters = new SqlGenerationParameters <Post, Post.Fields>(post)
            {
                Conditions = new[] { WherePostLiveDateUniqueToQueue }
            };

            using (var transaction = TransactionScopeBuilder.CreateAsync())
            {
                // The order we access tables should match RevisePostDbStatement.
                using (var connection = this.connectionFactory.CreateConnection())
                {
                    var success = -1 == await connection.InsertAsync(parameters);

                    if (!success)
                    {
                        throw new OptimisticConcurrencyException(string.Format("Failed to optimistically queue post to channel {0}", post.ChannelId));
                    }

                    await connection.InsertAsync(postFiles);
                }

                transaction.Complete();
            }
        }