Beispiel #1
0
        public IActionResult Modify(Lista lista)
        {
            Modify model = new Modify()
            {
                selectedItem = lista.selectedItem,
                what         = lista.what,
                listy        = new Listy()
            };

            using (var db = new MyContext())
            {
                NTR20Z.Activity toModify = db.activities.Find(lista.selectedSlot.activityID);
                model.toModify = JsonSerializer.Serialize(toModify);
                model.group    = db.groups.Find(toModify.GroupID).name;
                model.room     = db.rooms.Find(toModify.RoomID).name;
                model.teacher  = db.teachers.Find(db.assignments.Single(ass => ass.GroupID == toModify.GroupID &&
                                                                        ass.SubjectID == toModify.SubjectID).TeacherID).name;
                var act = 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
                {
                    ActivityID = activity.ActivityID,
                    SlotID     = activity.SlotID,
                    GroupID    = activity.GroupID,
                    RoomID     = activity.RoomID,
                    TeacherID  = assignment.TeacherID,
                    SubjectID  = assignment.SubjectID
                }).ToList();
                foreach (var teacher in db.teachers)
                {
                    if (!act.Any(act => act.TeacherID == teacher.TeacherID && act.SlotID == toModify.SlotID && act.ActivityID != toModify.ActivityID))
                    {
                        model.listy.teachers.Add(teacher.name);
                    }
                }
                foreach (var group in db.groups)
                {
                    if (!act.Any(act => act.GroupID == group.GroupID && act.SlotID == toModify.SlotID && act.ActivityID != toModify.ActivityID))
                    {
                        model.listy.groups.Add(group.name);
                    }
                }
                foreach (var room in db.rooms)
                {
                    if (!act.Any(act => act.RoomID == room.RoomID && act.SlotID == toModify.SlotID && act.ActivityID != toModify.ActivityID))
                    {
                        model.listy.rooms.Add(room.name);
                    }
                }
            }
            return(View("Modify", model));
        }
Beispiel #2
0
 public IActionResult Delete(Modify model)
 {
     NTR20Z.Activity toModify = JsonSerializer.Deserialize <NTR20Z.Activity>(model.toModify);
     using (var db = new MyContext())
     {
         NTR20Z.Activity toModifyFromDB = db.activities.Find(toModify.ActivityID);
         if (toModifyFromDB == null)
         {
             return(View("DeleteError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         if (toModifyFromDB.Timestamp != toModify.Timestamp)
         {
             return(View("DeleteError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         db.activities.Remove(toModifyFromDB);
         try
         {
             Lista.mutex.WaitOne();
             db.SaveChanges();
             Lista.mutex.ReleaseMutex();
         }
         catch (Exception)
         {
             Lista.mutex.ReleaseMutex();
             return(View("DeleteError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
     }
     return(Index(new Lista()
     {
         what = model.what, selectedItem = model.selectedItem
     }));
 }
Beispiel #3
0
 public IActionResult SetAssignmentFromModify(Modify model)
 {
     NTR20Z.Activity toModify = JsonSerializer.Deserialize <NTR20Z.Activity>(model.toModify);
     using (var db = new MyContext())
     {
         int teacher = db.teachers.Single(teacher => teacher.name == model.teacher).TeacherID;
         int subject = db.subjects.Single(sub => sub.name == model.subject).SubjectID;
         Lista.mutex.WaitOne();
         if (db.assignments.Any(ass => ass.GroupID == toModify.GroupID && (ass.TeacherID == teacher || ass.SubjectID == subject)))
         {
             Lista.mutex.ReleaseMutex();
             return(View("TeacherUnassignable", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         db.assignments.Add(new Assignment()
         {
             TeacherID = teacher,
             GroupID   = toModify.GroupID,
             SubjectID = subject
         });
         db.SaveChanges();
         Lista.mutex.ReleaseMutex();
         NTR20Z.Activity toModifyFromDB = db.activities.Find(toModify.ActivityID);
         if (toModifyFromDB == null)
         {
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         if (toModifyFromDB.Timestamp != toModify.Timestamp)
         {
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         toModifyFromDB.GroupID   = toModify.GroupID;
         toModifyFromDB.RoomID    = toModify.RoomID;
         toModifyFromDB.SubjectID = subject;
         try
         {
             Lista.mutex.WaitOne();
             db.SaveChanges();
             Lista.mutex.ReleaseMutex();
         }
         catch (Exception)
         {
             Lista.mutex.ReleaseMutex();
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
     }
     return(Index(new Lista()
     {
         what = model.what, selectedItem = model.selectedItem
     }));
 }
Beispiel #4
0
 public IActionResult Modify(Modify model)
 {
     NTR20Z.Activity toModify = JsonSerializer.Deserialize <NTR20Z.Activity>(model.toModify);
     using (var db = new MyContext())
     {
         NTR20Z.Activity toModifyFromDB = db.activities.Find(toModify.ActivityID);
         if (toModifyFromDB == null)
         {
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         if (toModifyFromDB.Timestamp != toModify.Timestamp)
         {
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
         toModifyFromDB.GroupID = db.groups.Single(gr => gr.name == model.group).GroupID;
         toModifyFromDB.RoomID  = db.rooms.Single(gr => gr.name == model.room).RoomID;
         int teacherID = db.teachers.Single(te => te.name == model.teacher).TeacherID;
         if (!db.assignments.Any(ass => ass.GroupID == toModifyFromDB.GroupID && ass.TeacherID == teacherID))
         {
             model.toModify      = JsonSerializer.Serialize(toModifyFromDB);
             ViewBag.subjectList = new List <string>();
             var  subjects = db.subjects.ToList();
             bool isEmpty  = true;
             foreach (var it in subjects)
             {
                 if (!db.assignments.Any(ass => ass.GroupID == toModifyFromDB.GroupID && ass.SubjectID == it.SubjectID))
                 {
                     ViewBag.subjectList.Add(it.name);
                     isEmpty = false;
                 }
             }
             if (isEmpty)
             {
                 return(View("TeacherUnassignable", new Err()
                 {
                     what = model.what, selectedItem = model.selectedItem
                 }));
             }
             return(View("SetAssignmentFromModify", model));
         }
         toModifyFromDB.SubjectID = db.assignments.Single(ass => ass.TeacherID == teacherID && ass.GroupID == toModifyFromDB.GroupID).SubjectID;
         try
         {
             Lista.mutex.WaitOne();
             db.SaveChanges();
             Lista.mutex.ReleaseMutex();
         }
         catch (Exception)
         {
             Lista.mutex.ReleaseMutex();
             return(View("UpdateError", new Err()
             {
                 what = model.what, selectedItem = model.selectedItem
             }));
         }
     }
     return(Index(new Lista()
     {
         what = model.what, selectedItem = model.selectedItem
     }));
 }