public UpdateItemResponse PutAccessCount(string tableName, string username, string project) { var Client = new AmazonDynamoDBClient(RegionEndpoint.APNortheast1); Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate> (); updates["AccessCount"] = new AttributeValueUpdate() { Action = AttributeAction.ADD, Value = new AttributeValue { N = "1" } }; var request = new UpdateItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue> () { { "username", new AttributeValue { S = username } }, { "project", new AttributeValue { S = project } }, }, AttributeUpdates = updates, ReturnValues = "UPDATED_NEW", }; return(Client.UpdateItemAsync(request).Result); }
public async Task <bool> UpdateBookAvailability(string id, string available) { try { var key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { S = id } }, }; var updates = new Dictionary <string, AttributeValueUpdate>(); updates["Available"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = available } }; var request = new UpdateItemRequest { TableName = Constants.BOOK_TABLE_NAME, Key = key, AttributeUpdates = updates }; await dynamoDBClient.UpdateItemAsync(request); return(true); } catch (Exception e) { return(false); } }
internal override AttributeValueUpdate ConvertToAttributeUpdateValue() { AttributeValueUpdate attributeUpdate = new AttributeValueUpdate(); if ((Entries != null) && (Entries.Count != 0)) { attributeUpdate.Action = "PUT"; attributeUpdate.Value = new AttributeValue(); List <string> values = new List <string>(); foreach (var entry in Entries) { values.Add(entry.Value); } if (SaveAsNumeric) { attributeUpdate.Value.NS = values; } else { attributeUpdate.Value.SS = values; } } else { attributeUpdate.Action = "DELETE"; } return(attributeUpdate); }
public async static Task <UpdateItemResponse> MainUpdateAsync(SongDescription UpdateSong) { Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Artist", new AttributeValue { S = UpdateSong.Artist } }, { "SongTitle", new AttributeValue { S = UpdateSong.SongTitle } }, }; Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate>(); updates["Duration"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = UpdateSong.Duration } }; var request = new UpdateItemRequest { TableName = tableName, Key = key, AttributeUpdates = updates, ReturnValues = "UPDATED_NEW" }; UpdateItemResponse r = await client.UpdateItemAsync(request); return(r); }
public async Task <bool> UpdateTransactionStatus(string id, string status) { try { var key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { S = id } }, }; var updates = new Dictionary <string, AttributeValueUpdate>(); updates["Active"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = status } }; var request = new UpdateItemRequest { TableName = Constants.TRANSACTION_TABLE_NAME, Key = key, AttributeUpdates = updates }; await dynamoDBClient.UpdateItemAsync(request); return(true); } catch (Exception e) { return(false); } }
private async void UpdateItem() { AmazonDynamoDBClient client = new AmazonDynamoDBClient(awsCredentials, awsRegion); Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { N = "10" } } }; // Define attribute updates Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate>(); // Add a new string to the item's Genres SS attribute updates["Genres"] = new AttributeValueUpdate() { Action = AttributeAction.ADD, Value = new AttributeValue { SS = new List <string> { "Bildungsroman" } } }; UpdateItemRequest request = new UpdateItemRequest { TableName = "Books", Key = key, AttributeUpdates = updates }; var response = await client.UpdateItemAsync(request); }
public async Task <URLRedirect> VisitRedirectAsync(string Id) { var key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { S = Id } } }; var update = new AttributeValueUpdate { Action = AttributeAction.ADD, Value = new AttributeValue { N = "1" } }; var updates = new Dictionary <string, AttributeValueUpdate> { { "NumVisits", update } }; var result = await client.UpdateItemAsync(tableName, key, updates, ReturnValue.ALL_NEW); return(DictToRedirect(result.Attributes)); }
protected UpdateItemRequest ToDummyUpdateItemRequest(int userId) { var dict = new Dictionary <string, AttributeValueUpdate>(); dict[AttrUserName] = new AttributeValueUpdate { Action = "PUT", Value = new AttributeValue { S = "Ennfi" } }; dict[AttrUserScore] = new AttributeValueUpdate { Action = "PUT", Value = new AttributeValue { N = "100" } }; return(new UpdateItemRequest { TableName = TestTableName, Key = new Dictionary <string, AttributeValue> { { AttrUserId, new AttributeValue { N = userId.ToString() } } }, AttributeUpdates = dict }); }
public void Test2InsertNumberSet() { var tableName = "GameHistory"; CreateGameHistoryTable(tableName); var userIds = GenerateDummyUserIds(); var dict = new Dictionary <string, AttributeValueUpdate>(); dict["Userids"] = new AttributeValueUpdate { Action = "PUT", Value = new AttributeValue { NS = userIds.ToList() } }; var request = new UpdateItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "GameId", new AttributeValue { N = "12345" } } }, AttributeUpdates = dict }; var response = Client.UpdateItem(request); Assert.AreEqual(response.HttpStatusCode, HttpStatusCode.OK); }
public async Task <UpdateItemResponse> UpdateItems <T>(T tabelaInterna, string nomeTabela) { tableUpdate = Table.LoadTable(_dynamoDbClient, nomeTabela); DynamoDBContext db = new DynamoDBContext(_dynamoDbClient); var respostaGet = await _getItem.GetItems(tabelaInterna, nomeTabela); Document documentResponseGet = Document.FromJson(respostaGet); Document documentRequestUpdate = db.ToDocument(tabelaInterna); //Variaveis internas List <String> keyUpdate = new List <string>(); Dictionary <string, AttributeValueUpdate> valoresUpdate = new Dictionary <string, AttributeValueUpdate>(); //Veriricar quais elementos deverão ser modificados para o update foreach (var i in documentResponseGet) { foreach (var j in documentRequestUpdate) { if (i.Key == j.Key) { if (i.Value.ToString() != j.Value.ToString()) { var valor = j.Value; //valor keyUpdate.Add(j.Key.ToString()); } } } } chaveUpdate = tableUpdate.ToAttributeMap(documentRequestUpdate); foreach (var item in keyUpdate) { foreach (var chave in chaveUpdate) { if (item == chave.Key) { AttributeValueUpdate valorUpdate = new AttributeValueUpdate(chave.Value, "PUT"); valoresUpdate.Add(chave.Key, valorUpdate); } } } chaveUpdate = Utils.VerificarChaves(chaveUpdate, tableUpdate); var response = await _dynamoDbClient.UpdateItemAsync(nomeTabela, chaveUpdate, valoresUpdate, cancellationToken); db.Dispose(); return(response); }
public async Task <IActionResult> Update([FromBody] Person p) { //Define key Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Name", new AttributeValue { S = p.Name } } }; // Define attribute updates Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate> (); updates["IsHome"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { BOOL = p.IsHome } }; updates["Timestamp"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { S = DateTime.Now.ToString() } }; UpdateItemRequest req = new UpdateItemRequest() { TableName = TABLE_NAME, AttributeUpdates = updates, Key = key }; //await response from the dynamodb client UpdateItemResponse resp = await _dynamoClient.UpdateItemAsync(req); if (resp.HttpStatusCode == System.Net.HttpStatusCode.OK) { return(Ok()); } else { return(NotFound()); } }
internal AttributeValueUpdate ConvertToAttributeUpdateValue() { AttributeValue attributeValue = ConvertToAttributeValue(); AttributeValueUpdate attributeUpdate = new AttributeValueUpdate(); if (attributeValue == null) { attributeUpdate.Action = "DELETE"; } else { attributeUpdate.Action = "PUT"; attributeUpdate.Value = attributeValue; } return(attributeUpdate); }
public void Test3UpdateNumberSet() { var tableName = "GameHistory"; DeleteTable(tableName); CreateGameHistoryTable(tableName); var userIds = GenerateDummyUserIds(); InsertUserIds(tableName, userIds); var newUserIds = new List <string>(); newUserIds.Add("100"); newUserIds.Add("101"); newUserIds.Add("102"); var dict = new Dictionary <string, AttributeValueUpdate>(); dict["UserIds"] = new AttributeValueUpdate { Action = "ADD", Value = new AttributeValue { NS = newUserIds } }; var request = new UpdateItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "GameId", new AttributeValue { N = "12345" } } }, AttributeUpdates = dict }; var response = Client.UpdateItem(request); Assert.AreEqual(response.HttpStatusCode, HttpStatusCode.OK); DeleteTable(tableName); }
public static Task SetName(string apiUrl, string connectionId, string room, string name) { Task result = Task.Run(() => { Logging.LogDebug("Setting name " + room); AmazonDynamoDBClient client = new AmazonDynamoDBClient(); Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "connectionId", new AttributeValue { S = connectionId } }, { "room", new AttributeValue { S = room } } }; Dictionary <string, AttributeValueUpdate> row = new Dictionary <string, AttributeValueUpdate>(); row["displayName"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = name } }; UpdateItemRequest request = new UpdateItemRequest { TableName = "connections", Key = key, AttributeUpdates = row }; client.UpdateItemAsync(request).Wait(); SendAttendance(apiUrl, room); }); return(result); }
internal override AttributeValueUpdate ConvertToAttributeUpdateValue() { AttributeValueUpdate attributeUpdate = new AttributeValueUpdate(); if (!string.IsNullOrEmpty(this.Value)) { attributeUpdate.Action = "PUT"; attributeUpdate.Value = new AttributeValue(); if (SaveAsNumeric) { attributeUpdate.Value.N = Value; } else { attributeUpdate.Value.S = Value; } } else { attributeUpdate.Action = "DELETE"; } return(attributeUpdate); }
protected void InsertUserIds(string tableName, HashSet <string> userIds) { var dict = new Dictionary <string, AttributeValueUpdate>(); dict["UserIds"] = new AttributeValueUpdate { Action = "PUT", Value = new AttributeValue { NS = userIds.ToList() } }; var request = new UpdateItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "GameId", new AttributeValue { N = "12345" } } }, AttributeUpdates = dict }; var response = Client.UpdateItem(request); }
public void CRUDSamples() { EnsureTables(); PutSample(); { #region GetItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Create GetItem request GetItemRequest request = new GetItemRequest { TableName = "SampleTable", Key = key, }; // Issue request var result = client.GetItem(request); // View response Console.WriteLine("Item:"); Dictionary <string, AttributeValue> item = result.Item; foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } #endregion } { #region UpdateItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Define attribute updates Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate>(); // Update item's Setting attribute updates["Setting"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = "St. Petersburg, Missouri" } }; // Remove item's Bibliography attribute updates["Bibliography"] = new AttributeValueUpdate() { Action = AttributeAction.DELETE }; // Add a new string to the item's Genres SS attribute updates["Genres"] = new AttributeValueUpdate() { Action = AttributeAction.ADD, Value = new AttributeValue { SS = new List <string> { "Bildungsroman" } } }; // Create UpdateItem request UpdateItemRequest request = new UpdateItemRequest { TableName = "SampleTable", Key = key, AttributeUpdates = updates }; // Issue request client.UpdateItem(request); #endregion } { #region DeleteItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Create DeleteItem request DeleteItemRequest request = new DeleteItemRequest { TableName = "SampleTable", Key = key }; // Issue request client.DeleteItem(request); #endregion } }
public string CreateResource(ResourceUpdateRequest request) { try { AttributeValueUpdate objectTypeUpdate = request.Attributes.FirstOrDefault(t => t.Name == AttributeNames.ObjectType); if (objectTypeUpdate == null) { throw new ArgumentException("An object type must be specified"); } string objectType = objectTypeUpdate.Value?[0] as string; if (objectType == null) { throw new ArgumentException("An object type must be specified"); } ResourceObject resource = Global.Client.CreateResource(objectType); foreach (AttributeValueUpdate kvp in request.Attributes) { if (kvp.Value.Length > 1) { resource.Attributes[kvp.Name].SetValue(kvp.Value); } else if (kvp.Value.Length == 1) { resource.Attributes[kvp.Name].SetValue(kvp.Value[0]); } else { resource.Attributes[kvp.Name].RemoveValues(); } } Global.Client.SaveResource(resource); return(resource.ObjectID.ToString(false)); } catch (WebFaultException) { throw; } catch (WebFaultException <ExceptionData> ) { throw; } catch (AuthorizationRequiredException ex) { throw WebExceptionHelper.CreateWebException(ex); } catch (ResourceManagementException ex) { throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex); } catch (ArgumentException ex) { throw WebExceptionHelper.CreateWebException(HttpStatusCode.BadRequest, ex); } catch (Exception ex) { throw WebExceptionHelper.CreateWebException(HttpStatusCode.InternalServerError, ex); } }
public Stream CreateResource(ResourceUpdateRequest request) { try { AttributeValueUpdate objectTypeUpdate = request.Attributes.FirstOrDefault(t => t.Name == AttributeNames.ObjectType); if (objectTypeUpdate == null) { throw new ArgumentException("An object type must be specified"); } string objectType = objectTypeUpdate.Value?[0] as string; if (objectType == null) { throw new ArgumentException("An object type must be specified"); } ResourceObject resource = Global.Client.CreateResource(objectType); foreach (AttributeValueUpdate kvp in request.Attributes) { if (kvp.Value.Length > 1) { resource.Attributes[kvp.Name].SetValue(kvp.Value); } else if (kvp.Value.Length == 1) { resource.Attributes[kvp.Name].SetValue(kvp.Value[0]); } else { resource.Attributes[kvp.Name].RemoveValues(); } } Global.Client.SaveResource(resource); string bareID = resource.ObjectID.ToString().Replace("urn:uuid:", string.Empty); Uri url = new Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri, bareID); WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.Location, url.ToString()); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Created; if (WebResponseHelper.RequestNoBody()) { return(null); } else { resource.Refresh(); return(WebResponseHelper.GetResponse(resource, false)); } } catch (WebFaultException) { throw; } catch (WebFaultException <Error> ) { throw; } catch (Exception ex) { ResourceManagementWebServicev2.HandleException(ex); throw; } }