Example #1
0
        //Korzystam z innego modelu studenta, bo nie chcę, żeby można było podawać sól itp.
        public void ModifyStudent(Models.Student newData)
        {
            var Db       = new s19110Context();
            var toUpdate = Db.Student.Where(st => st.IndexNumber == newData.IndexNumber).FirstOrDefault();

            if (toUpdate == null)
            {
                throw new StudentNotFoundException("Nie ma takiego studenta");
            }
            toUpdate.FirstName = newData.FirstName != null ? newData.FirstName : toUpdate.FirstName;
            toUpdate.LastName  = newData.LastName != null ? newData.LastName : toUpdate.LastName;
            toUpdate.BirthDate = newData.BirthDate.Equals(null) ? newData.BirthDate : toUpdate.BirthDate;


            if (newData.Studies != null && newData.Semester != 0)
            {
                var enrollment = Db.Enrollment.Where(en => en.IdStudyNavigation.Name.Equals(newData.Studies) && en.Semester == newData.Semester).FirstOrDefault();
                if (enrollment != null)
                {
                    toUpdate.IdEnrollment           = enrollment.IdEnrollment;
                    toUpdate.IdEnrollmentNavigation = enrollment;
                }
                else
                {
                    var study = Db.Studies.Where(st => st.Name.Equals(newData.Studies)).FirstOrDefault();
                    if (study == null)
                    {
                        throw new ArgumentException("Podany typ studiów nie istnieje");
                    }
                    var newEnrollment = new NewModels.Enrollment()
                    {
                        IdStudy           = study.IdStudy,
                        IdStudyNavigation = study,
                        Semester          = newData.Semester
                    };
                    Db.Enrollment.Add(newEnrollment);
                    toUpdate.IdEnrollmentNavigation = newEnrollment;
                }
            }

            Db.SaveChanges();
        }
Example #2
0
        public EnrollStudentResponse EnrollStudent(EnrollStudentRequest request)
        {
            var db = new s19110Context();

            //1. Czy studia instnieją?
            var study = db.Studies.Where(st => st.Name.Equals(request.Studies)).FirstOrDefault();

            if (study == null)
            {
                throw new ArgumentException("Podane studia nie instnieją");
            }
            //3. Sprawdzenie czy student o takim indeksie już istnieje
            var student = db.Student.Where(st => st.IndexNumber == request.IndexNumber).FirstOrDefault();

            if (student != null)
            {
                throw new ArgumentException("Student o podanym indeksie już znajduje się w bazie danych");
            }

            //3. Szukanie w tabeli Enrollment
            var enrollment = db.Enrollment.Where(en => en.IdStudy == study.IdStudy && en.Semester == 1).FirstOrDefault();

            var toRespond = new EnrollStudentResponse();

            toRespond.LastName = request.LastName;
            var addedStudent = new NewModels.Student()
            {
                IndexNumber = request.IndexNumber,
                FirstName   = request.FirstName,
                LastName    = request.LastName,
                BirthDate   = request.BirthDate
            };

            if (enrollment == null)
            {
                //Wstawianie nowego zapisu do bazy danych
                //Używam zagnieżdżonego selecta zamiast Identity, bo nie korzystałem z niego od początku
                var addedEnrollment = new NewModels.Enrollment()
                {
                    IdEnrollment      = db.Enrollment.Max(en => en.IdEnrollment),
                    Semester          = 1,
                    StartDate         = DateTime.Now,
                    IdStudy           = study.IdStudy,
                    IdStudyNavigation = study
                };
                db.Enrollment.Add(addedEnrollment);
                addedStudent.IdEnrollment           = addedEnrollment.IdEnrollment;
                addedStudent.IdEnrollmentNavigation = addedEnrollment;
                toRespond.Semester  = addedEnrollment.Semester;
                toRespond.StartDate = addedEnrollment.StartDate;
            }
            else
            {
                addedStudent.IdEnrollment           = enrollment.IdEnrollment;
                addedStudent.IdEnrollmentNavigation = enrollment;
                toRespond.Semester  = enrollment.Semester;
                toRespond.StartDate = enrollment.StartDate;
            }



            //4. Dodanie studenta
            db.Student.Add(addedStudent);
            db.SaveChanges();
            return(toRespond);
        }