private async Task ValidateSessionCreation(TutoringSessionNewDto tutoringSessionNew) { var currentUserId = _currentUserService.GetUserId(); var isEligibleForCreating = await _userManager.Users .AnyAsync(u => u.Id == currentUserId && u.TutorStudents.Any(ts => ts.StudentId == tutoringSessionNew.StudentId && ts.ModuleId == tutoringSessionNew.ModuleId) && u.TutorModules.Any(tm => tm.ModuleId == tutoringSessionNew.ModuleId) ); if (!isEligibleForCreating) { throw new InvalidOperationException("You are not eligible to create a tutoring session for this student in this module."); } var currentTime = _timeService.GetCurrentTime(); var difference = tutoringSessionNew.SessionDate.Subtract(currentTime).TotalHours; if (difference < 1) { throw new InvalidOperationException("You may only create a tutoring session one hour from now (or later)"); } }
public async Task <IActionResult> CreateTutoringSession([FromBody] TutoringSessionNewDto tutoringSessionNew) { try { await _tutoringSessionsService.CreateTutoringSession(tutoringSessionNew); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task When_CreatingTutoringSessionWithPastDate_Expect_Exception() { var tutoringSessionNew = new TutoringSessionNewDto { IsSubscribed = true, ModuleId = 1, SessionDate = DateTimeOffset.Now.AddDays(-3), StudentId = _userManager.Users.First(u => u.Email == "*****@*****.**").Id }; await Assert.ThrowsAsync <InvalidOperationException>(async() => await _tutoringSessionsService.CreateTutoringSession(tutoringSessionNew) ); }
public async Task When_CreatingTutoringSession_Expect_SessionCreated() { var tutoringSessionNew = new TutoringSessionNewDto { IsSubscribed = true, ModuleId = 1, SessionDate = DateTimeOffset.Now.AddDays(3), StudentId = _userManager.Users.First(u => u.Email == "*****@*****.**").Id }; await _tutoringSessionsService.CreateTutoringSession(tutoringSessionNew); var sessionCreated = await _context.TutoringSessions.FirstAsync(ts => ts.SessionDate.Date == DateTimeOffset.Now.AddDays(3).Date); Assert.True(sessionCreated.IsSubscribed); }
public async Task CreateTutoringSession(TutoringSessionNewDto tutoringSessionNew) { await ValidateSessionCreation(tutoringSessionNew); var tutoringSession = new TutoringSession { CreationDate = _timeService.GetCurrentTime(), IsSubscribed = tutoringSessionNew.IsSubscribed, ModuleId = tutoringSessionNew.ModuleId, TutorId = _currentUserService.GetUserId(), StudentId = tutoringSessionNew.StudentId, SessionDate = tutoringSessionNew.SessionDate, Status = TutoringSessionStatusEnum.Upcoming }; await _tutoringSessionsRepository.Create(tutoringSession); }