public EnrollStudentResponse EnrollStudent(EnrollStudentRequest request) { using (var transaction = _dbContext.Database.BeginTransaction()) { if (_dbContext.Studies.FirstOrDefault(st => st.Name == request.Studies) == null) { return(null); } int idStudy = _dbContext.Studies.Where(st => st.Name == request.Studies).Select(st => st.IdStudy).SingleOrDefault(); PartialModels.Enrollment enrollment = _dbContext.Enrollment.FirstOrDefault(e => (e.IdStudy == idStudy) && (e.Semester == 1)); if (enrollment == null) { int maxId = _dbContext.Enrollment.Max(e => e.IdEnrollment); enrollment = new PartialModels.Enrollment(); enrollment.IdEnrollment = maxId + 1; enrollment.Semester = 1; enrollment.IdStudy = idStudy; enrollment.StartDate = DateTime.Now; _dbContext.Enrollment.Add(enrollment); } var StudentExist = _dbContext.Student.FirstOrDefault(student => student.IndexNumber.Equals(request.IndexNumber)); if (StudentExist != null) { return(null); } //"BirthDate": "1993-03-30" Student student = new Student(); student.IndexNumber = request.IndexNumber; student.FirstName = request.FirstName; student.LastName = request.LastName; student.BirthDate = request.BirthDate; student.IdEnrollment = enrollment.IdEnrollment; _dbContext.Student.Add(student); _dbContext.SaveChanges(); transaction.Commit(); var response = new EnrollStudentResponse() { LastName = request.LastName, Semester = 1, StartDate = DateTime.Now }; return(response); } }
public PromoteStudentResponse PromoteStudents(int semester, string studies) { using (var transaction = _dbContext.Database.BeginTransaction()) { int idStudy = _dbContext.Studies .Where(st => st.Name == studies) .Select(st => st.IdStudy).SingleOrDefault(); PartialModels.Enrollment enrollment = _dbContext.Enrollment.FirstOrDefault(e => e.IdStudy == idStudy && e.Semester == semester); if (enrollment == null) { return(null); } int oldIdEnrollment = enrollment.IdEnrollment; enrollment = _dbContext.Enrollment.FirstOrDefault(e => e.IdStudy == idStudy && e.Semester == semester + 1); if (enrollment == null) { int maxId = _dbContext.Enrollment.Max(e => e.IdEnrollment); enrollment = new PartialModels.Enrollment(); enrollment.IdEnrollment = maxId + 1; enrollment.Semester = semester + 1; enrollment.IdStudy = idStudy; enrollment.StartDate = DateTime.Now; _dbContext.Enrollment.Add(enrollment); _dbContext.SaveChanges(); } var students = _dbContext.Student.Where(s => s.IdEnrollment == oldIdEnrollment).ToList(); foreach (Student student in students) { student.IdEnrollment = enrollment.IdEnrollment; } _dbContext.SaveChanges(); transaction.Commit(); var response = new PromoteStudentResponse() { Study = studies, NewIdStudy = enrollment.IdStudy, NewSemester = enrollment.Semester }; return(response); } }