Esempio n. 1
0
        public DynamoDbCityService(string environment)
        {
            this.environment = environment;
            DynamoDBContextConfig config = new DynamoDBContextConfig()
            {
                TableNamePrefix = environment + "-"
            };

            dynamoDbContext = new DynamoDBContext(DynamoDbClientService.getDynamoDBClient(environment), config);
            tableName       = environment + "-City";
        }
Esempio n. 2
0
        public async Task DeleteUserByUserName(string username)
        {
            var Req = new Amazon.DynamoDBv2.Model.DeleteItemRequest
            {
                TableName = this.tableName,
                Key       = new Dictionary <string, Amazon.DynamoDBv2.Model.AttributeValue>()
                {
                    { "username", new Amazon.DynamoDBv2.Model.AttributeValue {
                          S = username.ToString()
                      } }
                }
            };
            var response = await DynamoDbClientService.getDynamoDBClient(environment).DeleteItemAsync(Req);

            var attributeList = response.Attributes;
        }
Esempio n. 3
0
        public async Task DeleteCitybyCompositeKey(string state, string city)
        {
            var deleteItemRequest = new Amazon.DynamoDBv2.Model.DeleteItemRequest
            {
                TableName = this.tableName,
                Key       = new Dictionary <string, Amazon.DynamoDBv2.Model.AttributeValue>()
                {
                    { "state", new Amazon.DynamoDBv2.Model.AttributeValue {
                          S = state.ToString()
                      } },
                    { "city", new Amazon.DynamoDBv2.Model.AttributeValue {
                          S = city.ToString()
                      } }
                }
            };
            var response = await DynamoDbClientService.getDynamoDBClient(environment).DeleteItemAsync(deleteItemRequest);

            var attributeList = response.Attributes;
        }
Esempio n. 4
0
        public async Task GetCitiesByStateUsingPopulationLSI(string state, bool ascendingOrder)
        {
            try
            {
                //Note: For this Query Request
                //Sometimes you might encounter an exception such as:
                //Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: state:
                //It is for this reason that you will need to include the ExpressionAttributeNames below
                //(for the KeyConditionExpression and the ProjectionExpression

                //You can note that the records that are pulled for each state will be sorted by the default sort key(city)

                //ProjectExpression identifies the attributes that you want

                //ScanIndexForward will determine if you will use Ascending or descending order on the Sort Key(the LSI in this case)
                var request = new QueryRequest
                {
                    TableName = this.tableName,
                    IndexName = this.LSIPopulationIndex,
                    KeyConditionExpression   = "#state = :state",
                    ExpressionAttributeNames = new Dictionary <string, string>
                    {
                        { "#state", "state" },
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                        { ":state", new AttributeValue {
                              S = state
                          } }
                    },
                    ProjectionExpression = "#state, city, iscapital, population",
                    ScanIndexForward     = ascendingOrder,
                    ConsistentRead       = true
                };

                var response = await DynamoDbClientService.getDynamoDBClient(environment).QueryAsync(request);

                this.cities = new ArrayList();
                var items = response.Items;

                City currentCity = null;
                foreach (var currentItem in items)
                {
                    currentCity = new City();
                    foreach (string attr in currentItem.Keys)
                    {
                        if (attr == "state")
                        {
                            currentCity.state = currentItem[attr].S;
                        }
                        if (attr == "city")
                        {
                            currentCity.city = currentItem[attr].S;
                        }
                        ;
                        if (attr == "iscapital")
                        {
                            currentCity.iscapital = currentItem[attr].BOOL;
                        }
                        ;
                        if (attr == "population")
                        {
                            currentCity.population = int.Parse(currentItem[attr].N);
                        }
                        ;
                    }
                    this.cities.Add(currentCity);
                }
            }
            catch (Exception exc)
            {
                this.log += "Exception: " + exc.Message + ":" + exc.StackTrace;
            }
        }
Esempio n. 5
0
        public async Task GetUserByEmailGSI(string email)
        {
            try
            {
                QueryRequest queryRequest = new QueryRequest
                {
                    TableName = this.tableName,
                    IndexName = this.GSIEmailIndex,
                    KeyConditionExpression = "#email = :email",
                    //placeholder that you use in an Amazon DynamoDB expression as an alternative to an actual attribute name
                    ExpressionAttributeNames = new Dictionary <String, String> {
                        { "#email", "email" }
                    },
                    //substitutes for the actual values that you want to compare
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                        { ":email", new AttributeValue {
                              S = email
                          } }
                    },
                    ScanIndexForward = true
                };

                var response = await DynamoDbClientService.getDynamoDBClient(environment).QueryAsync(queryRequest);

                var items = response.Items;

                this.users = new ArrayList();
                User currentUser = null;
                foreach (var currentItem in items)
                {
                    currentUser = new User();
                    foreach (string attr in currentItem.Keys)
                    {
                        if (attr == "username")
                        {
                            currentUser.username = currentItem[attr].S;
                        }
                        if (attr == "firstname")
                        {
                            currentUser.firstname = currentItem[attr].S;
                        }
                        ;
                        if (attr == "lastname")
                        {
                            currentUser.lastname = currentItem[attr].S;
                        }
                        ;
                        if (attr == "email")
                        {
                            currentUser.email = currentItem[attr].S;
                        }
                        ;
                    }
                    this.users.Add(currentUser);
                }
                //this.log += "F";
            }
            catch (Exception exc)
            {
                this.log += "EXC: " + exc.Message + ":" + exc.StackTrace;
            }
        }