コード例 #1
0
ファイル: SqlServerDbService.cs プロジェクト: s19041/APBD10
        public EnrollStudentResponse EnrollStudent(EnrollStudentRequest request)
        {
            var database = new s19041Context();
            var studies  = database.Studies.First(stud => stud.Name.Equals(request.Studies));

            if (studies.Equals(null))
            {
                throw new Exception("No such studies: " + request.Studies);
            }

            var student = database.Student.First(stud => stud.IndexNumber == request.IndexNumber);

            if (!student.Equals(null))
            {
                throw new Exception("Student " + request.IndexNumber + " already exists");
            }
            var enrollment = database.Enrollment.First(enroll => enroll.IdStudy == studies.IdStudy && enroll.Semester == 1);
            var newStudent = new GeneratedModels.Student
            {
                IndexNumber = request.IndexNumber,
                FirstName   = request.FirstName,
                LastName    = request.LastName,
                BirthDate   = request.BirthDate
            };
            var response = new EnrollStudentResponse();

            if (enrollment.Equals(null))
            {
                var newEnrollment = new GeneratedModels.Enrollment
                {
                    IdEnrollment      = database.Enrollment.Max(enroll => enroll.IdEnrollment),
                    Semester          = 1,
                    IdStudy           = studies.IdStudy,
                    StartDate         = DateTime.Now,
                    IdStudyNavigation = studies
                };
                database.Enrollment.Add(newEnrollment);
                newStudent.IdEnrollment           = newEnrollment.IdEnrollment;
                newStudent.IdEnrollmentNavigation = newEnrollment;
                response.IdEnrollment             = newEnrollment.IdEnrollment;
                response.Semester  = newEnrollment.Semester;
                response.IdStudy   = newEnrollment.IdStudy;
                response.StartDate = newEnrollment.StartDate;
            }
            else
            {
                newStudent.IdEnrollment           = enrollment.IdEnrollment;
                newStudent.IdEnrollmentNavigation = enrollment;
                response.IdEnrollment             = enrollment.IdEnrollment;
                response.Semester  = enrollment.Semester;
                response.IdStudy   = enrollment.IdStudy;
                response.StartDate = enrollment.StartDate;
            }

            database.Add(newStudent);
            database.SaveChanges();
            return(response);
        }
コード例 #2
0
ファイル: SqlServerDbService.cs プロジェクト: s19041/APBD10
        public void UpdateStudent(UpdateStudentRequest request)
        {
            var database = new s19041Context();
            var student  = database.Student.First(st => st.IndexNumber == request.IndexNumber);

            student.FirstName    = request.FirstName != null ? request.FirstName : student.FirstName;
            student.LastName     = request.LastName != null ? request.LastName : student.LastName;
            student.BirthDate    = request.BirthDate.Equals(null) ? request.BirthDate : student.BirthDate;
            student.IdEnrollment = request.Enrollment.Equals(null) ? request.Enrollment : student.IdEnrollment;
            database.SaveChanges();
        }
コード例 #3
0
ファイル: SqlServerDbService.cs プロジェクト: s19041/APBD10
        public void DeleteStudent(string id)
        {
            var database = new s19041Context();
            var student  = new GeneratedModels.Student
            {
                IndexNumber = id
            };
            var roles = database.StudentRoles.Where(studentRoles => studentRoles.IndexNumber.Equals(id)).ToList();

            foreach (var role in roles)
            {
                database.Remove(role);
            }

            database.SaveChanges();
            database.Remove(student);
        }
コード例 #4
0
ファイル: SqlServerDbService.cs プロジェクト: s19041/APBD10
        public EnrollStudentResponse PromoteStudents(PromoteStudentRequest request)
        {
            var database   = new s19041Context();
            var enrollment = database.Enrollment.Include(enroll => enroll.IdStudyNavigation).FirstOrDefault(enroll => enroll.Semester == request.Semester && enroll.IdStudyNavigation.Name.Equals(request.Studies));

            if (enrollment == null)
            {
                throw new Exception("No such enrollment to promote");
            }

            var sem  = new SqlParameter("@semester", request.Semester);
            var stud = new SqlParameter("@Studies", request.Studies);

            database.Database.ExecuteSqlRaw("exec PromoteStudents @Studies,@Semester", stud, sem);
            EnrollStudentResponse response = new EnrollStudentResponse
            {
                IdEnrollment = enrollment.IdEnrollment,
                Semester     = enrollment.Semester + 1,
                IdStudy      = enrollment.IdStudy
            };

            return(response);
        }
コード例 #5
0
ファイル: SqlServerDbService.cs プロジェクト: s19041/APBD10
        public IEnumerable <GeneratedModels.Student> GetStudents()
        {
            var database = new s19041Context();

            return(database.Student.ToList());
        }