Ejemplo n.º 1
0
        public static List <UniqueKeyDto> GetUniqueKeyDtoList()
        {
            var list = new List <UniqueKeyDto>();

            foreach (var item in _listUniqueStrings)
            {
                var model = new UniqueKeyDto()
                {
                    UniqueKey = item,
                    InUse     = false
                };
                list.Add(model);
            }
            return(list);
        }
Ejemplo n.º 2
0
        public static bool CheckForKeyDBAndSeed()
        {
            _mongoClient   = new MongoClient("mongodb+srv://Steph:[email protected]/");
            _mongoDatabase = _mongoClient.GetDatabase("KittenKeyDb");

            var collection = _mongoDatabase.GetCollection <UniqueKeyDto>("KittenKeyDb");

            var count = collection.CountDocumentsAsync(new BsonDocument()).Result;

            //exit if database is already present and populated
            if (count == _amount + 1)
            {
                return(true);
            }
            else
            {
                //to create the mongodb database and collection you must insert a record.
                var testKey = new UniqueKeyDto()
                {
                    InUse     = true,
                    UniqueKey = "testKey"
                };

                var testRecord = collection.Find(u => u.UniqueKey == testKey.UniqueKey).ToList();

                //this will create the collection
                if (testRecord.Count == 0)
                {
                    collection.InsertOne(testKey);
                }

                var filter  = new BsonDocument("name", "KittenKeyDb");
                var options = new ListCollectionNamesOptions {
                    Filter = filter
                };

                var collectionExists = _mongoDatabase.ListCollectionNames(options).Any();

                //exit if the program has failed
                if (!collectionExists)
                {
                    return(false);
                }
                else
                {
                    _chars             = _uniqueKeyCharSet.ToCharArray();
                    _listUniqueStrings = new List <string>();
                    var createPasswordsForDatabase = CreatePasswordsForKeyDb();

                    //if the number of passwords is not what we have set it to exit
                    if (!(_listUniqueStrings.Count == _amount))
                    {
                        return(false);
                    }
                    else
                    {
                        var passwords = GetUniqueKeyDtoList();

                        try
                        {
                            collection.InsertMany(passwords);
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }

                    return(false);
                }
            }
        }