Exemplo n.º 1
0
        public void FlagService_FlagExistingAnswerDescription_DoesNotAddAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerDescriptionFlag1 = new AnswerDescriptionFlagDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };
            var answerDescriptionFlag2 = new AnswerDescriptionFlagDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };

            var count = setup.AnswerDescriptionFlagRepository.Queryable().Where(x => x.UserId == "1").Count();

            Assert.Equal(0, count);
            var dataOperationResult = setup.FlagService.FlagAnswerDescription(answerDescriptionFlag1);

            count = setup.AnswerDescriptionFlagRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(1, count);
            Assert.Equal(dataOperationResult.IntId, setup.EXISTING_ANSWER_ID);

            dataOperationResult = setup.FlagService.FlagAnswerDescription(answerDescriptionFlag2);
            // Verify insert was called for every flag save. Flags work differently than votes.
            // Double flags are OK.
            count = setup.AnswerDescriptionFlagRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(2, count);
            Assert.Equal(dataOperationResult.IntId, setup.EXISTING_ANSWER_ID);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Save answer description flag
        /// </summary>
        /// <param name="answerFlag"></param>
        /// <returns></returns>
        /// <remarks>Flag is always considered new</remarks>
        public DataOperationResult FlagAnswerDescription(AnswerDescriptionFlagDto answerDescriptionFlag)
        {
            if (answerDescriptionFlag == null)
            {
                throw new ServicesException("Null parameter FlagService.FlagAnswerDescription(answerDescriptionFlag)");
            }

            if (answerDescriptionFlag.AnswerDescriptionId <= 0)
            {
                throw new ServicesException("Unexpected AnswerDescriptionId in FlagService.FlagAnswerDescription(answerDescriptionFlag)");
            }

            if (answerDescriptionFlag.UserId == null)
            {
                throw new ServicesException("Unexpected UserId in FlagService.FlagAnswerDescription(answerDescriptionFlag)");
            }


            var answerDescriptionFlagObject = new AnswerDescriptionFlag();

            answerDescriptionFlagObject.FromDto(answerDescriptionFlag);

            _answerDescriptionFlagRepository.Insert(answerDescriptionFlagObject);

            var task = _answerDescriptionFlagRepository.SaveChangesAsync();

            task.Wait();

            // Add to user cache if there is a user
            if (answerDescriptionFlagObject.UserId != null)
            {
                var userCachedData = GetUserDescriptionFlagsCachedData();
                userCachedData.Insert(answerDescriptionFlagObject);
            }

            // Find the id of the answer whos description was flagged
            // We need to give it caller so that caller can redirect to answer page
            var answerDescriptionDto = _answerDescriptionService
                                       .FindByAnswerDescriptionId(answerDescriptionFlag.AnswerDescriptionId);

            var result = new DataOperationResult();

            result.IntId = answerDescriptionDto.AnswerId;
            result.IsNew = true;
            return(result);
        }
Exemplo n.º 3
0
        public void FlagService_FlagExistingAnswerDescription_DoesNotCacheAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerDescriptionFlag1 = new AnswerDescriptionFlagDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };
            var answerDescriptionFlag2 = new AnswerDescriptionFlagDto()
            {
                AnswerDescriptionId = 2, UserId = "1"
            };

            setup.FlagService.FlagAnswerDescription(answerDescriptionFlag1);
            setup.FlagService.FlagAnswerDescription(answerDescriptionFlag2);

            // Verify cache add to cache was called only once
            //setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_DESCRIPTION_FlagS_DATA,
            //    It.IsAny<KeyIndexedDataSource<AnswerDescriptionFlag>>()), Times.Once());
        }