コード例 #1
0
        public void Should_GetRandomCard_When_1OrMoreWordsInDb()
        {
            string       word = "Test";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            CardDocument randomCard = mongoHandler.FindRandomCardAsync().Result;

            Assert.IsTrue(randomCard.Word == word);

            word = "Wall";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Random";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Fake";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            randomCard = mongoHandler.FindRandomCardAsync().Result;
            Assert.IsNotNull(randomCard);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #2
0
        public bool InsertCard(CardDocument card)
        {
            if (card == null)
            {
                return(false);
            }

            lock (_lock)
            {
                CardDocument possibleExistingId = FindCardAtIndex((uint)card.Id);
                if (possibleExistingId != null)
                {
                    return(false);
                }

                var WordFilter = Builders <CardDocument> .Filter.Eq("Word", card.Word);

                CardDocument possibleExistingWord = wordsCollection.Find(WordFilter).FirstOrDefault();
                if (possibleExistingWord != null)
                {
                    return(false);
                }

                wordsCollection.InsertOne(card);
                return(true);
            }
        }
コード例 #3
0
        public async Task <bool> InsertCardAsync(CardDocument card)
        {
            if (card == null)
            {
                return(false);
            }

            CardDocument possibleExistingId = await FindCardAtIndexAsync((uint)card.Id).ConfigureAwait(false);

            if (possibleExistingId != null)
            {
                return(false);
            }

            var WordFilter = Builders <CardDocument> .Filter.Eq("Word", card.Word);

            var findQueryResult = await wordsCollection.FindAsync(WordFilter).ConfigureAwait(false);

            CardDocument possibleExistingWord = findQueryResult.FirstOrDefault();

            if (possibleExistingWord != null)
            {
                return(false);
            }

            await wordsCollection.InsertOneAsync(card).ConfigureAwait(false);

            return(true);
        }
コード例 #4
0
        public async Task <bool> RemoveWordAsync(string word)
        {
            var wordFilter = Builders <CardDocument> .Filter.Eq("Word", word);

            CardDocument lastCardInDb = await FindLastDocumentAsync().ConfigureAwait(false);

            if (lastCardInDb == null)
            {
                return(false);
            }

            IAsyncCursor <CardDocument> cardsFound = await wordsCollection.FindAsync(wordFilter).ConfigureAwait(false);

            CardDocument cardToRemove = cardsFound.FirstOrDefault();

            if (cardToRemove == null)
            {
                return(false);
            }

            var findOptions = new FindOneAndUpdateOptions <CardDocument>()
            {
                ReturnDocument = ReturnDocument.After
            };
            var updateDefinition = Builders <CardDocument> .Update.Set("Word", lastCardInDb.Word);

            CardDocument updatedDocument = await wordsCollection.FindOneAndUpdateAsync(wordFilter, updateDefinition, findOptions).ConfigureAwait(false);

            if (updatedDocument.Word == lastCardInDb.Word)
            {
                return(await DeleteCardDocumentAsync(lastCardInDb).ConfigureAwait(false));
            }

            return(false);
        }
コード例 #5
0
        public void Should_Fail_When_CardDocumentIsInvalid()
        {
            CardDocument card = null;

            mongoHandler.InsertCard(card);
            Assert.IsTrue(mongoHandler.GetDocumentsCount() == 0);
        }
コード例 #6
0
 internal Response <long> CardDocumentSave(CardDocument entity, int languageId, long userId, string auditWorkstation)
 {
     try
     {
         long newId = 0;
         if (entity.Id == 0)
         {
             newId = _documentManagement.CardDocumentCreate(entity, userId, auditWorkstation);
         }
         else
         {
             newId = _documentManagement.CardDocumentUpdate(entity, userId, auditWorkstation);
         }
         return(new Response <long>(newId, ResponseType.SUCCESSFUL,
                                    string.Empty,
                                    string.Empty));
     }
     catch (Exception ex)
     {
         log.Error(ex);
         return(new Response <long>(0, ResponseType.ERROR,
                                    "Error processing request, please try again.",
                                    log.IsDebugEnabled || log.IsTraceEnabled ? ex.Message : ""));
     }
 }
コード例 #7
0
        public void Should_Succeed_When_WordValid()
        {
            string       word     = "Test";
            CardDocument document = new CardDocument(1, word);

            Assert.IsNotNull(document);
        }
コード例 #8
0
        public void Should_CreateCardBasedOnWord_When_DatabaseIsValid()
        {
            MongoHandler mongoHandler = new MongoHandler(TestingConsts.DB_INFO);
            CardDocument document     = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            Assert.IsNotNull(document);
            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #9
0
        public void Should_GetNull_When_DbIsEmpty()
        {
            CardDocument card = mongoHandler.FindLastDocument();

            Assert.IsNull(card);
            CardDocument card2 = mongoHandler.FindLastDocumentAsync().Result;

            Assert.IsNull(card2);
        }
コード例 #10
0
        public CardDocument FindLastDocument()
        {
            var sort = Builders <CardDocument> .Sort.Descending("_id");

            lock (_lock)
            {
                CardDocument last = wordsCollection.Find(new BsonDocument()).Sort(sort).Limit(1).FirstOrDefault();
                return(last);
            }
        }
コード例 #11
0
        public void Should_GetNull_When_IndexOutOfBounds()
        {
            string       word = "Test";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            Assert.IsNull(mongoHandler.FindCardAtIndex(5));
            Assert.IsNull(mongoHandler.FindCardAtIndexAsync(5).Result);
        }
コード例 #12
0
        public void Should_GetCard_When_IndexValid()
        {
            string       word = "Test";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            Assert.IsNotNull(mongoHandler.FindCardAtIndex(1));
            Assert.IsNotNull(mongoHandler.FindCardAtIndexAsync(1).Result);
        }
コード例 #13
0
        internal List <CardDocument> GetDocuments(string accountNumber)
        {
            List <CardDocument> documents = new List <CardDocument>();
            string baseUrl      = ConfigurationManager.AppSettings["RemoteDocumentLocation"];
            string userName     = ConfigurationManager.AppSettings["RemoteUser"];
            string password     = ConfigurationManager.AppSettings["RemotePassword"];
            string searchMethod = ConfigurationManager.AppSettings["RemoteSearchMethod"];

            string currentToken = Logon(baseUrl, userName, password);

            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("GET"), $"{baseUrl}/{searchMethod}?term={accountNumber}&orderBy=name"))
                {
                    request.Headers.TryAddWithoutValidation("Accept", "application/json");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}", currentToken))));

                    var response = httpClient.SendAsync(request).Result;

                    var content = response.Content.ReadAsStringAsync();

                    dynamic stuff = JObject.Parse(content.Result);


                    dynamic entries = stuff.list.entries;
                    foreach (var array in entries)
                    {
                        foreach (var dbl in array)
                        {
                            foreach (var itm in dbl)
                            {
                                var created    = itm.createdAt;
                                var isFolder   = itm.isFolder;
                                var isFile     = itm.isFile;
                                var modifiedAt = itm.modifiedAt;
                                var name       = itm.name;
                                var id         = itm.id;
                                var parentId   = itm.parentId;

                                CardDocument newDocument = new CardDocument()
                                {
                                    CardId         = 0,
                                    Comment        = name,
                                    DocumentTypeId = 0,
                                    Id             = 0,
                                    Location       = id
                                };
                                documents.Add(newDocument);
                            }
                        }
                    }
                }
                return(documents);
            }
        }
コード例 #14
0
        public void Should_Fail_When_CardDocumentIdAlreadyExists()
        {
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            mongoHandler.InsertCard(card);
            mongoHandler.InsertCard(card);

            Assert.IsTrue(mongoHandler.GetDocumentsCount() == 1);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #15
0
        public void Should_ThrowArgumentNullException_When_DatabaseIsInvalid()
        {
            MongoHandler mongoHandler = null;

            try
            {
                CardDocument document = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;
            } catch (AggregateException e) {
                throw e.InnerException;
            }
        }
コード例 #16
0
        public void Should_GenerateCorrectId_When_DbIsEmptyOrFilled()
        {
            Assert.IsTrue(mongoHandler.GenerateNewId().Result == 1);

            string       word = "Test";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            Assert.IsTrue(mongoHandler.GenerateNewId().Result == 2);
        }
コード例 #17
0
        public void Should_FailRemove_When_WordNotInDb()
        {
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            mongoHandler.InsertCard(card);

            string word = "Shniztel";

            Assert.IsFalse(mongoHandler.RemoveWord(word));
            Assert.IsFalse(mongoHandler.RemoveWordAsync(word).Result);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #18
0
        public void Should_Succeed_When_AddWordsToDbAsyncAndSync()
        {
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            mongoHandler.InsertCard(card);
            Assert.IsTrue(mongoHandler.GetDocumentsCount() == 1);

            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Wall").Result;
            mongoHandler.InsertCardAsync(card).Wait();
            Assert.IsTrue(mongoHandler.GetDocumentsCount() == 2);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #19
0
        private async Task <bool> DeleteCardDocumentAsync(CardDocument cardDocument)
        {
            if (cardDocument == null)
            {
                return(false);
            }

            var wordFilter = Builders <CardDocument> .Filter.Eq("_id", cardDocument.Id);

            CardDocument returnedDocument = await wordsCollection.FindOneAndDeleteAsync <CardDocument>(wordFilter).ConfigureAwait(false);

            return(returnedDocument.Equals(cardDocument));
        }
コード例 #20
0
        private void Lane_LaneRequestingEditCardLane(object sender, EditCardLaneArgs e)
        {
            e.target.ParentLaneId = e.NewLaneId;
            var cardDocument = new CardDocument
            {
                Id = e.target.CardId,
                CardDescription = e.target.CardDescription,
                CardName        = e.target.CardName,
                CardPoints      = e.target.CardPoints,
                ParentLaneId    = e.target.ParentLaneId
            };

            _cardDatabaseService.Update(cardDocument);
        }
コード例 #21
0
        private bool DeleteCardDocument(CardDocument cardDocument)
        {
            if (cardDocument == null)
            {
                return(false);
            }

            var wordFilter = Builders <CardDocument> .Filter.Eq("_id", cardDocument.Id);

            lock (_lock)
            {
                CardDocument returnedDocument = wordsCollection.FindOneAndDelete <CardDocument>(wordFilter);
                return(returnedDocument.Equals(cardDocument));
            }
        }
コード例 #22
0
        public void Should_Count2Documents_When_Inserted2Documents()
        {
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            mongoHandler.InsertCard(card);

            CardDocument card2 = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Wall").Result;

            mongoHandler.InsertCard(card2);

            Assert.IsTrue(mongoHandler.GetDocumentsCount() == 2);
            Assert.IsTrue(mongoHandler.GetDocumentsCountAsync().Result == 2);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #23
0
        public void Should_GetLastDocument_When_WordsInDb()
        {
            FillDatabase();

            string       word = "GamesIsOn";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            card = mongoHandler.FindLastDocument();
            Assert.IsNotNull(card);
            CardDocument card2 = mongoHandler.FindLastDocumentAsync().Result;

            Assert.IsNotNull(card2);
            Assert.AreEqual(card.Word, word);
            Assert.AreEqual(card2.Word, word);
        }
コード例 #24
0
        public void Should_RemoveWord_When_WordExistsInDocument()
        {
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;

            mongoHandler.InsertCard(card);

            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Screen").Result;
            mongoHandler.InsertCard(card);

            Assert.IsTrue(mongoHandler.RemoveWord("Test") && mongoHandler.GetDocumentsCount() == 1);

            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, "Test").Result;
            mongoHandler.InsertCard(card);

            Assert.IsTrue(mongoHandler.RemoveWord("Test") && mongoHandler.GetDocumentsCount() == 1);

            mongoHandler.DeleteDatabase(mongoHandler.DbInfo.DatabaseName);
        }
コード例 #25
0
        public void Should_ThrowArgumentException_When_WordInvalid()
        {
            string       word     = "Hi Bro!";
            uint         id       = 1;
            CardDocument document = new CardDocument(id, word);

            word     = "Hi~Bro!";
            document = new CardDocument(id, word);

            word     = "3rio!";
            document = new CardDocument(id, word);

            word     = "";
            document = new CardDocument(id, word);

            word     = "   ";
            document = new CardDocument(id, word);
        }
コード例 #26
0
        private void FillDatabase()
        {
            string       word = "Test";
            CardDocument card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;

            mongoHandler.InsertCard(card);

            word = "Wall";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Random";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Fake";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Word";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Filler";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Nini";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Tamir";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Code";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);

            word = "Name";
            card = CardDocument.CreateBasedOnWordAsync(mongoHandler, word).Result;
            mongoHandler.InsertCard(card);
        }
コード例 #27
0
ファイル: Card.cs プロジェクト: JayConnerGhost/IronCards
        private void EditButton_Click(object sender, EventArgs e)
        {
            var result =
                new EditCardDialog().ShowDialog(this.CardId, this.CardName, this.CardDescription, this.CardPoints, this.CardType);

            //TODO update card values
            UpdateValues(result.Item1, result.Item2, CardId, result.Item4, result.Item6);

            //ToDO update database
            var cardDocument = new CardDocument
            {
                Id = CardId,
                CardDescription = CardDescription,
                CardName        = CardName,
                CardPoints      = CardPoints,
                ParentLaneId    = ParentLaneId,
                CardType        = CardType.ToString()
            };

            DatabaseService.Update(cardDocument);
        }
コード例 #28
0
        public bool RemoveWord(string word)
        {
            var wordFilter = Builders <CardDocument> .Filter.Eq("Word", word);

            lock (_lock)
            {
                CardDocument lastCardInDb = FindLastDocument();
                if (lastCardInDb == null)
                {
                    return(false);
                }

                if (lastCardInDb.Word == word)
                {
                    return(DeleteCardDocument(lastCardInDb));
                }

                CardDocument cardToRemove = wordsCollection.Find(wordFilter).FirstOrDefault();
                if (cardToRemove == null)
                {
                    return(false);
                }

                var findOptions = new FindOneAndUpdateOptions <CardDocument>()
                {
                    ReturnDocument = ReturnDocument.After
                };
                var updateDefinition = Builders <CardDocument> .Update.Set("Word", lastCardInDb.Word);

                CardDocument updatedDocument = wordsCollection.FindOneAndUpdate(wordFilter, updateDefinition, findOptions);
                if (updatedDocument.Word == lastCardInDb.Word)
                {
                    return(DeleteCardDocument(lastCardInDb));
                }

                return(false);
            }
        }
コード例 #29
0
        public async Task <CardDocument[]> FindMultipleRandomCardsAsync(uint numberOfRandomCards)
        {
            if (numberOfRandomCards < 1)
            {
                return(null);
            }

            long documentsCount = await GetDocumentsCountAsync().ConfigureAwait(false);

            if (documentsCount < numberOfRandomCards)
            {
                throw new ArgumentException("There Aren't Enough Words in Database.");
            }

            uint[] randomCardIndexes = RandomNumberGenerator.GenerateRandomNumbers((uint)documentsCount, numberOfRandomCards);
            var    randomCards       = new BlockingCollection <CardDocument>((int)numberOfRandomCards);

            foreach (uint index in randomCardIndexes)
            {
                CardDocument card = await FindCardAtIndexAsync(index).ConfigureAwait(false);

                if (card != null)
                {
                    randomCards.Add(card);
                }
            }

            // TODO check if better performance possible using parallel
            //Parallel.ForEach(randomCardIndexes, async (index) =>
            //{
            //    CardDocument card = await FindCardAtIndexAsync(index);
            //    if(card != null)
            //        randomCards.Add(card);
            //});

            return(randomCards.ToArray());
        }