public void DeletePatient(Patient patient) { using var tran = context.Database.BeginTransaction(); var read = from p in context.Patient join pre in context.Prescription on p.IdPatient equals pre.IdPatient into gpre from pprree in gpre.DefaultIfEmpty() join pm in context.PrescriptionMedicament on pprree.IdPrescription equals pm.IdPrescription into gpm from ppmm in gpm.DefaultIfEmpty() where p.IdPatient == patient.IdPatient select new { p.IdPatient, pprree.IdPrescription, ppmm.IdMedicament }; var result = read.ToList(); var firstDelete = read.Where(x => x.IdMedicament != 0 && x.IdPrescription != 0).Select(x => new { x.IdPrescription, x.IdMedicament }); foreach (var item in firstDelete) { var e = new PrescriptionMedicament() { IdPrescription = item.IdPrescription, IdMedicament = item.IdMedicament }; context.PrescriptionMedicament.Attach(e); context.PrescriptionMedicament.Remove(e); } context.SaveChanges(); var secondDelete = read.Where(x => x.IdPrescription != 0).Select(x => new { x.IdPrescription }).Distinct(); foreach (var item in secondDelete) { var e = new Prescription() { IdPrescription = item.IdPrescription }; context.Prescription.Attach(e); context.Prescription.Remove(e); } context.SaveChanges(); var thirdDelete = read.Where(x => x.IdPatient == patient.IdPatient).Select(x => new { x.IdPatient }).Distinct(); foreach (var item in thirdDelete) { var e = new Patient() { IdPatient = item.IdPatient }; context.Patient.Attach(e); context.Patient.Remove(e); } context.SaveChanges(); tran.Commit(); }
public Student DeleteStudents(Student student) { var result = context.Student.FirstOrDefault(s => s.IndexNumber == student.IndexNumber); if (result == null) { return(null); } context.Student.Remove(result); context.SaveChanges(); return(result); }
public void AddPLayerToTeam(int id, PlayerRequest request) { var searchingPlayer = context.Player.FirstOrDefault(e => e.FirstName == request.FirstName && e.LastName == request.LastName); if (searchingPlayer == null) { throw new PlayerDoesNotExistException("Such a player does not exist"); ///// ''''nie ma takiego zawodniaka''''' } var additionalInfo = context.PlayerTeam.FirstOrDefault(e => e.IdPlayer == searchingPlayer.IdPlayer); //var playerToAdd = new PlayerRequest() //{ // FirstName = searchingPlayer.FirstName, // LastName = searchingPlayer.LastName, // DateOfBirth = searchingPlayer.DateOfBirth, // NumOfShirt = additionalInfo.NumOnShirt, // Comment = additionalInfo.Comment //}; var team = context.Team.FirstOrDefault(e => e.IdTeam == id); DateTime zeroTime = new DateTime(1, 1, 1); TimeSpan timeSpan = request.DateOfBirth - DateTime.Now; int years = (zeroTime + timeSpan).Year - 1; if (years > team.MaxAge) { throw new TooOldException("Player is too old to play for that team"); ////'''za stary' } var result = new PlayerTeam() { IdPlayer = searchingPlayer.IdPlayer, IdTeam = id, NumOnShirt = request.NumOfShirt, Comment = request.Comment }; context.PlayerTeam.Add(result); context.SaveChanges(); }
public EnrollStudentResult EnrollStudent(EnrollStudentRequest request) { EnrollStudentResult result = new EnrollStudentResult(); if (string.IsNullOrWhiteSpace(request.FirstName) || string.IsNullOrWhiteSpace(request.LastName) || string.IsNullOrWhiteSpace(request.IndexNumber) || string.IsNullOrWhiteSpace(request.BirthDate) || string.IsNullOrWhiteSpace(request.Studies) || !DateTime.TryParse(request.BirthDate, out DateTime birthDate)) { result.ResultCode = ResultCodes.NieWpisanoWszystkichDanychStudenta; return(result); } var studies = context.Studies.FirstOrDefault(e => e.Name == request.Studies); context.SaveChanges(); if (studies == null) { result.ResultCode = ResultCodes.NieIstniejaStudia; return(result); } int idStudy = studies.IdStudy; var bigSelect = context.Student.Any(x => x.IndexNumber == "" && x.IdEnrollmentNavigation.Semester == 1); if (bigSelect) { result.ResultCode = ResultCodes.StudentJestJuzZapisanyNaSemest; return(result); } var maxSelect = context.Enrollment.Max(e => e.IdEnrollment); int maxId = maxSelect + 1; DateTime startDate = DateTime.Now; context.Enrollment.Add(new Enrollment { IdEnrollment = maxId, Semester = 1, IdStudy = idStudy, StartDate = startDate, }); var nameSelect = context.Student.Any(e => e.IndexNumber == request.IndexNumber); if (nameSelect) { result.ResultCode = ResultCodes.StudentJuzIstnieje; return(result); } DateTime date = DateTime.Parse(request.BirthDate); context.Student.Add(new Student { IndexNumber = request.IndexNumber, FirstName = request.FirstName, LastName = request.LastName, BirthDate = date, IdEnrollment = maxId }); var response = new EnrollStudentResponse { IdEnrollment = maxId, IdStudy = idStudy, Semester = 1, StartDate = startDate }; result.ResultCode = ResultCodes.StudentDodany; result.Response = response; return(result); }