public void Create(DalTest e)
 {
     if (context.Database.Connection.State != ConnectionState.Open)
         context.Database.Connection.Open();
     Tests test = new Tests()
     {
         id = e.Id,
         AuthorId = e.AuthorId,
         Name = e.Name,
         Description = e.Description,
         AllowedTime = e.AllowedTime,
         Anonymous = e.Anonymous,
         CreationDate = e.CreationDate,
         GlobalAvailability = e.GlobalAvailability,
         Interview = e.Interview,
         QuestionCount = e.QuestionCount
     };
     context.Set<Tests>().Add(test);
 }
 public void StartTest(DalUser user, DalTest test)
 {
     if (IsUserTested(user))
         return;
     Users ormuser = context.Set<Users>().FirstOrDefault(users => users.id == user.Id);        
     Tests ormtest = context.Set<Tests>().FirstOrDefault(tests => tests.id == test.Id);
     UsersTests startingTest = new UsersTests()
     {
         TestId = ormtest.id,
         BeginningTime = DateTime.Now,
         EndingTime = null,
         MarkForTest = null,
         RightAnswersCount = null,
         UserId = ormuser.id
     };
     context.Set<UsersTests>().Add(startingTest);
 }
 public void SetCoauthor(DalUser user, DalTest test)
 {
     context.Database.Connection.Open();
     Users ormuser = context.Set<Users>().FirstOrDefault(users => users.id == user.Id);
     Tests ormtest = context.Set<Tests>().FirstOrDefault(tests => tests.id == test.Id);
     ormtest.CoAuthors.Add(ormuser);
 }
 public bool AllowedUser(DalTest test, DalUser user)
 {
     if (context.Database.Connection.State != ConnectionState.Open)
         context.Database.Connection.Open();
     Users ormuser = context.Set<Users>().FirstOrDefault(users => users.id == user.Id);
     Tests ormtest = context.Set<Tests>().FirstOrDefault(tests => tests.id == test.Id);
     return ormtest.AllowedUsers.Contains(ormuser);
 //    return context.Set<Tests>().Any(t => t.AllowedUsers.Contains(ormuser)) && t.id == test.Id);
 }
 public void Update(DalTest entity)
 {
     if (context.Database.Connection.State != ConnectionState.Open)
         context.Database.Connection.Open();
     Tests test = context.Set<Tests>().FirstOrDefault(tests => tests.id == entity.Id);
     //check existing
     test.Name = entity.Name;
     test.AllowedTime = entity.AllowedTime;
     test.Anonymous = entity.Anonymous;
     test.AuthorId = entity.AuthorId;
     test.CreationDate = entity.CreationDate;
     test.Description = entity.Description;
     test.GlobalAvailability = entity.GlobalAvailability;
     test.Interview = entity.Interview;
     test.QuestionCount = entity.QuestionCount;
     test.RequiredQuestionCount = entity.RequiredQuestionCount;
 }
 public void Delete(DalTest e)
 {
     context.Database.Connection.Open();
     Tests test = context.Set<Tests>().FirstOrDefault(tests => tests.id == e.Id);
     context.Set<Tests>().Remove(test);
 }
 public IEnumerable<DalQuestion> GetAllTestQuestions(DalTest test)
 {
     if (context.Database.Connection.State != ConnectionState.Open)
         context.Database.Connection.Open();
     IEnumerable<Questions> questions = context.Set<Questions>().Where(q => q.TestId == test.Id).AsEnumerable();
     List<DalQuestion> result = new List<DalQuestion>();
     foreach(Questions question in questions)
     {
         result.Add(new DalQuestion()
         {
             Id = question.id,
             QuestionNumberInTest = question.QuestionNumberInTest,
             QuestionStructure = question.QuestionStructure,
             TestId = question.TestId
         });
     }
     return result;
 }
 public IEnumerable<DalUser> GetCoauthors(DalTest test)
 {
     if (context.Database.Connection.State != ConnectionState.Open)
         context.Database.Connection.Open();
     Tests ormtest = context.Set<Tests>().FirstOrDefault(tests => tests.id == test.Id);
     IEnumerable<Users> users = ormtest.CoAuthors.AsEnumerable();
     List<DalUser> result = new List<DalUser>();
     foreach (Users u in users)
     {
         result.Add(new DalUser()
         {
             Id = u.id,
             Login = u.Login,
             Email = u.Email,
             FirstName = u.FirstName,
             SecondName = u.SecondName,
             ThirdName = u.ThirdName,
             PasswordHash = u.PasswordHash
         });
     }
     return result;
 }