public async Task <IHttpActionResult> Create(TestResultModel result) { if (result == null) { return(BadRequest("Test must not be null.")); } try { result.UserId = User.Identity.GetUserId(); result.PassageDate = DateTime.Now; TestResultDTO resultDTO = _mapper.Map <TestResultModel, TestResultDTO>(result); TestResultDTO createdResult = await _resultService.Create(resultDTO); TestResultModel returnedResult = _mapper.Map <TestResultDTO, TestResultModel>(createdResult); string createdAtUrl = "http://www.google.com"; return(Created(createdAtUrl, returnedResult)); } catch (EntityNotFoundException) { return(NotFound()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public ActionResult PassingTest(PassingTestViewModel model) { bool isSuccess = false; var test = testService.GetById(model.Id); int userId = userService.GetOneByPredicate(u => u.UserName == User.Identity.Name).Id; var testResults = testService.CheckAnswers(model.Id, model.Results); var runtime = (DateTime.UtcNow - model.StartTest); if (testResultService.CheckPercentAnswers(testResults, test.MinToSuccess) && testResultService.CheckTime(test.TimeLimit, runtime)) { isSuccess = true; } var bllTestResult = new BllTestResult { TestId = model.Id, UserId = userId, Runtime = runtime, DateComplete = DateTime.Now, IsSuccess = isSuccess, Results = testResults.ToList() }; testResultService.Create(bllTestResult); var mvcTestResult = bllTestResult.ToMvcStatistics(); mvcTestResult.TimeLimit = test.TimeLimit; mvcTestResult.MinToSuccess = test.MinToSuccess; mvcTestResult.PercentCorrectAnswers = testResultService.GetPercentGoodAnswers(testResults); if (Request.IsAjaxRequest()) { return(PartialView("_Statistics", mvcTestResult)); } return(View("_Statistics", mvcTestResult)); }