public void checkAssignment(ZajeciaDB toAdd) { using (var db = new MyContext()) { if (!db.assignments.Any(a => a.TeacherID == toAdd.teacher && a.GroupID == toAdd.group)) { throw new Exception("The teacher is not assigned to this class! An assignment menu should have appeared."); } } }
public void changeSlot(Zajecia toChange) { ZajeciaDB toAdd = new ZajeciaDB(toChange); checkAssignment(toAdd); if (data.isSlotOccupied(toChange)) { throw new Exception("1"); } else { data.activities.Add(toChange); using (var db = new MyContext()) { mutex.WaitOne(); if (db.activities.Join(db.assignments, activity => new Tuple <int, int>(activity.GroupID, activity.SubjectID), assignment => new Tuple <int, int>(assignment.GroupID, assignment.SubjectID), (activity, assignment) => new { SlotID = activity.SlotID, GroupID = activity.GroupID, RoomID = activity.RoomID, TeacherID = assignment.TeacherID, }).Any(act => act.SlotID == toAdd.slot && (act.GroupID == toAdd.group || act.RoomID == toAdd.room || act.TeacherID == toAdd.teacher))) { mutex.ReleaseMutex(); throw new Exception("1"); } db.activities.Add(new Activity() { SubjectID = toAdd.subject, GroupID = toAdd.group, RoomID = toAdd.room, SlotID = toAdd.slot }); db.SaveChanges(); mutex.ReleaseMutex(); } } //saveData(); }