private static TestSession UpdatePutableFields(TestSession TestSession, TestSessionDTO TestSessionDTO) { TestSession.LastVisitedTime = TestSessionDTO.LastVisitedTime; TestSession.SessionClosedTime = TestSessionDTO.SessionClosedTime; return(TestSession); }
private TestSession CreateNewSession(TestSessionDTO TestSessionDTO) { var EligibleQuestionsByTopic = GetEligibleQuestions(TestSessionDTO); if (EligibleQuestionsByTopic.Count < 1) { throw new Exception("The topic requested has no valid questions."); } TestSession session = new TestSession { LastVisitedTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), UserId = TestSessionDTO.UserId, TopicId = TestSessionDTO.TopicId }; _context.TestSession.Add(session); _context.SaveChanges(); var TestSessionId = session.Id; foreach (Question question in EligibleQuestionsByTopic) { SessionQuestion sQuestion = new SessionQuestion { QuestionId = question.Id, TestSessionId = TestSessionId, ResultTypeId = 1 }; _context.SessionQuestion.Add(sQuestion); _context.SaveChanges(); } return(session); }
public ActionResult Evaluation(int testId) { var testQuestions = _testService.GetTestQuestions(testId); var questions = _mapper.Map <IEnumerable <QuestionDTO>, IEnumerable <QuestionViewModel> >(testQuestions); var evaluationViewModel = new EvaluationViewModel(testId); foreach (var item in questions) { evaluationViewModel.Questions.Add(item); } var test = _testService.GetTestById(testId); var user = _userService.GetUserByEmail(User.Identity.Name); ViewBag.TimeLimit = Session[""]; ViewBag.TestName = test.Name; var testSession = new TestSessionDTO { IsPassed = false, Score = 0, TestId = testId, TimeStart = DateTime.Now, TimeFinish = DateTime.Now, UserId = user.UserID }; _testSessionService.CreateSession(testSession); var testSessionId = _testSessionService.GetLastSessionByUserIdAndTestId(user.UserID, testId).Id; //remake THis!!! evaluationViewModel.TestSessionId = testSessionId; return(View(evaluationViewModel)); }
public void UpdateSession(TestSessionDTO session) { var testSession = _mapper.Map <TestSessionDTO, TestSession>(session); db.TestSessions.Update(testSession); db.Save(); }
public void CreateSession(TestSessionDTO session) { var sessionToAdd = _mapper.Map <TestSessionDTO, TestSession>(session); db.TestSessions.Create(sessionToAdd); db.Save(); }
public async Task Should_Create_New_Item() { var sessionToCreate = new TestSessionDTO { Id = 1, Title = "My session", TestIds = { 2 }, MemberIds = { "3" } }; var repositoryMock = new Mock <IRepository <TestSession> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); unitOfWorkMock.Setup(u => u.TestSessions).Returns(() => repositoryMock.Object); var service = new TestSessionService(unitOfWorkMock.Object, mapper); await service.CreateAsync(sessionToCreate); repositoryMock.Verify(m => m.Create(It.Is <TestSession>(t => t.Title == sessionToCreate.Title && t.Tests.Single().TestId == sessionToCreate.TestIds.Single() && t.Members.Single().UserId == sessionToCreate.MemberIds.Single()))); unitOfWorkMock.Verify(m => m.SaveAsync()); }
public async Task <ActionResult <TestSessionDTO> > PostTestSession(TestSessionDTO TestSessionDTO) { var TestSession = CreateNewSession(TestSessionDTO); await _context.SaveChangesAsync(); //return CreatedAtAction("GetTestSession", new { id = TestSession.Id }, TestSession); return(CreatedAtAction(nameof(GetTestSession), new { id = TestSession.Id }, TestSessionToDTO(TestSession))); }
private static TestSession CreateFromDTO(TestSessionDTO TestSessionDTO) { var TestSession = new TestSession { LastVisitedTime = TestSessionDTO.LastVisitedTime, UserId = TestSessionDTO.UserId }; return(TestSession); }
public async Task <IActionResult> Edit([FromForm] TestSessionDTO testSession) { if (testSession.StartTime < DateTimeOffset.Now && !HttpContext.User.IsInRole(Roles.Admin)) { return(RedirectToAction(nameof(AccountController.AccessDenied), "Account")); } await testSessionService.UpdateAsync(testSession); return(RedirectToAction(nameof(List))); }
private List <Question> GetEligibleQuestions(TestSessionDTO TestSessionDTO) { List <Question> questions = new List <Question>(); long UserId = TestSessionDTO.UserId; long? TopicId = TestSessionDTO.TopicId; var QuestionsByTopic = _context.Question .Where(q => q.TopicId == TopicId) .Select(q => new { QuestionId = q.Id, QuestionExplanation = q.QuestionExplanation, QuestionText = q.QuestionText, TopicId = q.TopicId, Answers = q.Answers .Select(ans => new Answer { Id = ans.Id, AnswerText = ans.AnswerText, IsCorrect = ans.IsCorrect }) .ToList() }) .ToList(); foreach (var resultQuestion in QuestionsByTopic) { Question newQuestion = new Question { Id = resultQuestion.QuestionId, QuestionExplanation = resultQuestion.QuestionExplanation, QuestionText = resultQuestion.QuestionText, TopicId = resultQuestion.TopicId, Answers = resultQuestion.Answers }; if (testQuestionValidity(newQuestion)) { questions.Add(newQuestion); } } return(questions); }
public Task UpdateAsync(TestSessionDTO testSessionDTO) { if (testSessionDTO == null) { throw new ArgumentNullException(nameof(testSessionDTO)); } if (!testSessionDTO.TestIds.Any()) { throw new ArgumentException("Test session should contains at least one test"); } if (!testSessionDTO.MemberIds.Any()) { throw new ArgumentException("Test session shoul contains at least one examenee"); } var testSession = mapper.Map <TestSessionDTO, TestSession>(testSessionDTO); unitOfWork.TestSessions.Update(testSession); return(unitOfWork.SaveAsync()); }
public async Task <IActionResult> Create(DateTimeOffset?startTime = null) { var test = new TestSessionDTO() { StartTime = startTime ?? DateTimeOffset.Now }; ViewData["Templates"] = testTemplateService .GetAll() .Select(template => new SelectListItem { Value = template.Id.ToString(), Text = template.Title }); ViewData["Users"] = (await identityService .GetAllAsync(Roles.Examinee)) .Select(user => new SelectListItem { Value = user.Id, Text = user.Name }); return(View(test)); }
public async Task <IActionResult> Create([FromForm] TestSessionDTO testSession) { await testSessionService.CreateAsync(testSession); return(RedirectToAction(nameof(List))); }