public Enrollment PostProm(PostProm request) { /*if (!ModelState.IsValid) * { * var d = ModelState; * return BadRequest("!!!"); * }*/ var result = new List <Enrollment>(); if (request.Studies.Length == 0 || request.Semester.ToString().Length == 0) { return(null);//BadRequest("Nie podano wszystkich danych"); } using (SqlConnection con = new SqlConnection(ConnString)) using (SqlCommand com = new SqlCommand()) { com.Connection = con; con.Open(); //SqlTransaction tran = con.BeginTransaction(); //var tran = con.BeginTransaction(); com.CommandText = "select 1 kolumna from Enrollment where semester =" + request.Semester + " and idstudy = (select idstudy from studies where name ='" + request.Studies + "')"; com.Parameters.AddWithValue("semester", request.Semester); com.Parameters.AddWithValue("name", request.Studies); var dr = com.ExecuteReader(); int jest = 0; if (dr.Read()) { jest = (int)dr["kolumna"]; } if (jest == 0) { return(null);//NotFound("nie znaleziono danycjh w tabeli enrolments"); } dr.Close(); var trans = con.BeginTransaction(); com.Transaction = trans; com.CommandText = "exec promotion @name2, @semester2"; com.Parameters.AddWithValue("semester2", request.Semester); com.Parameters.AddWithValue("name2", request.Studies); com.ExecuteNonQuery(); var enrol = new Enrollment(); com.CommandText = "select * from Enrollment join Studies on Enrollment.IdStudy = Studies.IdStudy where Semester = @semester3 +1 and Name = @name3"; com.Parameters.AddWithValue("semester3", request.Semester); com.Parameters.AddWithValue("name3", request.Studies); dr = com.ExecuteReader(); if (dr.Read()) { enrol.IdEnrollment = (int)dr["IdEnrollment"]; enrol.IdStudy = (int)dr["IdStudy"]; enrol.Semester = (int)dr["Semester"]; enrol.StartDate = (DateTime)dr["StartDate"]; } dr.Close(); trans.Commit(); return(enrol);//Created("", enrol); } }
public IActionResult PostProm(PostProm request) { var enrl = _context.PostProm(request); if (enrl == null) { return(BadRequest("nie ma takich studiów lub nie uzupełniono wszytskich danych")); } return(Created("", enrl)); }
public string PostProm(PostProm prom) { if (prom.Studies.Length == 0 || prom.Semester.ToString().Length == 0) { return(null); } var study = _context.Studies.Where(stu => stu.Name == prom.Studies).FirstOrDefault(); int idStudy = study.IdStudy; if (!(_context.Enrollment.Any(st => st.Semester == prom.Semester && st.IdStudy == idStudy))) { return(null); } if (_context.Enrollment.Any(enrl => enrl.IdStudy == idStudy && enrl.Semester == prom.Semester + 1)) { var Enroll = _context.Enrollment.Where(enrl => enrl.IdStudy == idStudy && enrl.Semester == prom.Semester + 1).FirstOrDefault(); var Enroll_old = _context.Enrollment.Where(enrl => enrl.IdStudy == idStudy && enrl.Semester == prom.Semester).OrderBy(enrl => enrl.StartDate).FirstOrDefault(); var studt = _context.Student.Where(stu => stu.IdEnrollment == Enroll_old.IdEnrollment); var nstudenci = _context.Student.Where(stu => stu.IdEnrollment == Enroll.IdEnrollment); var set = nstudenci.Union(studt); Enroll.Student = set.ToHashSet(); _context.Attach(Enroll); _context.Entry(Enroll).State = EntityState.Modified; _context.SaveChanges(); return("przepisano " + nstudenci.Count() + " studentów na wyższy sem o id=" + Enroll.IdEnrollment.ToString()); } else { var Enroll = new Enrollment(); var oldEnroll = _context.Enrollment.Where(enrl => enrl.IdStudy == idStudy && enrl.Semester == prom.Semester).OrderBy(enrl => enrl.StartDate).FirstOrDefault(); var studenci = _context.Student.Where(stu => stu.IdEnrollment == oldEnroll.IdEnrollment); Enroll.IdEnrollment = _context.Enrollment.Select(enrl => enrl.IdEnrollment).Max() + 1; Enroll.Semester = prom.Semester + 1; Enroll.StartDate = DateTime.Now.Date; Enroll.Student = studenci.ToHashSet(); study.Enrollment.Add(Enroll); _context.Add(Enroll); _context.Attach(study); _context.Entry(study).State = EntityState.Modified; _context.SaveChanges(); return("przepisano " + studenci.Count() + " studentów na wyższy sem o id=" + Enroll.IdEnrollment.ToString()); } }