Пример #1
0
        public async Task UpdateUsernameAsync(BlogUser userWithUpdatedUsername, string oldUsername)
        {
            //first try to create the username in the partition with partitionKey "unique_username" to confirm the username does not exist already
            var uniqueUsername = new UniqueUsername {
                Username = userWithUpdatedUsername.Username
            };

            //First create a user with a partitionkey as "unique_username" and the new username.  Using the same partitionKey "unique_username" will put all of the username in the same logical partition.
            //  Since there is a Unique Key on /username (per logical partition), trying to insert a duplicate username with partition key "unique_username" will cause a Conflict.
            //  See this question/answer https://stackoverflow.com/a/62438454/21579
            await _usersContainer.CreateItemAsync <UniqueUsername>(uniqueUsername, new PartitionKey(uniqueUsername.UserId));

            userWithUpdatedUsername.Action = "Update";

            //if we get past adding a new username for partition key "unique_username", then go ahead and update this user's username
            await _usersContainer.ReplaceItemAsync <BlogUser>(userWithUpdatedUsername, userWithUpdatedUsername.UserId, new PartitionKey(userWithUpdatedUsername.UserId));

            //then we need to delete the old "unique_username" for the username that just changed.
            var queryDefinition = new QueryDefinition("SELECT * FROM u WHERE u.userId = 'unique_username' AND u.type = 'unique_username' AND u.username = @username").WithParameter("@username", oldUsername);
            var query           = this._usersContainer.GetItemQueryIterator <BlogUniqueUsername>(queryDefinition);

            while (query.HasMoreResults)
            {
                var response = await query.ReadNextAsync();

                var oldUniqueUsernames = response.ToList();

                foreach (var oldUniqueUsername in oldUniqueUsernames)
                {
                    //Last delete the old unique username entry
                    await _usersContainer.DeleteItemAsync <BlogUser>(oldUniqueUsername.Id, new PartitionKey("unique_username"));
                }
            }
        }
Пример #2
0
        public async Task CreateUserAsync(BlogUser user)
        {
            var uniqueUsername = new UniqueUsername {
                Username = user.Username
            };

            //First create a user with a partitionkey as "unique_username" and the new username.  Using the same partitionKey "unique_username" will put all of the username in the same logical partition.
            //  Since there is a Unique Key on /username (per logical partition), trying to insert a duplicate username with partition key "unique_username" will cause a Conflict.
            //  This question/answer https://stackoverflow.com/a/62438454/21579
            await _usersContainer.CreateItemAsync <UniqueUsername>(uniqueUsername, new PartitionKey(uniqueUsername.UserId));

            user.Action = "Create";

            //if we get past adding a new username for partition key "unique_username", then go ahead and insert the new user.
            await _usersContainer.CreateItemAsync <BlogUser>(user, new PartitionKey(user.UserId));

            //await _usersContainer.CreateItemAsync<BlogUser>(user, new PartitionKey(user.UserId), new ItemRequestOptions { PreTriggers = new List<string> { "validateUserUsernameNotExists" } });
        }