public Enrollment PromoteStudents(PromoteStudentsDTO promoteStudentsDto) { using (var client = new SqlConnection( "Data Source=db-mssql;Initial Catalog=s17428;Integrated Security=True")) using (var cmd = new SqlCommand("spPromoteStudents")) { cmd.Connection = client; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("studies", promoteStudentsDto.Studies); cmd.Parameters.AddWithValue("semester", promoteStudentsDto.Semester); client.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { var enrollment = new Enrollment(); enrollment.IdEnrollment = Convert.ToInt32(reader["IdEnrollment"]); enrollment.Semester = Convert.ToInt32(reader["Semester"]); enrollment.IdStudy = Convert.ToInt32(reader["IdStudy"]); enrollment.StartDate = Convert.ToDateTime(reader["StartDate"]); return(enrollment); } return(null); } }
public IActionResult PromoteStudents(PromoteStudentsDTO promoteStudents) { var studies = _studentsDbContext.Studies .Include(x => x.Enrollments) .FirstOrDefault(x => x.Name == promoteStudents.Studies); if (studies == null) { return(NotFound($"Nie znaleziono kierunku studiow {promoteStudents.Studies}")); } if (studies.Enrollments.All(x => x.Semester != promoteStudents.Semester)) { return(NotFound($"Nie znaleziono semestru {promoteStudents.Semester}")); } // sprawdź czy istnieje enrollment dla następnego semestru var nextSemesterEnrollment = studies.Enrollments .OrderByDescending(x => x.StartDate) .FirstOrDefault(x => x.Semester == promoteStudents.Semester + 1); if (nextSemesterEnrollment == null) { // tworzymy nowy enrollment z semestrem + 1 nextSemesterEnrollment = new ModelsEf.Enrollment() { IdEnrollment = _studentsDbContext.Enrollment.Max(x => x.IdEnrollment) + 1, Studies = studies, StartDate = DateTime.Now, Semester = promoteStudents.Semester + 1 }; _studentsDbContext.Enrollment.Add(nextSemesterEnrollment); } // promujemy studentow var studentsToPromote = _studentsDbContext.Student .Where(x => x.Enrollment.Studies == studies && x.Enrollment.Semester == promoteStudents.Semester) .ToList(); foreach (var student in studentsToPromote) { student.Enrollment = nextSemesterEnrollment; } _studentsDbContext.SaveChanges(); return(Created("", nextSemesterEnrollment)); }
public IActionResult PromoteStudents(PromoteStudentsDTO promoteStudents) { var studies = _dbService.GetStudiesByName(promoteStudents.Studies); if (studies == null) { return(NotFound($"Nie znaleziono kierunku studiow {promoteStudents.Studies}")); } if (_dbService.GetLatestEnrollment(promoteStudents.Semester, studies.IdStudy) == null) { return(NotFound($"Nie znaleziono semestru {promoteStudents.Semester}")); } var enrollment = _dbService.PromoteStudents(promoteStudents); return(Created("", enrollment)); }