Example #1
0
        public async Task <IActionResult> Create([FromBody] Amazon.DynamoDBv2.DocumentModel.Document product)
        {
            var table = await _dbTableBuilder.Build();

            try
            {
                Console.WriteLine(product["DocumentProductId"]);
                Console.WriteLine(product["PublishOn"]);

                await table.PutItemAsync(product);

                var productId = product["DocumentProductId"].AsString();
                return(new JsonResult(
                           new
                {
                    message = $"Created new product: {productId}",
                    product = product.ToJson()
                })
                {
                    StatusCode = 201
                });
            }
            catch (AmazonDynamoDBException addbe)
            {
                if (addbe.ErrorCode == "ConditionalCheckFailedException")
                {
                    return new JsonResult(
                        new
                    {
                        message = $"Product {product["DocumentProductId"]} already exists"
                    })
                           {
                               StatusCode = 409
                           }
                }
                ;
                return(AmazonExceptionHandlers.HandleAmazonDynamoDBException(addbe));
            }
            catch (AmazonServiceException ase)
            {
                AmazonExceptionHandlers.HandleAmazonServiceExceptionException(ase);
            }
            catch (AmazonClientException ace)
            {
                AmazonExceptionHandlers.HandleAmazonClientExceptionException(ace);
            }
            return(StatusCode(500));
        }