forked from aws-samples/aws-sdk-unity-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighLevelTableExample.cs
126 lines (112 loc) · 4.05 KB
/
HighLevelTableExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright 2014-2015 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the AWS Mobile SDK For Unity
// Sample Application License Agreement (the "License").
// You may not use this file except in compliance with the
// License. A copy of the License is located
// in the "license" file accompanying this file. This file is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, express or implied. See the License
// for the specific language governing permissions and
// limitations under the License.
//
using UnityEngine;
using System.Collections;
using Amazon.DynamoDBv2.DataModel;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using UnityEngine.UI;
namespace AWSSDK.Examples
{
public class HighLevelTableExample : DynamoDbBaseExample {
private IAmazonDynamoDB _client;
private DynamoDBContext _context;
public Text resultText;
public Button back;
public Button createOperation;
public Button updateOperation;
public Button deleteOperation;
int bookID = 1001;
private DynamoDBContext Context
{
get
{
if(_context == null)
_context = new DynamoDBContext(_client);
return _context;
}
}
void Awake()
{
back.onClick.AddListener(BackListener);
createOperation.onClick.AddListener(PerformCreateOperation);
updateOperation.onClick.AddListener(PerformUpdateOperation);
deleteOperation.onClick.AddListener(PerformDeleteOperation);
_client = Client;
}
private void PerformCreateOperation()
{
Book myBook = new Book
{
Id = bookID,
Title = "object persistence-AWS SDK for.NET SDK-Book 1001",
ISBN = "111-1111111001",
BookAuthors = new List<string> { "Author 1", "Author 2" },
};
// Save the book.
Context.SaveAsync(myBook,(result)=>{
if(result.Exception == null)
resultText.text += @"book saved";
});
}
private void PerformUpdateOperation()
{
// Retrieve the book.
Book bookRetrieved = null;
Context.LoadAsync<Book>(bookID,(result)=>
{
if(result.Exception == null )
{
bookRetrieved = result.Result as Book;
// Update few properties.
bookRetrieved.ISBN = "222-2222221001";
bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; // Replace existing authors list with this.
Context.SaveAsync<Book>(bookRetrieved,(res)=>
{
if(res.Exception == null)
resultText.text += ("\nBook updated");
});
}
});
}
private void PerformDeleteOperation()
{
// Delete the book.
Context.DeleteAsync<Book>(bookID,(res)=>{
if(res.Exception ==null)
{
Context.LoadAsync<Book>(bookID,(result)=>
{
Book deletedBook = result.Result;
if(deletedBook==null)
resultText.text += ("\nBook is deleted");
});
}
});
}
}
[DynamoDBTable("ProductCatalog")]
public class Book
{
[DynamoDBHashKey] // Hash key.
public int Id { get; set; }
[DynamoDBProperty]
public string Title { get; set; }
[DynamoDBProperty]
public string ISBN { get; set; }
[DynamoDBProperty("Authors")] // Multi-valued (set type) attribute.
public List<string> BookAuthors { get; set; }
}
}