コード例 #1
0
ファイル: Function.cs プロジェクト: collin-smith/DynamoDBApp
        /** Sample request json
         * {
         *  }
         */
        public object TestPrimaryKeyTableLambdaHandler(object input, ILambdaContext context)
        {
            //basic elements of our response
            bool   success          = true;
            string message          = "";
            string responseText     = "";
            string requestBody      = "";
            string usersObjectJson  = "{}";
            string citiesObjectJson = "{}";

            try
            {
                string environment   = Environment.GetEnvironmentVariable("ENVIRONMENT");
                string userTableName = Environment.GetEnvironmentVariable("USERTABLE");
                string cityTableName = Environment.GetEnvironmentVariable("CITYTABLE");
                responseText  = "TestPrimaryKeyTableLambdaHandler CDK Lambda " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") + " ";
                responseText += "User DynamoDB Table:" + userTableName;
                responseText += "City DynamoDB Table:" + cityTableName;

                var request = JObject.Parse("" + input);
                requestBody = request["body"].ToString();
                var requestBodyJson = JObject.Parse(requestBody);
                DynamoDbUserService  dbUserService = new DynamoDbUserService(environment);
                List <ScanCondition> conditions    = new List <ScanCondition>();

                dbUserService.GetUsersByScan(environment, conditions).Wait();
                responseText += " *(1) GetUsersByScan(All) collection size =" + dbUserService.users.Count;
                int index = 0;
                foreach (User user in dbUserService.users)
                {
                    responseText += "  User(" + index + "):" + user + "  ";
                    index++;
                }

                //Query Users(with ScanCondition) which username starts with J
                conditions = new List <ScanCondition>();
                conditions.Add(new ScanCondition("username", ScanOperator.BeginsWith, "J"));
                dbUserService.GetUsersByScan(environment, conditions).Wait();
                responseText += " *(2)  GetUsersByScan(UserNames that startWith J) collection size =" + dbUserService.users.Count;
                index         = 0;
                foreach (User user in dbUserService.users)
                {
                    responseText += "  User(" + index + "):" + user + "  ";
                    index++;
                }

                //Deleting a User
                string usernameToDelete = "JasonP";
                responseText += " *(3) Delete username="******"Delete Log="+dbUserService.log;
                responseText += "Done delete operation";

                //Verify the deletion
                dbUserService.GetUsersByScan(environment, conditions).Wait();
                responseText += " *(4)After Delete collection size =" + dbUserService.users.Count;
                index         = 0;
                foreach (User user in dbUserService.users)
                {
                    responseText += "  User(" + index + "):" + user + "  ";
                    index++;
                }

                string searchEmail = "*****@*****.**";
                //Search via the Partition Key (username)
                responseText += " *(5)Querying user database With Partition Key :" + userTableName;
                dbUserService.GetUser(searchEmail).Wait();
                responseText += "***  GetUser(By Partition Key)=" + searchEmail + " :" + dbUserService.users.Count;
                index         = 0;
                foreach (User user in dbUserService.users)
                {
                    responseText += "  User(" + index + "):" + user + "  ";
                    index++;
                }

                //Search via the Global Secondary Index(email)
                responseText += " *(6)Querying user database With GSI :" + userTableName;
                dbUserService.GetUserByEmailGSI(searchEmail).Wait();
                responseText += "GetUserByEmailGSI(By GSI) # Users in database with email=" + searchEmail + " :" + dbUserService.users.Count;
                index         = 0;
                foreach (User user in dbUserService.users)
                {
                    responseText += "  User(" + index + "):" + user + "  ";
                    index++;
                }


                //Let's just display the current state of the users table in the json attribute of the users attribute
                responseText += " *(7) Retrieving the current User Table(Primary Key) contents";
                conditions    = new List <ScanCondition>();
                dbUserService.GetUsersByScan(environment, conditions).Wait();
                usersObjectJson = JsonSerializer.Serialize(dbUserService.users);
                responseText   += " # Users=" + dbUserService.users.Count;
            }
            catch (Exception exc)
            {
                success  = false;
                message += "TestPrimaryKeyTableLambdaHandler Exception:" + exc.Message + "," + exc.StackTrace;
            }


            //create the responseBody for the response
            string responseBody = "{";

            responseBody += " \"request\":" + requestBody + ",\n";
            responseBody += " \"response\":\"" + responseText + "\",\n";
            responseBody += " \"users\":" + usersObjectJson + ",\n";
            responseBody += " \"cities\":" + citiesObjectJson + ",\n";
            responseBody += " \"success\":\"" + success + "\",\n";
            responseBody += " \"message\":\"" + message + "\"\n";
            responseBody += "}";

            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = responseBody,
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                }
            };

            return(response);
        }
コード例 #2
0
ファイル: Function.cs プロジェクト: collin-smith/DynamoDBApp
        public object InitializeTablesLambdaHandler(object input, ILambdaContext context)
        {
            //basic elements of our response
            bool   success          = true;
            string message          = "";
            string responseText     = "";
            string requestBody      = "";
            string usersObjectJson  = "{}";
            string citiesObjectJson = "{}";

            try
            {
                string environment   = Environment.GetEnvironmentVariable("ENVIRONMENT");
                string userTableName = Environment.GetEnvironmentVariable("USERTABLE");
                string cityTableName = Environment.GetEnvironmentVariable("CITYTABLE");
                responseText  = "InitializeTablesLambdaHandler CDK Lambda " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") + " ";
                responseText += "User DynamoDB Table:" + userTableName;
                responseText += "City DynamoDB Table:" + cityTableName;

                var request = JObject.Parse("" + input);
                requestBody = request["body"].ToString();
                var requestBodyJson = JObject.Parse(requestBody);


                //Create the user objects to save
                ArrayList users = new ArrayList();
                User      user1 = new User();
                user1.username  = "******";
                user1.firstname = "Steve";
                user1.lastname  = "Jamieson";
                user1.email     = "*****@*****.**";
                users.Add(user1);
                User user2 = new User();
                user2.username  = "******";
                user2.firstname = "Jenny";
                user2.lastname  = "Ross";
                user2.email     = "*****@*****.**";
                users.Add(user2);
                User user3 = new User();
                user3.username  = "******";
                user3.firstname = "Jason";
                user3.lastname  = "Pratt";
                user3.email     = "*****@*****.**";
                users.Add(user3);
                User user4 = new User();
                user4.username  = "******";
                user4.firstname = "Thomas";
                user4.lastname  = "Richards";
                user4.email     = "*****@*****.**";
                users.Add(user4);


                responseText += " *(1) Populating the User Table(Primary Key)";
                //Inserting a set of users
                DynamoDbUserService dbUserService = new DynamoDbUserService(environment);
                dbUserService.InsertUsers(users);


                responseText += " *(2) Retrieving the User Table(Primary Key) contents";
                List <ScanCondition> conditions = new List <ScanCondition>();
                dbUserService.GetUsersByScan(environment, conditions).Wait();
                usersObjectJson = JsonSerializer.Serialize(dbUserService.users);
                responseText   += " # Users=" + dbUserService.users.Count;

                //Create the city objects to save
                ArrayList cities = new ArrayList();
                City      city3  = new City();
                city3.state      = "California";
                city3.city       = "San Diego";
                city3.iscapital  = false;
                city3.population = 1423851;
                cities.Add(city3);
                City city2 = new City();
                city2.state      = "California";
                city2.city       = "Sacramento";
                city2.iscapital  = true;
                city2.population = 513624;
                cities.Add(city2);

                City city1 = new City();
                city1.state      = "California";
                city1.city       = "Los Angeles";
                city1.iscapital  = false;
                city1.population = 3792621;
                cities.Add(city1);
                City city4 = new City();
                city4.state      = "New York";
                city4.city       = "New York";
                city4.iscapital  = false;
                city4.population = 8175133;
                cities.Add(city4);
                City city6 = new City();
                city6.state      = "New York";
                city6.city       = "Buffalo";
                city6.iscapital  = false;
                city6.population = 261310;
                cities.Add(city6);

                City city5 = new City();
                city5.state      = "New York";
                city5.city       = "Albany";
                city5.iscapital  = true;
                city5.population = 97478;
                cities.Add(city5);

                responseText += " *(3) Populating the City Table(Composite Key)";
                //Inserting all Cities
                DynamoDbCityService dbCityService = new DynamoDbCityService(environment);
                dbCityService.InsertCities(cities);

                //Let's populate the cities attribute in the response with what is in the table
                responseText += " *(4) Retrieving the City Table(Composite Key) contents";
                conditions    = new List <ScanCondition>();
                dbCityService.GetCities(conditions).Wait();
                citiesObjectJson = JsonSerializer.Serialize(dbCityService.cities);
                responseText    += " # Cities=" + dbCityService.cities.Count;
            }
            catch (Exception exc)
            {
                success  = false;
                message += "InitializeTablesLambdaHandler Exception:" + exc.Message + "," + exc.StackTrace;
            }


            //create the responseBody for the response
            string responseBody = "{";

            responseBody += " \"request\":" + requestBody + ",\n";
            responseBody += " \"response\":\"" + responseText + "\",\n";
            responseBody += " \"users\":" + usersObjectJson + ",\n";
            responseBody += " \"cities\":" + citiesObjectJson + ",\n";
            responseBody += " \"success\":\"" + success + "\",\n";
            responseBody += " \"message\":\"" + message + "\"\n";
            responseBody += "}";

            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = responseBody,
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                }
            };

            return(response);
        }
コード例 #3
0
ファイル: Function.cs プロジェクト: xerris/CDKApp
        /*
         *
         *  {
         * "email":"*****@*****.**"
         * }
         */
        public object ReadDynamoDBLambdaHandler(object input, ILambdaContext context)
        {
            //basic elements of our response
            bool   success        = true;
            string message        = "";
            string responseText   = "";
            string requestBody    = "";
            string userObjectJson = "{}";

            try
            {
                string dynamicText = "ReadDynamoDB CDK Lambda " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") + " ";
                string environment = Environment.GetEnvironmentVariable("ENVIRONMENT");
                string tableName   = Environment.GetEnvironmentVariable("TABLE");

                var request = JObject.Parse("" + input);
                requestBody = request["body"].ToString();
                var requestBodyJson = JObject.Parse(requestBody);

                string email = requestBodyJson["email"].ToString();

                responseText += "Searching for user(" + email + ")";
                DynamoDbUserService dynamoDbUserService = new DynamoDbUserService();
                dynamoDbUserService.GetDynamoDbUser(environment, email).Wait();
                User user = dynamoDbUserService.user;
                responseText += "DynamoDBUserService Log:" + dynamoDbUserService.log + ".";
                if (user != null)
                {
                    userObjectJson = JsonSerializer.Serialize(user);
                    responseText  += " Found the User:"******"," + user.firstName + "," + user.lastName;
                }
                else
                {
                    success        = false;
                    userObjectJson = "{}";
                    responseText  += " Did not find the user(" + email + ")";
                    message        = "Did not find the user(" + email + ")";
                }
            }
            catch (Exception exc)
            {
                success  = false;
                message += "ReadDynamoDBLambdaHandler Exception:" + exc.Message + "," + exc.StackTrace;
            }


            //create the responseBody for the response
            string responseBody = "{";

            responseBody += " \"request\":" + requestBody + ",\n";
            responseBody += " \"response\":\"" + responseText + "\",\n";
            responseBody += " \"user\":" + userObjectJson + ",\n";
            responseBody += " \"success\":\"" + success + "\",\n";
            responseBody += " \"message\":\"" + message + "\"\n";
            responseBody += "}";

            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = responseBody,
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "text/plain" }
                }
            };

            return(response);
        }