示例#1
0
        public void ShouldAddUserstoDb(string hostname, int portNum, string databaseName)
        {
            var nsContext = new NeighborstashContext(new NeighbostashDbSettings
            {
                ConnectionString = $"mongodb://{hostname}:{portNum}",
                DatabaseName     = databaseName
            });

            for (var i = 0; i < 10; i++)
            {
                var user = new User
                {
                    Phones = new List <Phone>()
                    {
                        new Core.Models.Phone
                        {
                            Type       = "home",
                            Num        = $"{i}{i}{i}-{i}{i}{i}-{i}{i}{i}{i}",
                            IsPrefered = false
                        },
                        new Core.Models.Phone
                        {
                            Type       = "work",
                            Num        = $"{i*2}{i*2}{i*2}-{i}{i}{i}-{i}{i}{i}{i}",
                            IsPrefered = false
                        },
                        new Core.Models.Phone
                        {
                            Type       = "cell",
                            Num        = $"{i*3}{i*3}{i*3}-{i}{i}{i}-{i}{i}{i}{i}",
                            IsPrefered = true
                        }
                    },

                    DateOfBirth = "199{}-010-01",

                    Identities = new List <SocIdentity>()
                    {
                        new SocIdentity
                        {
                            Provider   = "google-oauth2",
                            UserId     = $"101010010000000_{i}",
                            Connection = "google-oauth2",
                            IsSocial   = true
                        }
                    },
                    Picture =
                        "https://secure.gravatar.com/avatar/5426f6b9d63ad92d60e6fe9fdf83aa21?s=480&r=pg&d=https%3A%2F%2Fssl.gstatic.com%2Fs2%2Fprofiles%2Fimages%2Fsilhouette80.png",
                    Createdate = DateTime.Now,
                    Email      = $"hithm{i}@gmail.com",
                    Firstname  = "Miller",
                    Lastname   = "Teddie",
                    Username   = $"hithm_{i}",
                    Gender     = i % 2 == 0 ? "Male" : "Female",
                    UserType   = i % 2 == 0 ? 0: 1, // neighbor=0, stasher=1
                    //Id = ObjectId.GenerateNewId().ToString()
                };

                nsContext.Users.InsertOne(user);


                var address = new UserAddress
                {
                    Username = user.Username,
                    Location = i % 2 == 0 ? "home" : "work",

                    Address = new AddressDetail
                    {
                        Addr1   = $"{i * 100} Cypress way",
                        Addr2   = $"## {i}",
                        City    = "Laurel",
                        State   = "MD",
                        Pcode   = i % 2 == 0 ? "20724" : (20701 + i).ToString(),
                        Country = "USA"
                    },
                    Geo = new Geo()
                    {
                        Type  = i % 2 == 0 ? "home" : "work",
                        Coord = new double[] { 56.00000 + i * 10, 45.000000 + i * 5 }
                    }
                };

                nsContext.UserAddresses.InsertOne(address);

                var userLogin = new UserLogin
                {
                    Username     = user.Username,
                    Lastlogin    = DateTime.Now,
                    Createdate   = DateTime.Now,
                    Passwordhash = ConvertToSha256($"userpassword_{i}")
                };


                nsContext.UserLogins.InsertOne(userLogin);

                var secQuestionAnswer = new SecurityQeustion
                {
                    Username        = user.Username,
                    QuestionAnswers = new List <QuestionAnswer>()
                    {
                        new QuestionAnswer
                        {
                            Question = "Your first job", Answer = "DOI"
                        },
                        new QuestionAnswer
                        {
                            Question = "Your first pet name", Answer =
                                "Tina"
                        },
                        new QuestionAnswer
                        {
                            Question = "Your first car model",
                            Answer   = "Dahtsun"
                        }
                    }
                };

                var result = nsContext.UserSecurityQuestions.InsertOneAsync(secQuestionAnswer);
                Console.WriteLine($"sercQuestion ID: {result.Id}");
            }
        }
示例#2
0
        public async Task MongoDriverCreating(string hostname, int portNum, string databaseName)
        {
            var nsContext = new NeighborstashContext(new NeighbostashDbSettings
            {
                ConnectionString = $"mongodb://{hostname}:{portNum}",
                DatabaseName     = databaseName
            });

            var secQuestionAnswer = new SecurityQeustion
            {
                Username        = "******", //user.Username,
                QuestionAnswers = new List <QuestionAnswer>()
                {
                    new QuestionAnswer
                    {
                        Question = "Your fav movie",
                        Answer   = "God Father"
                    },
                    new QuestionAnswer
                    {
                        Question = "Your firts international travel destination",
                        Answer   =
                            "Morituass"
                    },
                    new QuestionAnswer
                    {
                        Question = "Your first girl friend",
                        Answer   = "Hanna"
                    }
                }
            };

            // insert async
            await nsContext.UserSecurityQuestions.InsertOneAsync(secQuestionAnswer);

            // or
            var result = nsContext.UserSecurityQuestions.InsertOneAsync(secQuestionAnswer);

            Console.WriteLine($"sercQuestion ID: {result.Id}");


            var sc = nsContext.UserSecurityQuestions
                     .Find(Builders <SecurityQeustion> .Filter.Where(s => s.Username == "hithm_0"))
                     .FirstOrDefault();

            sc = nsContext.UserSecurityQuestions
                 .Find(s => s.Username == "hithm_0")
                 .FirstOrDefault();

            // now modify or update
            sc.QuestionAnswers.Add(new QuestionAnswer
            {
                Question = "What's your maternal grandmother's name",
                Answer   = "Dmk"
            });

            if (sc.Username == "hithm_0")
            {
                var replaced = await nsContext.UserSecurityQuestions.ReplaceOneAsync(s => s.Username == "hithm_0", sc);

                Console.WriteLine(replaced.IsAcknowledged);
            }
        }