Exemplo n.º 1
0
        public EnrollmentResponse EnrollStudent(EnrollStudentRequest request)
        {
            var db = new s18706Context();

            using (var dbTran = db.Database.BeginTransaction())
            {
                try
                {
                    var idStudies = db.Studies.SingleOrDefault(x => x.Name == request.Studies);
                    if (idStudies == null)
                    {
                        throw new Exception();
                    }

                    if (db.Enrollment.Where(x => x.Semester == 1)
                        .SingleOrDefault(x => x.IdStudy == idStudies.IdStudy) == null)
                    {
                        db.Enrollment.Add(new Enrollment
                        {
                            IdEnrollment = db.Enrollment.Max(x => x.IdEnrollment) + 1,
                            Semester     = 1,
                            IdStudy      = idStudies.IdStudy,
                            StartDate    = DateTime.Now
                        });
                    }

                    db.Student.Add(new Student
                    {
                        IndexNumber  = request.IndexNumber,
                        FirstName    = request.FirstName,
                        LastName     = request.LastName,
                        BirthDate    = request.Birthdate,
                        IdEnrollment = db.Enrollment.Where(x => x.Semester == 1)
                                       .Where(x => x.IdStudy == idStudies.IdStudy)
                                       .Select(x => x.IdEnrollment).First()
                    });

                    db.SaveChanges();
                    dbTran.Commit();
                    return(db.Enrollment.Where(x => x.Semester == 1)
                           .Where(x => x.IdStudy == idStudies.IdStudy).Select(x => new EnrollmentResponse
                    {
                        IdEnrollment = x.IdEnrollment,
                        IdStudy = x.IdStudy,
                        Semester = x.Semester,
                        StartDate = x.StartDate
                    }).First());
                }
                catch (Exception)
                {
                    dbTran.Rollback();
                    return(null);
                }
            }
        }
Exemplo n.º 2
0
        public Student DeleteStudent(string id)
        {
            var db      = new s18706Context();
            var student = db.Student.SingleOrDefault(x => x.IndexNumber == id);

            if (student != null)
            {
                db.Remove(student);
            }

            db.SaveChanges();
            return(student);
        }
Exemplo n.º 3
0
        public Student ModifyStudent(string id, string name, string surname)
        {
            var db      = new s18706Context();
            var student = db.Student.SingleOrDefault(x => x.IndexNumber == id);

            if (name != null && student != null)
            {
                student.FirstName = name;
            }
            if (surname != null && student != null)
            {
                student.LastName = surname;
            }

            db.SaveChanges();
            return(student);
        }