Пример #1
0
        public async Task CreateAsync(ScoreItemDatabaseItemDataBase itemData)
        {
            var now = ScoreDatabaseUtils.UnixTimeMillisecondsNow();

            var maxSize = _quota.OwnerItemMaxSize;

            await PutDataAsync(_dynamoDbClient, ScoreTableName, itemData, maxSize, now);
Пример #2
0
        private ScoreItemInfoBase ConvertToItemInfo(ScoreItemDatabaseItemDataBase itemData)
        {
            if (itemData is ScoreItemDatabaseItemDataImage itemDataImage)
            {
                return(new ScoreImageItemInfo()
                {
                    ScoreId = itemDataImage.ScoreId,
                    ItemId = itemDataImage.ItemId,
                    Size = itemDataImage.Size,
                    TotalSize = itemDataImage.TotalSize,
                    ObjectName = itemDataImage.ObjName,
                    OriginalName = itemDataImage.OrgName,
                    Thumbnail = itemDataImage.Thumbnail.ObjName,
                    ThumbnailSize = itemDataImage.Thumbnail.Size,
                });
            }

            throw new ArgumentException();
        }
Пример #3
0
            static async Task PutDataAsync(
                IAmazonDynamoDB client,
                string tableName,
                ScoreItemDatabaseItemDataBase itemData,
                long maxSize,
                DateTimeOffset now)
            {
                var(items, partitionKey, _, item, totalSize) = ScoreItemDatabaseUtils.CreateDynamoDbValue(itemData, now);

                var actions = new List <TransactWriteItem>()
                {
                    new TransactWriteItem()
                    {
                        Update = new Update()
                        {
                            Key = new Dictionary <string, AttributeValue>()
                            {
                                [ScoreItemDatabasePropertyNames.OwnerId] = new AttributeValue(partitionKey),
                                [ScoreItemDatabasePropertyNames.ItemId]  = new AttributeValue(ScoreItemDatabaseConstant.ItemIdSummary),
                            },
                            ExpressionAttributeNames = new Dictionary <string, string>()
                            {
                                ["#size"] = ScoreItemDatabasePropertyNames.Size,
                            },
                            ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                            {
                                [":itemSize"] = new AttributeValue()
                                {
                                    N = totalSize.ToString()
                                },
                                [":maxSize"] = new AttributeValue()
                                {
                                    N = (maxSize - totalSize).ToString()
                                },
                            },
                            ConditionExpression = "#size < :maxSize",
                            UpdateExpression    = "ADD #size :itemSize",
                            TableName           = tableName,
                        },
                    },
                    new TransactWriteItem()
                    {
                        Put = new Put()
                        {
                            Item      = items,
                            TableName = tableName,
                            ExpressionAttributeNames = new Dictionary <string, string>()
                            {
                                ["#item"] = ScoreItemDatabasePropertyNames.ItemId,
                            },
                            ConditionExpression = "attribute_not_exists(#item)",
                        }
                    },
                };

                try
                {
                    await client.TransactWriteItemsAsync(new TransactWriteItemsRequest()
                    {
                        TransactItems          = actions,
                        ReturnConsumedCapacity = ReturnConsumedCapacity.TOTAL
                    });
                }
                catch (ResourceNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }
                catch (InternalServerErrorException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }
                catch (TransactionCanceledException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }
            }