public IActionResult EnrollStudent(EnrollStudentRequest request) { //czy index jest unikalny if (db.Student.Any(s => s.IndexNumber.Equals(request.IndexNumber))) { return(BadRequest("Student " + request.IndexNumber + " już istnieje")); } //czy studia istnieją if (!db.Studies.Any(s => s.Name.Equals(request.Studies))) { return(BadRequest("Studia " + request.Studies + " nie istnieją")); } //czy istnieje wpis dla studiow, z semestrem 1 var studies = db.Studies.Where(s => s.Name.Equals(request.Studies)).First(); Enrollment enrollment = null; if (!db.Enrollment.Any(e => e.Semester == 1 && e.IdStudy == studies.IdStudy)) { enrollment = new Enrollment(); enrollment.IdEnrollment = db.Enrollment.Max(e => e.IdEnrollment) + 1; enrollment.Semester = 1; enrollment.IdStudy = studies.IdStudy; enrollment.StartDate = DateTime.Now; db.Enrollment.Add(enrollment); } else { enrollment = db.Enrollment.Where(e => e.Semester == 1 && e.IdStudy == studies.IdStudy).First(); } //zapisanie studenta var student = new Student(); student.IndexNumber = request.IndexNumber; student.FirstName = request.FirstName; student.LastName = request.LastName; student.BirthDate = request.BirthDate; student.IdEnrollment = enrollment.IdEnrollment; var response = new EnrollStudentResponse(); response.IndexNumber = request.IndexNumber; response.FirstName = request.FirstName; response.LastName = request.LastName; response.Studies = request.Studies; response.Semester = 1; db.Student.Add(student); db.SaveChanges(); return(Created("Zapisano studenta", response)); }
public IActionResult ModifyStudentData(ModifyStudentDataRequest request) { var s = db.Student.Find(request.IndexNumber); if (s == null) { return(BadRequest("Student o indeksie " + request.IndexNumber + " nie istnieje")); } else { s.FirstName = request.FirstName; s.LastName = request.LastName; s.BirthDate = request.BirthDate; s.IdEnrollment = request.IdEnrollment; db.SaveChanges(); return(Ok(s)); } }