示例#1
0
        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
        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);
        }