<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[dynamodb.php.codeexample.Items_CRUD]
require 'vendor/autoload.php';

date_default_timezone_set('UTC');

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();

$tableName = 'ProductCatalog';

// ###################################################################
// Adding data to the table

echo "# Adding data to table $tableName...\n";

$response = $dynamodb->putItem([
    'TableName' => $tableName,
    'Item' => [
        'Id' => ['N' => '120'],
        'Title' => ['S' => 'Book 120 Title'],
        'ISBN' => ['S' => '120-1111111111'],
        'Authors' => ['SS' => ['Author12', 'Author22']],
        'Price' => ['N' => '20'],
        'Category' => ['S' => 'Book'],
        'Dimensions' => ['S' => '8.5x11.0x.75'],
        'InPublication' => ['BOOL' => false],
    ],
    'ReturnConsumedCapacity' => 'TOTAL'
]);

echo "Consumed capacity: " . $response ["ConsumedCapacity"] ["CapacityUnits"] . "\n";

$response = $dynamodb->putItem([
    'TableName' => $tableName,
    'Item' => [
        'Id' => ['N' => '121'],
        'Title' => ['S' => 'Book 121 Title'],
        'ISBN' => ['S' => '121-1111111111'],
        'Authors' => ['SS' => ['Author21', 'Author22']],
        'Price' => ['N' => '20'],
        'Category' => ['S' => 'Book'],
        'Dimensions' => ['S' => '8.5x11.0x.75'],
        'InPublication' => ['BOOL' => true],
    ],
    'ReturnConsumedCapacity' => 'TOTAL'
]);

echo "Consumed capacity: " . $response ["ConsumedCapacity"] ["CapacityUnits"] . "\n";

// ###################################################################
// Getting an item from the table

echo "\n\n";
echo "# Getting an item from table $tableName...\n";

$response = $dynamodb->getItem([
    'TableName' => $tableName,
    'ConsistentRead' => true,
    'Key' => [
        'Id' => [
            'N' => '120'
        ]
    ],
    'ProjectionExpression' => 'Id, ISBN, Title, Authors'
]);
print_r($response ['Item']);

// ###################################################################
// Updating item attributes

echo "\n\n";
echo "# Updating an item and returning the whole new item in table $tableName...\n";

$response = $dynamodb->updateItem([
    'TableName' => $tableName,
    'Key' => [
        'Id' => [
            'N' => '120' //was 121
        ]
    ],
    'ExpressionAttributeNames' => [
        '#NA' => 'NewAttribute',
        '#A' => 'Authors'
    ],
    'ExpressionAttributeValues' =>  [
        ':val1' => ['S' => 'Some Value'],
        ':val2' => ['SS' => ['Author YY','Author ZZ']]
    ] ,
    'UpdateExpression' => 'set #NA = :val1, #A = :val2',
    'ReturnValues' => 'ALL_NEW'
]);
print_r($response ['Attributes']);

// ###################################################################
// Conditionally updating the Price attribute, only if it has not changed.

echo "\n\n";
echo "# Updating an item attribute only if it has not changed in table $tableName...\n";

$response = $dynamodb->updateItem([
    'TableName' => $tableName,
    'Key' => [
        'Id' => [
            'N' => '121'
        ]
    ],
    'ExpressionAttributeNames' => [
        '#P' => 'Price'
    ],
    'ExpressionAttributeValues' => [
        ':val1' => ['N' => '25'],
        ':val2' => ['N' => '20'],
    ],
    'UpdateExpression' => 'set #P = :val1',
    'ConditionExpression' => '#P = :val2',
    'ReturnValues' => 'ALL_NEW'
]);

print_r($response ['Attributes']);

// ###################################################################
// Deleting an item

echo "\n\n";
echo "# Deleting an item and returning its previous values from in table $tableName...\n";

$response = $dynamodb->deleteItem([
    'TableName' => $tableName,
    'Key' => [
        'Id' => [
            'N' => '121'
        ]
    ],
    'ReturnValues' => 'ALL_OLD'
]);
print_r($response ['Attributes']);

// snippet-end:[dynamodb.php.codeexample.Items_CRUD]
