public void DeleteLecture(Przedmiot lectureExisted)
        {
            var             noteContainDeleteLecture = MongoDBContext.Oceny.Find(_ => true).ToList().FindAll(ocenaObj => ocenaObj.IdPrzedmiot == lectureExisted.Id);
            List <ObjectId> listNoteObjectContainIdDeletedLecture = new List <ObjectId>();

            foreach (var note in noteContainDeleteLecture)
            {
                listNoteObjectContainIdDeletedLecture.Add(note.Id);
            }

            //Delete Oceny which contains Id deleted lecture from concrete student and then replace student note list
            var studenciContainDeleteLecture = MongoDBContext.Studenci.Find(_ => true).ToList()
                                               .FindAll(studentObj =>
                                                        studentObj.Oceny.Any(noteObj => listNoteObjectContainIdDeletedLecture.Any(noteTempObj => noteTempObj == noteObj)));

            foreach (var student in studenciContainDeleteLecture)
            {
                student.Oceny.RemoveAll(ocenaObj => listNoteObjectContainIdDeletedLecture.Contains(ocenaObj));
                MongoDBContext.Studenci.ReplaceOne(studentObj => studentObj.Id == student.Id,
                                                   student, new UpdateOptions {
                    IsUpsert = true
                });
            }

            //Remove all Oceny, which contains Id deleted lecture
            foreach (var deleteNote in noteContainDeleteLecture)
            {
                MongoDBContext.Oceny.DeleteOne(ocenaObj => ocenaObj.Id == deleteNote.Id);
            }

            //Delete Przedmiot
            MongoDBContext.Przedmioty.DeleteOne(przedmiotObj => przedmiotObj.Id == lectureExisted.Id);
        }
        public List <Ocena> GetNotesStudentsByFilter(Student student, Przedmiot przedmiot, double?ge, double?le)
        {
            try
            {
                var indexNumberFiler = Builders <Ocena> .Filter
                                       .Where(studentObj => studentObj.Indeks == student.Indeks);

                List <Ocena> notes = MongoDBContext.Oceny
                                     .Find(indexNumberFiler).ToList();

                if (ge != null)
                {
                    notes = notes.Where(o => o.Wartosc >= ge).ToList();
                }

                if (le != null)
                {
                    notes = notes.Where(o => o.Wartosc <= le).ToList();
                }

                return(notes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private List <Student> PrepareStudents()
        {
            List <Student> students = new List <Student>();

            Student student = new Student()
            {
                Imie          = "Paweł",
                Nazwisko      = "Nowak",
                Indeks        = 112513,
                DataUrodzenia = new DateTime(1980, 2, 12)
            };

            Przedmiot przedmiotRef = new Przedmiot();

            przedmiotRef.Id = 1;
            student.Przedmioty.Add(przedmiotRef);

            students.Add(student);

            student = new Student()
            {
                Imie          = "Tymoteusz",
                Nazwisko      = "Borowiak",
                Indeks        = 153623,
                DataUrodzenia = new DateTime(1983, 5, 20)
            };

            przedmiotRef    = new Przedmiot();
            przedmiotRef.Id = 2;
            student.Przedmioty.Add(przedmiotRef);

            students.Add(student);

            return(students);
        }
        public void UpdateStudentNote(Student student, Przedmiot lecture, Ocena noteTemp, Ocena updatedNote)
        {
            if (student != null && lecture != null && noteTemp != null && updatedNote != null)
            {
                //Prepare Ocena field
                float    wartoscOceny = updatedNote.Wartosc;
                DateTime time         = updatedNote.DataWystawienia;

                updatedNote = noteTemp;

                if (time == null)
                {
                    updatedNote.DataWystawienia = DateTime.Now;
                }
                else
                {
                    updatedNote.DataWystawienia = time;
                }

                updatedNote.Wartosc = wartoscOceny;

                //Replace in Oceny (only, student store objectid note )
                MongoDBContext.Oceny.ReplaceOne(ocenyObj => ocenyObj.Id == updatedNote.Id, updatedNote, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
        public Przedmiot AddLecture(Przedmiot lecture)
        {
            Przedmiot lectureToInsert = new Przedmiot();

            if (lecture != null)
            {
                //Find last added Przedmiot
                int       przedmiotIndex     = 0;
                Przedmiot lastAddedPrzedmiot = MongoDBContext.Przedmioty.Find(_ => true).ToList().OrderByDescending(przedmiotObj => przedmiotObj.IdPrzedmiotu).FirstOrDefault();

                if (lastAddedPrzedmiot != null)
                {
                    przedmiotIndex = lastAddedPrzedmiot.IdPrzedmiotu;
                    przedmiotIndex++;
                }

                lectureToInsert.IdPrzedmiotu = przedmiotIndex;
                lectureToInsert.Nauczyciel   = lecture.Nauczyciel;
                lectureToInsert.Nazwa        = lecture.Nazwa;

                MongoDBContext.Przedmioty.InsertOne(lectureToInsert);

                return(lectureToInsert);
            }

            return(null);
        }
 public void AddLecture(Przedmiot lecture)
 {
     if (lecture != null)
     {
         lecture.Id = generateUniqueLectureID();
         Lectures.Add(lecture);
     }
 }
 public void AddStudentToLecture(Student studentExisted, Przedmiot lectureExisted)
 {
     if (studentExisted != null && lectureExisted != null)
     {
         var studentFounded      = Students.Find(studentObj => studentObj.Indeks == studentExisted.Indeks);
         int indexFoundedStudent = Students.IndexOf(studentFounded);
         Students[indexFoundedStudent].Przedmioty.Add(lectureExisted);
         //studentExisted.Przedmioty.Add(lectureExisted);
     }
 }
 public void AddStudentToLecture(Student studentExisted, Przedmiot lectureExisted)
 {
     if (studentExisted != null && lectureExisted != null)
     {
         lectureExisted.ZapisaniStudenci.Add(studentExisted.Id);
         MongoDBContext.Przedmioty.ReplaceOne(lectureObj => lectureObj.Id == lectureExisted.Id, lectureExisted, new UpdateOptions {
             IsUpsert = true
         });
     }
 }
        public void DeleteStudentFromLecture(Student studentExisted, Przedmiot lectureExisted)
        {
            if (studentExisted != null && lectureExisted != null)
            {
                lectureExisted.ZapisaniStudenci.RemoveAll(studentObj => studentObj == studentExisted.Id);

                MongoDBContext.Przedmioty.ReplaceOne(przedmiotObj => przedmiotObj.Id == lectureExisted.Id,
                                                     lectureExisted, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
        public IEnumerable <Student> GetStudentsFromLecture(Przedmiot lecture)
        {
            IEnumerable <Student> allStudentsList     = null;
            IEnumerable <Student> studentsFromLecture = null;

            if (lecture != null)
            {
                allStudentsList     = GetStudents().FindAll(_ => true).ToList();
                studentsFromLecture = allStudentsList.Where(s => lecture.ZapisaniStudenci.Contains(s.Id));
            }

            return(studentsFromLecture);
        }
        public void UpdateLecture(Przedmiot lectureFromDB, Przedmiot lectureFromBody)
        {
            if (lectureFromDB != null && lectureFromBody != null)
            {
                lectureFromDB.Nauczyciel = lectureFromBody.Nauczyciel;
                lectureFromDB.Nazwa      = lectureFromBody.Nazwa;

                MongoDBContext.Przedmioty.ReplaceOne(przedmiotObj => przedmiotObj.Id == lectureFromDB.Id,
                                                     lectureFromDB, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
        public void DeleteLecture(Przedmiot lectureExisted)
        {
            var lectureToDelete    = Lectures.Find(studentObj => studentObj.Id == lectureExisted.Id);
            int indexDeleteLecture = -1;

            if (lectureToDelete != null)
            {
                indexDeleteLecture = Lectures.IndexOf(lectureToDelete);

                if (indexDeleteLecture >= 0)
                {
                    Lectures.RemoveAt(indexDeleteLecture);
                }
            }
        }
        public Przedmiot getLecture(int indexLecture)
        {
            Przedmiot przedmiotRef = Lectures.Find(lectureObj => lectureObj.Id == indexLecture);

            try
            {
                przedmiotRef = Lectures.Find(lectureObj => lectureObj.Id == indexLecture);
            }
            catch
            {
                przedmiotRef = null;
            }

            return(przedmiotRef);
        }
        public void DeleteStudentNote(Student student, Przedmiot lecture, Ocena note)
        {
            if (student != null && lecture != null && note != null)
            {
                Ocena ocena = Notes.Find(noteObj => noteObj.Id == note.Id &&
                                         student.Indeks == note.IdStudent &&
                                         lecture.Id == note.IdPrzedmiot);

                if (ocena != null)
                {
                    var indexFoundedNotes = Notes.IndexOf(ocena);
                    Notes.RemoveAt(indexFoundedNotes);
                }
            }
        }
        public void DeleteStudentFromLecture(Student studentExisted, Przedmiot lectureExisted)
        {
            int indexFoundedStudent = -1;
            int indexFoundedLecture = -1;

            if (studentExisted != null && lectureExisted != null)
            {
                indexFoundedStudent = Students.IndexOf(studentExisted);
                var lectureFounded = Students[indexFoundedStudent].Przedmioty.Find(lectureObj => lectureObj.Id == lectureExisted.Id);
                indexFoundedLecture = Students[indexFoundedStudent].Przedmioty.IndexOf(lectureFounded);

                if (indexFoundedLecture >= 0)
                {
                    Students[indexFoundedStudent].Przedmioty.RemoveAt(indexFoundedLecture);
                }
            }
        }
        public bool UpdateLecture(Przedmiot updateLecture)
        {
            bool IsLectureUpdated = false;
            var  lectureToReplace = Lectures.Find(lectureObj => lectureObj.Id == updateLecture.Id);

            if (lectureToReplace != null)
            {
                var indexOfReplacedStudent = Lectures.IndexOf(lectureToReplace);

                if (indexOfReplacedStudent >= 0)
                {
                    Lectures[indexOfReplacedStudent] = updateLecture;
                    IsLectureUpdated = true;
                }
            }

            return(IsLectureUpdated);
        }
        public void DeleteStudentNote(Student student, Przedmiot lecture, Ocena note)
        {
            if (student != null && lecture != null && note != null)
            {
                //Delete from Oceny
                MongoDBContext.Oceny.DeleteMany(ocenaObj => ocenaObj.Id == note.Id &&
                                                ocenaObj.IdPrzedmiot == note.IdPrzedmiot &&
                                                ocenaObj.IdStudent == note.IdStudent);

                //Delete from concrete student
                student.Oceny.RemoveAll(ocenaObj => ocenaObj == note.Id);

                //Replace modified student
                MongoDBContext.Studenci.ReplaceOne(studentObj => studentObj.Id == student.Id,
                                                   student, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
        public void AddNoteStudentFromLecture(Student studentExisted, Przedmiot lectureExisted, Ocena note)
        {
            var studentFounded = Students.Find(studentObj => studentObj.Indeks == studentExisted.Indeks);
            var lectureFounded = Lectures.Find(lectureObj => lectureObj.Id == lectureExisted.Id);

            if (studentExisted != null && lectureExisted != null && note != null)
            {
                int indexStudent = Students.IndexOf(studentFounded);
                int indexLecture = Lectures.IndexOf(lectureFounded);

                note.Id              = generateUniqueNoteID();
                note.IdStudent       = studentExisted.Indeks;
                note.IdPrzedmiot     = lectureExisted.Id;
                note.DataWystawienia = DateTime.Now;

                Students[indexStudent].Oceny.Add(note);
                Notes.Add(note);
                //studentExisted.Przedmioty.Add(lectureExisted);
            }
        }
        public void AddNoteStudentFromLecture(Student studentExisted, Przedmiot lectureExisted, Ocena note)
        {
            if (studentExisted != null && lectureExisted != null && note != null)
            {
                //Find last added Ocena
                long  ocenyIndex    = 0;
                Ocena lastAddedNote = MongoDBContext.Oceny.Find(_ => true).ToList().OrderByDescending(ocenaObj => ocenaObj.IdOceny).FirstOrDefault();

                if (lastAddedNote != null)
                {
                    ocenyIndex = lastAddedNote.IdOceny;
                    ocenyIndex++;
                }

                //Prepare Ocena field
                note.IdOceny      = (int)ocenyIndex;
                note.IdPrzedmiotu = lectureExisted.IdPrzedmiotu;
                note.Indeks       = studentExisted.Indeks;
                note.IdStudent    = studentExisted.Id;
                note.IdPrzedmiot  = lectureExisted.Id;

                if (note.DataWystawienia == null)
                {
                    note.DataWystawienia = DateTime.Now;
                }

                //Add to Oceny
                MongoDBContext.Oceny.InsertOne(note);

                //Add to concrete Student Oceny
                var ocenaLastAdded = MongoDBContext.Oceny.Find(_ => true).ToList().OrderByDescending(ocenaObj => ocenaObj.IdOceny).FirstOrDefault();
                studentExisted.Oceny.Add(ocenaLastAdded.Id);

                //Replace old Student data to new
                MongoDBContext.Studenci.ReplaceOne(studentObj => studentObj.Id == studentExisted.Id, studentExisted, new UpdateOptions {
                    IsUpsert = true
                });
            }
        }
        public void UpdateStudentNote(Student student, Przedmiot lecture, Ocena note)
        {
            var studentFounded = Students.Find(studentObj => studentObj.Indeks == student.Indeks);
            var lectureFounded = Lectures.Find(lectureObj => lectureObj.Id == lecture.Id);
            var noteFounded    = Notes.Find(lectureObj => lectureObj.Id == note.Id);

            if (studentFounded != null && lectureFounded != null && noteFounded != null)
            {
                int indexStudent = Students.IndexOf(studentFounded);
                int indexLecture = Lectures.IndexOf(lectureFounded);
                int indexNote    = Notes.IndexOf(noteFounded);

                note.IdStudent       = student.Indeks;
                note.IdPrzedmiot     = lecture.Id;
                note.DataWystawienia = DateTime.Now;

                //Students[indexStudent].Oceny.Add(note);
                Notes.RemoveAt(indexNote);
                Notes.Insert(indexNote, note);
                //studentExisted.Przedmioty.Add(lectureExisted);
            }
        }
        List <Przedmiot> PrepareLectures()
        {
            List <Przedmiot> lectures = new List <Przedmiot>();

            Przedmiot lecture = new Przedmiot()
            {
                Id         = 1,
                Nazwa      = "Język polski",
                Nauczyciel = "Mikołaj Nowacki",
            };

            lectures.Add(lecture);

            lecture = new Przedmiot()
            {
                Id         = 2,
                Nazwa      = "Matematyka",
                Nauczyciel = "Marta Polak",
            };

            lectures.Add(lecture);

            return(lectures);
        }
 public void UpdateNote(Przedmiot lecture, Student student, Ocena note, Ocena noteFromBody)
 {
     UpdateStudentNote(student, lecture, note, noteFromBody);
 }
 public void DeleteNote(Student student, Przedmiot lecture, Ocena note)
 {
     DeleteStudentNote(student, lecture, note);
 }
        private void PrepareStudents()
        {
            MongoDBContext.Studenci.DeleteMany(_ => true);
            MongoDBContext.Przedmioty.DeleteMany(_ => true);
            MongoDBContext.Oceny.DeleteMany(_ => true);

            Student studentPawel = new Student()
            {
                Imie          = "Paweł",
                Nazwisko      = "Nowak",
                Indeks        = 112513,
                DataUrodzenia = new DateTime(1980, 2, 12)
            };

            Student studentTymek = new Student()
            {
                Imie          = "Tymoteusz",
                Nazwisko      = "Borowiak",
                Indeks        = 153623,
                DataUrodzenia = new DateTime(1983, 5, 20)
            };

            Przedmiot przedmiotJezykPolski = new Przedmiot()
            {
                IdPrzedmiotu = 1,
                Nazwa        = "Polski",
                Nauczyciel   = "Bogdan Marecki",
            };

            Przedmiot przedmiotMatematyka = new Przedmiot()
            {
                IdPrzedmiotu = 2,
                Nazwa        = "Matematyka",
                Nauczyciel   = "Roman Bielecki",
            };

            MongoDBContext.Przedmioty.InsertOne(przedmiotJezykPolski);
            MongoDBContext.Przedmioty.InsertOne(przedmiotMatematyka);

            MongoDBContext.Studenci.InsertOne(studentPawel);
            MongoDBContext.Studenci.InsertOne(studentTymek);

            var przedmioty = MongoDBContext.Przedmioty.Find(_ => true).ToList();
            var studenci   = MongoDBContext.Studenci.Find(_ => true).ToList();

            var przedmiot = przedmioty.FindAll(przedmiotTemp => przedmiotTemp.Nazwa == przedmiotJezykPolski.Nazwa).FirstOrDefault();
            var student   = studenci.FindAll(studentTemp => studentTemp.Indeks == studentPawel.Indeks).FirstOrDefault();

            przedmiotJezykPolski.ZapisaniStudenci.Add(student.Id);

            przedmiot = przedmioty.FindAll(przedmiotTemp => przedmiotTemp.Nazwa == przedmiotMatematyka.Nazwa).FirstOrDefault();
            student   = studenci.FindAll(studentTemp => studentTemp.Indeks == studentTymek.Indeks).FirstOrDefault();
            przedmiotMatematyka.ZapisaniStudenci.Add(student.Id);

            MongoDBContext.Przedmioty.ReplaceOne(x => x.IdPrzedmiotu == 1, przedmiotJezykPolski, new UpdateOptions {
                IsUpsert = true
            });
            MongoDBContext.Przedmioty.ReplaceOne(x => x.IdPrzedmiotu == 2, przedmiotMatematyka, new UpdateOptions {
                IsUpsert = true
            });
        }