/// <summary> /// This method creates result of passing test. /// </summary> /// <param name="userTest">Result of passing test which should be created.</param> public void CreateUserTestByAdmin(UserTestDTO userTest) { if (userTest == null) { throw new ValidationException("UserTest can not be null"); } var user = Database.Users.Find(x => x.UserName == userTest.Username).FirstOrDefault(); if (user == null) { throw new NotFoundException("User was not found", "Username"); } var test = Database.Tests.Find(x => x.Title == userTest.TestTitle).FirstOrDefault(); if (test == null) { throw new NotFoundException("Test was not found", "Username"); } var date = new DateTime(); DateTime userTestTime = new DateTime (date.Year, date.Month, date.Day, date.Hour, userTest.Time.Minute, userTest.Time.Second); DateTime testTime = new DateTime (date.Year, date.Month, date.Day, date.Hour, test.MaxTime.Minute, test.MaxTime.Second); if (userTestTime > testTime) { throw new ValidationException("Test result`s time can not be higher than test`s maximum time"); } userTest.TestId = test.TestId; userTest.UserId = user.Id; Database.UserTests.Create(mapper.Map <UserTestDTO, UserTest>(userTest)); test.PassedTimes++; Database.Tests.Update(test); Database.Save(); }
public void AssignTestToUser(string category) { if (string.IsNullOrEmpty(category)) { throw new ArgumentException("message", nameof(category)); } var userId = this.userService.GetCurrentLoggedUser(); var categoryDTO = new CategoryDTO() { Name = category }; var test = this.testService.GetRandomTestFromCategory(categoryDTO); var userTestsToAdd = new UserTestDTO() { UserId = userId, TestId = test.Id }; this.Publish(userTestsToAdd); }
/// <summary> /// This method updates data of certain result of passing test. /// </summary> /// <param name="userTestDTO">Result of passing test which should be updated.</param> public void UpdateUserTest(UserTestDTO userTestDTO) { if (userTestDTO == null) { throw new ValidationException("UserTest can not be null", "Id"); } var userTest = Database.UserTests.Get(userTestDTO.UserTestId.ToString()); if (userTest == null) { throw new NotFoundException("UserTest was not found", "Id"); } var test = Database.Tests.Get(userTest.TestId.ToString()); var date = new DateTime(); DateTime userTestTime = new DateTime (date.Year, date.Month, date.Day, date.Hour, userTestDTO.Time.Minute, userTestDTO.Time.Second); DateTime testTime = new DateTime (date.Year, date.Month, date.Day, date.Hour, test.MaxTime.Minute, test.MaxTime.Second); if (userTestTime > testTime) { throw new ValidationException("Test result`s time can not be higher than test`s maximum time"); } userTest.Mark = userTestDTO.Mark; userTest.Time = userTestDTO.Time; Database.UserTests.Update(userTest); Database.SaveAsync(); }
public void Publish(UserTestDTO dto) { var model = this.mapper.MapTo <UserTest>(dto); this.userTestRepository.Add(model); this.saver.SaveChanges(); }
/// <summary> /// This method creates result of passing test. /// </summary> /// <param name="userTest">Result of passing test which should be created</param> public void CreateUserTest(UserTestDTO userTest) { if (userTest == null) { throw new ValidationException("UserTest can not be null", "Id"); } Database.UserTests.Create(mapper.Map <UserTestDTO, UserTest>(userTest)); var test = Database.Tests.Get(userTest.TestId.ToString()); test.PassedTimes++; Database.Tests.Update(test); Database.Save(); }