private static void CreateOthers(AppDbContext ctx, IEnumerable <AppUser> students) { // Subjects, exams and registrations if (!ctx.Subjects.Any() && !ctx.Exams.Any()) { var subjectEnglish = new Subject("Engels"); var classroom = new Classroom(16, true, "ABC"); var examEnglish = new Exam(DateTime.Now.AddDays(-7), subjectEnglish, 15, classroom, true, true, new TimeSpan(1, 30, 0)); var examEnglish2 = new Exam(DateTime.Now.AddDays(7).AddHours(3), subjectEnglish, 15, classroom, true, true, new TimeSpan(1, 30, 0)); var examEnglish3 = new Exam(DateTime.Now.AddDays(7), subjectEnglish, 15, classroom, true, true, new TimeSpan(1, 30, 0)); ctx.Classrooms.Add(classroom); ctx.Subjects.Add(subjectEnglish); ctx.Exams.Add(examEnglish); ctx.Exams.Add(examEnglish2); if (!ctx.ExamRegistrations.Any()) { foreach (var s in students) { var reg = new ExamRegistration(examEnglish, s.Id, DateTime.Now); ctx.ExamRegistrations.Add(reg); } } } }
public Dictionary <List <ExamRegistration>, object[]> GetRegistredStudentsForExam(int courseId, int examPeriodId) { using (StudentDbEntities dbContext = new StudentDbEntities()) { List <ExamRegistration> examRegistrations = new List <ExamRegistration>(); var exam = dbContext.tblExams.FirstOrDefault(x => x.ExamPeriodId == examPeriodId && x.CourseId == courseId); if (exam == null) { return(new Dictionary <List <ExamRegistration>, object[]>()); } //Get Info about Exam object[] examInfo = new object[] { exam.DateAndTime, exam.Place }; var dbStudentIds = (from s in dbContext.tblStudents select s.StudentId).ToList(); var examQuery = (from examReg in dbContext.ExamRegistrationView where examReg.ExamId == exam.ExamId && examReg.IsRegistred == true select examReg).ToList(); foreach (var examReg in examQuery) { if (dbStudentIds.Contains(examReg.StudentId)) { ExamRegistration examRegistration = new ExamRegistration() { ExamRegistrationId = examReg.ExamRegistrationId, StudentNameAndSurname = examReg.NameAndSurname }; examRegistrations.Add(examRegistration); } } return(new Dictionary <List <ExamRegistration>, object[]>() { { examRegistrations, examInfo } }); } }
public ActionResult RegisterForExam(int id) { var ctx = new AppDbContext(); var exam = ctx.Exams.SingleOrDefault(e => e.Id == id); if (exam == null || !exam.DateTime.HasValue || exam.DateTime.Value < DateTime.Now) { return(new HttpUnauthorizedResult()); } var registration = new ExamRegistration(exam, User.Identity.GetUserId(), DateTime.Now); ctx.ExamRegistrations.Add(registration); ctx.SaveChanges(); return(RedirectToAction("RegisterExam")); }