public void AnswerDescriptionVoteTests_IDtoConvertable_Converts()
        {
            var dto = _answerDescriptionVote.ToDto();

            Assert.Equal(dto.Id, _answerDescriptionVote.Id);
            Assert.Equal(dto.AnswerDescriptionId, _answerDescriptionVote.AnswerDescriptionId);
            Assert.Equal(dto.UserId, _answerDescriptionVote.UserId);
            Assert.Equal(dto.DateAdded, _answerDescriptionVote.DateAdded);

            var item = new AnswerDescriptionVote();

            item.FromDto(dto);

            Assert.Equal(item.Id, _answerDescriptionVote.Id);
            Assert.Equal(item.AnswerDescriptionId, _answerDescriptionVote.AnswerDescriptionId);
            Assert.Equal(item.UserId, _answerDescriptionVote.UserId);
            Assert.Equal(item.DateAdded, _answerDescriptionVote.DateAdded);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Save answer description Vote
        /// </summary>
        /// <param name="answerVote"></param>
        /// <returns>Id of the answer whos description was voted.</returns>
        public DataOperationResult VoteAnswerDescription(AnswerDescriptionVoteDto answerDescriptionVote)
        {
            if (answerDescriptionVote == null)
            {
                throw new ServicesException("Null parameter VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            if (answerDescriptionVote.AnswerDescriptionId <= 0)
            {
                throw new ServicesException("Unexpected AnswerDescriptionId in VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            if (answerDescriptionVote.UserId == null)
            {
                throw new ServicesException("Unexpected UserId in VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            var result = new DataOperationResult();

            // Find if vote is already there.
            var existingVote = _answerDescriptionVoteRepository.Queryable()
                               .FirstOrDefault(x => x.UserId == answerDescriptionVote.UserId && x.AnswerDescriptionId == answerDescriptionVote.AnswerDescriptionId);

            // Do not re-add existing vote.
            if (existingVote != null)
            {
                result.IntId = existingVote.Id;
                return(result);
            }

            // Add new description vote
            var answerDescriptionVoteObject = new AnswerDescriptionVote();

            answerDescriptionVoteObject.FromDto(answerDescriptionVote);

            // Insert
            _answerDescriptionVoteRepository.Insert(answerDescriptionVoteObject);
            var task = _answerDescriptionVoteRepository.SaveChangesAsync();

            task.Wait();

            // Add to cache.
            var cachedData = GetVoteDescriptionsCachedData();

            cachedData.Insert(answerDescriptionVoteObject);

            // Add to user cache if there is a user
            if (answerDescriptionVoteObject.UserId != null)
            {
                var userCachedData = GetUserVoteDescriptionsCachedData();
                userCachedData.Insert(new AnswerDescriptionVoteUserMask(answerDescriptionVoteObject));
            }

            // Find the id of the answer whos description was voted for
            var answerDescriptionDto = _answerDescriptionService
                                       .FindByAnswerDescriptionId(answerDescriptionVote.AnswerDescriptionId);

            result.IntId = answerDescriptionDto.AnswerId;
            result.IsNew = true;
            return(result);
        }