public string EnrollStudent(Models.Student s) { if (!(_context.Studies.Any(st => st.Name == s.Studies))) { return("Nie ma takich studiow"); } if ((_context.Student.Any(st => st.IndexNumber == s.IndexNumber))) { // JEST TAKI INDEX return("Nie jest to unikalny index"); } var s2 = new Models2.Student() { IndexNumber = s.IndexNumber, FirstName = s.FirstName, LastName = s.LastName, BirthDate = DateTime.Parse(s.BirthDate) }; //var result = _context.Studies.Where(stu => stu.Name == s.Studies).; var study = _context.Studies.Where(stu => stu.Name == s.Studies).FirstOrDefault(); int idStudy = study.IdStudy; if (!(_context.Enrollment.Any(en => en.IdStudy == idStudy && en.Semester == 1 && en.StartDate.Date == DateTime.Now.Date))) { var setStudent = new HashSet <Student> { s2 }; var enroll = new Models2.Enrollment() { IdEnrollment = _context.Enrollment.Select(en => en.IdEnrollment).Max() + 1, Semester = 1, StartDate = DateTime.Now.Date, Student = setStudent, }; study.Enrollment.Add(enroll); _context.Add(enroll); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("Utworzono enrollment i student"); } else { var enroll = _context.Enrollment.Where(en => en.IdStudy == idStudy && en.Semester == 1 && en.StartDate.Date == DateTime.Now.Date).FirstOrDefault(); enroll.Student.Add(s2); _context.Attach(enroll); _context.Entry(enroll).State = EntityState.Modified; _context.SaveChanges(); return("Utworzono student"); } }
public string EnrollStudent(Models.Student s) { int eskaUnik = 1; var student = new Student(); var studia = _context.Studies.Where(stu => stu.Name == s.Studies).ToList(); if (studia.Count == 0) { return("nie ma takiego kierunku"); } var unikId = _context.Student.Where(stu => stu.IndexNumber == s.IndexNumber).ToList(); if (unikId.Count != 0) { eskaUnik = 0; student = unikId[0]; } var stud = new Models2.Student() { IndexNumber = s.IndexNumber, FirstName = s.FirstName, LastName = s.LastName, BirthDate = DateTime.Parse(s.BirthDate) }; var study = _context.Studies.Where(stu => stu.Name == s.Studies).FirstOrDefault(); int idStudy = study.IdStudy; if ((!(_context.Enrollment.Any(enrl => enrl.IdStudy == idStudy && enrl.Semester == 1 && enrl.StartDate.Date == DateTime.Now.Date)))) { if (eskaUnik == 1) { var setStudent = new HashSet <Student> { stud }; var enroll = new Models2.Enrollment() { IdEnrollment = _context.Enrollment.Select(en => en.IdEnrollment).Max() + 1, Semester = 1, StartDate = DateTime.Now.Date, Student = setStudent, }; study.Enrollment.Add(enroll); _context.Add(enroll); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("dodano nowego studenta i nowy enroll"); } else { var setStudent = new HashSet <Student> { student }; var enroll = new Models2.Enrollment() { IdEnrollment = _context.Enrollment.Select(en => en.IdEnrollment).Max() + 1, Semester = 1, StartDate = DateTime.Now.Date, Student = setStudent, }; study.Enrollment.Add(enroll); _context.Add(enroll); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("zapisano studenta na nowy enroll"); } } else { if (eskaUnik == 1) { var enroll = _context.Enrollment.Where(en => en.IdStudy == idStudy && en.Semester == 1 && en.StartDate.Date == DateTime.Now.Date).FirstOrDefault(); enroll.Student.Add(stud); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("dodano nowego studenta na instniejacy enroll"); } else { var enroll = _context.Enrollment.Where(en => en.IdStudy == idStudy && en.Semester == 1 && en.StartDate.Date == DateTime.Now.Date).FirstOrDefault(); enroll.Student.Add(student); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("dodano studenta na instniejacy enroll"); } } }