public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);

            if (RequirePassword)
            {
                if (!await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    ModelState.AddModelError(string.Empty, "Incorrect password.");
                    return(Page());
                }
            }

            var patientQuestionnaires =
                _patientQuestionnaireRepo.GetPatientQuestionnairesByEmail(User.FindFirstValue(ClaimTypes.Email)).Result;

            foreach (var patientQuestionnaire in patientQuestionnaires)
            {
                _patientQuestionnaireRepo.Delete(patientQuestionnaire).Wait();
            }

            var userEmail             = User.FindFirstValue(ClaimTypes.Email);
            var userAnswers           = _userAnswerRepo.GetUserAnswersByUserEmail(userEmail).Result;
            var questionnaireAnswered = _userQuestionnaireAnswerRepo.GetByUserEmail(userEmail).Result;

            foreach (var userAnswer in userAnswers)
            {
                _userAnswerRepo.Delete(userAnswer).Wait();
            }

            foreach (var questionnaire in questionnaireAnswered)
            {
                _userQuestionnaireAnswerRepo.Delete(questionnaire).Wait();
            }

            var result = await _userManager.DeleteAsync(user);


            var userId = await _userManager.GetUserIdAsync(user);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'.");
            }

            await _signInManager.SignOutAsync();

            _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);

            return(Redirect("~/"));
        }
Esempio n. 2
0
        public async Task <ActionResult> PatientQuestionnaireResults(int id, string email)
        {
            var userAnswers = await _userAnswerRepo.GetUserAnswersByUserEmail(email);

            var answersToSelectedQuestionnaire = userAnswers.Where(answer =>
                                                                   _questionRepo.GetById(answer.QuestionId).Result.QuestionnaireId == id).ToList();

            var user = await _userManager.FindByEmailAsync(email);

            var model = new ResultViewModel
            {
                NumberOfSolvedQuestionnaires = 0,
                PatientEmail = email,
                FirstName    = user.FirstName,
                LastName     = user.LastName,
                PESEL        = user.PESEL
            };

            if (answersToSelectedQuestionnaire.Count == 0)
            {
                return(View(model));
            }

            var questionnaireAnswered = await _userQuestionnaireAnswerRepo.GetByUserEmailAndQuestionnaireId(email, id);

            var questionnaireAnswerDetails = new List <QuestionnaireAnswerDetails>();

            foreach (var questionnaireAnswer in questionnaireAnswered)
            {
                var questionnaireDetails = new QuestionnaireAnswerDetails
                {
                    AnswerDate  = questionnaireAnswer.AnswerDate,
                    AnswerCount = 0,
                    AnswerSum   = 0
                };
                foreach (var answer in answersToSelectedQuestionnaire.Where(answer => answer.UserQuestionnaireAnswerId == questionnaireAnswer.Id))
                {
                    questionnaireDetails.AnswerCount++;
                    questionnaireDetails.AnswerSum += answer.Value;
                }
                questionnaireAnswerDetails.Add(questionnaireDetails);
            }

            questionnaireAnswerDetails = questionnaireAnswerDetails.Where(q =>
                                                                          q.AnswerDate.HasValue).OrderBy(q =>
                                                                                                         q.AnswerDate.Value).ToList();

            var averageScorePerQuestionnaire = questionnaireAnswerDetails.Select(answerDetail => answerDetail.GetAverageScore()).ToList();

            model.AverageQuestionnaireScore    = averageScorePerQuestionnaire.ToArray();
            model.LastQuestionnaireDate        = questionnaireAnswerDetails.LastOrDefault().AnswerDate;
            model.NumberOfSolvedQuestionnaires = questionnaireAnswerDetails.Count;
            model.QuestionnaireName            = (await _questionnaireRepo.GetById(id)).Name;
            model.StudyStart = questionnaireAnswerDetails.FirstOrDefault().AnswerDate;

            return(View(model));
        }
Esempio n. 3
0
        public async Task <ActionResult> RemoveQuestionnaireFromPatient(int id, string email)
        {
            var patientQuestionnaire = await _patientQuestionnaireRepo.GetByIdAndUserEmail(id, email);

            await _patientQuestionnaireRepo.Delete(patientQuestionnaire);

            var userAnswers = await _userAnswerRepo.GetUserAnswersByUserEmail(email);

            var answersToSelectedQuestionnaire = userAnswers.Where(answer =>
                                                                   _questionRepo.GetById(answer.QuestionId).Result.QuestionnaireId == id).ToList();
            var questionnaireAnswered = _userQuestionnaireAnswerRepo.GetByUserEmailAndQuestionnaireId(email, id).Result;

            foreach (var userAnswer in answersToSelectedQuestionnaire)
            {
                await _userAnswerRepo.Delete(userAnswer);
            }

            foreach (var questionnaire in questionnaireAnswered)
            {
                await _userQuestionnaireAnswerRepo.Delete(questionnaire);
            }

            return(RedirectToAction("ManagePatientQuestionnaires", new { id = email }));
        }