public async Task SaveTODOListAsync()
        {
            Console.WriteLine("----- Executing Save -----");
            #region datamodel_construct_save
            var list = new TODOList
            {
                User       = "******",
                ListId     = "generated-list-id",
                Complete   = false,
                Name       = "ExampleList",
                CreateDate = DateTime.UtcNow,
                UpdateDate = DateTime.UtcNow,
                Items      = new List <TODOListItem>
                {
                    new TODOListItem {
                        Description = "Task1", Complete = true
                    },
                    new TODOListItem {
                        Description = "Task2", Complete = false
                    }
                }
            };

            await this.Context.SaveAsync(list);

            Console.WriteLine("List saved");
            #endregion
            Console.WriteLine();
        }
        public static async Task SaveTODOListAsync()
        {
            var config = new DynamoDBContextConfig
            {
                Conversion     = DynamoDBEntryConversion.V2,
                ConsistentRead = true
            };
            var Context = new DynamoDBContext(new AmazonDynamoDBClient());

            #region test_save_lambda_todo

            var assignedEmail = "";
            if (string.IsNullOrEmpty(assignedEmail))
            {
                Console.Error.WriteLine("You must set the email to the \"assignedEmail\" variable");
                return;
            }

            var list = new TODOList
            {
                User       = "******",
                ListId     = Guid.NewGuid().ToString(),
                Complete   = false,
                Name       = "ExampleList",
                CreateDate = DateTime.UtcNow,
                UpdateDate = DateTime.UtcNow,
                Items      = new List <TODOListItem>
                {
                    new TODOListItem
                    {
                        Description   = "Assigned Task",
                        Complete      = false,
                        AssignedEmail = assignedEmail
                    }
                }
            };

            await Context.SaveAsync(list);

            Console.WriteLine("List saved, check your email to see if you get the assignment");
            #endregion
        }
Ejemplo n.º 3
0
        public static async Task TestStreamAsync()
        {
            var config = new DynamoDBContextConfig
            {
                Conversion     = DynamoDBEntryConversion.V2,
                ConsistentRead = true
            };
            var Context = new DynamoDBContext(new AmazonDynamoDBClient());

            #region test_stream_read

            // Save list to table
            var list = new TODOList
            {
                User       = "******",
                ListId     = Guid.NewGuid().ToString(),
                Complete   = false,
                Name       = "StreamTest",
                CreateDate = DateTime.UtcNow,
                UpdateDate = DateTime.UtcNow,
                Items      = new List <TODOListItem>
                {
                    new TODOListItem {
                        Description = "Task1", Complete = true
                    },
                    new TODOListItem {
                        Description = "Task2", Complete = false
                    }
                }
            };

            await Context.SaveAsync(list);

            using (var streamClient = new AmazonDynamoDBStreamsClient())
            {
                // Function for reading records continuously from a shard.
                Func <string, CancellationToken, Task> shardReader = async(iterator, token) =>
                {
                    while (!token.IsCancellationRequested)
                    {
                        var response = (await streamClient.GetRecordsAsync(new GetRecordsRequest {
                            ShardIterator = iterator
                        }));

                        // Update position in shard iterator
                        iterator = response.NextShardIterator;

                        // This is what you would write in a Lambda function processing DynamoDB Streams.
                        foreach (var record in response.Records)
                        {
                            var newVersion = record.Dynamodb.NewImage;
                            Console.WriteLine($"Item read: {newVersion["User"].S}/{newVersion["ListId"].S}");
                        }
                    }
                };


                // Find the arn for the DynamoDB Stream
                var streamArn = (await streamClient.ListStreamsAsync(new ListStreamsRequest {
                    TableName = "TODOList"
                }))
                                .Streams.FirstOrDefault()?.StreamArn;

                Console.WriteLine($"The stream arn is {streamArn} shards");

                // A stream is made of shards.
                var shards = (await streamClient.DescribeStreamAsync(new DescribeStreamRequest {
                    StreamArn = streamArn
                }))
                             .StreamDescription.Shards;

                Console.WriteLine($"The stream currently has {shards.Count} shards");


                // Execute a separate reader for each shard.
                var cancelSource = new CancellationTokenSource();
                var readerTasks  = new Task[shards.Count];
                for (int i = 0; i < readerTasks.Length; i++)
                {
                    var shardIteratorRequest = new GetShardIteratorRequest
                    {
                        StreamArn         = streamArn,
                        ShardId           = shards[i].ShardId,
                        ShardIteratorType = ShardIteratorType.TRIM_HORIZON
                    };

                    var shardIterator = (await streamClient.GetShardIteratorAsync(shardIteratorRequest))
                                        .ShardIterator;
                    Console.WriteLine($"Shard Iterator {i}: {shardIterator}");

                    readerTasks[i] = shardReader(shardIterator, cancelSource.Token);
                }

                Task.WaitAll(readerTasks, TimeSpan.FromSeconds(5));
                cancelSource.Cancel();
            }
            #endregion
        }