public async Task <SaveSurveyUserResponse> SaveAsync(SurveyUser surveyUser)
        {
            try
            {
                await _surveyUserRepository.AddAsync(surveyUser);

                await _unitOfWork.CompleteAsync();

                return(new SaveSurveyUserResponse(surveyUser));
            }
            catch (Exception e)
            {
                return(new SaveSurveyUserResponse($"Произошла ошибка: {e.Message}"));
            }
        }
Beispiel #2
0
        public async Task <ActionResult <Dto.Survey.Questionary> > AnswerQuestion([FromBody] AnswerRequest answerRequest,
                                                                                  int id, int qid, int userId)
        {
            var item = await _surveyRepository.GetAsync(answerRequest.SurveyId);

            if (item == null)
            {
                return(NotFound());
            }

            if (item.Survey.NeedToBeFinishedFor.ToUnixTimeMilliseconds() - item.Survey.NeedToBeFinishedForStart.ToUnixTimeMilliseconds() != 0 &&
                item.Survey.NeedToBeFinishedFor.LocalDateTime < DateTime.Now)
            {
                return(ValidationProblem());
            }

            if (item.Survey.Limit <= item.Survey.NumberOfUser)
            {
                return(ValidationProblem());
            }

            var user = await _userRepository.GetAsync(userId);

            if (user.IsBanned)
            {
                return(Forbid());
            }

            var questionary = item.Survey.Questionary.FirstOrDefault(q => q.Id == qid);

            if (questionary.RightAnswer.ToLower() == answerRequest.Answer.ToLower())
            {
                var surveyUserItem = await _surveyUserRepository.GetAsync(answerRequest.SurveyId, userId);

                if (surveyUserItem == null)
                {
                    await _surveyUserRepository.AddAsync(new SurveyUser
                    {
                        IsFinished          = false,
                        IsAllAnswersCorrect = false,
                        StartDate           = DateTime.UtcNow,
                        SurveyId            = answerRequest.SurveyId,
                        UserId = userId
                    });
                }

                if (surveyUserItem != null && surveyUserItem.IsFinished)
                {
                    return(ValidationProblem());
                }

                var listAnswersId =
                    await _userQuestionRepository.GetAsync(item.Survey.SurveyUser.First(p => p.UserId == userId).Id);

                var filteredAnswer = listAnswersId
                                     .Where(p => p.QuestionaryID == qid && p.SurveyUserId == surveyUserItem.UserId).ToList();

                if (!filteredAnswer.Any())
                {
                    await _userQuestionRepository.AddAsync(new UserQuestionAnswer
                    {
                        QuestionaryID = qid,
                        Answer        = answerRequest.Answer,
                        SurveyUserId  = item.Survey.SurveyUser.FirstOrDefault(p => p.UserId == userId).Id
                    });
                }

                surveyUserItem = await _surveyUserRepository.GetAsync(answerRequest.SurveyId, userId);

                listAnswersId =
                    await _userQuestionRepository.GetAsync(item.Survey.SurveyUser.First(p => p.UserId == userId).Id);

                var allQuestionUser     = item.Survey.Questionary.ToList();
                var allQuestionAnswered =
                    allQuestionUser.Select(p => p.Id).Except(listAnswersId.Select(x => x.QuestionaryID));
                if (!allQuestionAnswered.Any())
                {
                    surveyUserItem.FinishDate          = DateTime.UtcNow;
                    surveyUserItem.IsAllAnswersCorrect = true;
                    surveyUserItem.IsFinished          = true;

                    item.Survey.NumberOfUser = item.Survey.NumberOfUser + 1;
                    await _surveyRepository.Update(item.Survey);

                    var userScore = await _userScoreRepository.GetAsync(userId, answerRequest.SurveyId);

                    if (userScore == null)
                    {
                        await _userScoreRepository.AddAsync(new UserTransaction
                        {
                            Score     = item.Survey.Score,
                            UserId    = userId,
                            SurveyId  = answerRequest.SurveyId,
                            CreatedAt = DateTime.UtcNow,
                            Type      = TransactionType.Survey
                        });
                    }
                    else
                    {
                        userScore.Score = userScore.Score + item.Survey.Score;
                        await _userScoreRepository.Update(userScore);
                    }

                    //update user balance

                    /*var allUserTransactions = await _userScoreRepository.GetAllAsync(userId);
                     * var allOrderOfUser = await _orderRepository.GetListAsync(p => p.UserId == userId);
                     *
                     * //find latest user transaction
                     *
                     * //var orderSum = allOrderOfUser.Where(p => !p.IsNotCalculated).Sum(p => p.Good.Cost);
                     * var transactionSum = allUserTransactions.Sum(p => p.Score);
                     *
                     * //if (transactionSum > orderSum)
                     * //    user.Balance = transactionSum - orderSum;
                     * //else
                     *
                     *
                     * user.Balance = transactionSum;*/

                    user.Balance += item.Survey.Score;

                    await _userRepository.Update(user);

                    await _surveyUserRepository.Update(surveyUserItem);
                }
            }

            var newQuestionary = new Dto.Survey.Questionary
            {
                Answer   = string.Empty,
                Id       = questionary.Id,
                Question = questionary.Question,
            };

            return(newQuestionary);
        }