// Private method to save User test results. private async Task <OperationDetails> SaveCurrentTestResults(int?id, string currentUserId, TestResultViewModel testResult) { bool isTopicTest; OperationDetails operationDetails; try { // I. Checks. if (id != null) { if (!int.TryParse(id.ToString(), out int intId) || intId > 1 || intId < 0) { operationDetails = new OperationDetails(false, "Incorrect parameter id!", "TestController.SaveCurrentTestResults"); return(operationDetails); } if (intId == 1) { isTopicTest = true; } else { isTopicTest = false; } } else { operationDetails = new OperationDetails(false, "Incorrect parameter id!", "TestController.SaveCurrentTestResults"); return(operationDetails); } // II. Create TestResultDTO object and set its properties. TestResultDTO testResultDTO = new TestResultDTO() { UserProfileId = currentUserId, Result = testResult.Result, MaxScore = testResult.MaxScore, IsPassedTest = testResult.IsPassedTest, IsTopicTest = isTopicTest }; // III. Add a test result to DB. operationDetails = await TestResultService.CreateAsync(testResultDTO, currentUserId); if (!operationDetails.Succedeed) { return(operationDetails); } // IV. Add test result details to DB. foreach (var item in testResult.TestResultDetails) { TestResultDetailDTO testResultDetailDTO = new TestResultDetailDTO() { TestResultId = testResultDTO.TestResultId, QuestionId = item.QuestionId, IsProperAnswer = item.IsProperAnswer }; operationDetails = await TestResultDetailService.CreateAsync(testResultDetailDTO, currentUserId); if (!operationDetails.Succedeed) { return(operationDetails); } } // V. return(new OperationDetails(true, "Test results have successfully added to DB!", "TestController.SaveCurrentTestResults")); } catch (Exception ex) { return(new OperationDetails(false, "Failed to add test results to DB! Exception: " + ex.Message, "TestController.SaveCurrentTestResults")); } }