コード例 #1
0
        public async Task <ActionResult> Add(QuestionAnswerModel model)
        {
            var uploadedFile = new byte[model.File.InputStream.Length];

            model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
            model.FileInputStream = uploadedFile;

            if (model.Id > 0)
            {
                await _questionsRepository.Update(model);
            }
            else
            {
                await _questionsRepository.Insert(model);
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public void SaveSurveyAnswers(int userId, DetailedSurvey survey, List <QuestionAnswer> answers, int pageNum)
        {
            List <QuestionAnswer> answersToUpdate = null;
            List <QuestionAnswer> answersToCreate = null;

            // Get the user survey record to save the page state.
            var userSurvey = _surveyRepository.GetUserSurvey(userId, survey.Id);

            // Check if the survey is already filled by the user.
            if (userSurvey.CompletedOn.HasValue)
            {
                throw new InvalidOperationException("Survey is already completed!");
            }

            // Fill the user id in the answer objects.
            answers.ForEach(a => a.UserId = userId);

            // Get the saved answers.
            var savedAnswers = _questionAnswerRepository.GetSurveyAnswers(userId, survey.Id);

            // Get the answers that already have records in the database to update them.
            // Otherwise, create new answer records.
            if (savedAnswers.Any())
            {
                IEnumerable <int> intersect = savedAnswers
                                              .Select(a => a.SurveyQuestionId).Intersect(answers.Select(a => a.SurveyQuestionId));

                // Records to update.
                answersToUpdate = savedAnswers.Where(a => intersect.Contains(a.SurveyQuestionId)).ToList();

                // Records to create.
                answersToCreate = answers.Where(a => !intersect.Contains(a.SurveyQuestionId)).ToList();
            }
            else
            {
                answersToCreate = answers;
            }

            // Save the answers of the given survey page.
            // Update the existing records.
            QuestionAnswer tmp = null;

            if (answersToUpdate != null && answersToUpdate.Any())
            {
                foreach (var ans in answersToUpdate)
                {
                    tmp            = answers.FirstOrDefault(a => a.SurveyQuestionId == ans.SurveyQuestionId);
                    ans.Answer     = tmp?.Answer;
                    ans.EmployerId = tmp?.EmployerId;

                    _questionAnswerRepository.Update(ans);
                }
            }

            // Create new records.
            if (answersToCreate != null && answersToCreate.Any())
            {
                answersToCreate.ForEach(a => a.CreatedOn = DateTime.UtcNow);
                _questionAnswerRepository.AddList(answersToCreate);
            }

            // Save the completed page number.
            userSurvey.PageCompleted = pageNum;

            // Check if it is the last page, then close the survey as completed for this user.
            if (pageNum == survey.NumOfPages)
            {
                // Change status to completed by filling "CompletedOn" field.
                userSurvey.CompletedOn = DateTime.UtcNow;
            }

            // Commit the changes to the database.
            _unitOfWork.Commit();
        }
コード例 #3
0
        public async Task <ActionResult> Add(QuestionAnswerModel model)
        {
            await _questionsRepository.Update(model);

            return(RedirectToAction("Index"));
        }