Ejemplo n.º 1
0
        //TODO improve performance
        public List <Patient> PatientsWithSimilarDisease(int patientId)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(_entityTypePatient, _context, patientId);

            var patientRecord = _context.Records.Where(p => p.PatientId == patientId).ToList();
            var patientsWithSimilarDiseasesList = new List <Patient>()
            {
            };

            if (patientRecord.Count > 0)
            {
                var DbF = Microsoft.EntityFrameworkCore.EF.Functions;
                foreach (var item in patientRecord)
                {
                    var patientIds = _context.Records.AsEnumerable <Record>()
                                     .Where(rec => rec.DiseaseName.Contains(item.DiseaseName, StringComparison.CurrentCultureIgnoreCase) && rec.PatientId != patientId)
                                     .Select(r => r.PatientId).ToArray();

                    var patientsWithThisDieseas = _context.Patients.AsEnumerable().Where(t => patientIds.Contains(t.Id)).ToList();

                    patientsWithSimilarDiseasesList.AddRange(patientsWithThisDieseas);
                }
            }
            return(patientsWithSimilarDiseasesList.Distinct().ToList());
        }
Ejemplo n.º 2
0
        public async Task <PatientDoctor> AddPatientDoctor(int doctorId, int patientId)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctorId);

            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.patinet, _context, patientId);


            if (await GetPatientDoctor(doctorId, patientId) != null)
            {
                throw new BadRequestException("This doctor is already assigned for this patient");
            }

            var doctor = await _doctorRepo.GetDoctor(doctorId);

            var patient = await _patientRepo.GetPatient(patientId);


            var patientDoctor = new PatientDoctor()
            {
                DoctorId = doctorId, PatientId = patientId, Doctor = doctor, Patient = patient
            };

            var newPatientDoctor = await _context.PatientDoctors.AddAsync(patientDoctor);

            await _context.SaveChangesAsync();

            return(newPatientDoctor.Entity);
        }
Ejemplo n.º 3
0
        public async Task <Doctor> EditDoctor(Doctor doctor)
        {
            if (doctor.OfficialId == null)
            {
                throw new InternalServerException("OfficialId is null");
            }

            var doctorToBeUpdated = await GetDoctor(doctor.Id);


            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctor.Id);

            _context.Entry(doctorToBeUpdated).CurrentValues.SetValues(doctor);

            try
            {
                await _context.SaveChangesAsync();

                return(doctor);
            }
            catch (System.Exception)
            {
                throw new Exception("Could not save");
            }
        }
Ejemplo n.º 4
0
        public async Task <Doctor> AddDoctor(Doctor doctor)
        {
            ExceptionThrowers.ThrowErrorIfEntiryExist(_entityTypeDoctor, _context, doctor.OfficialId);

            var newDoctor = await _context.Doctors.AddAsync(doctor);

            await _context.SaveChangesAsync();

            return(newDoctor.Entity);
        }
Ejemplo n.º 5
0
        public async Task <Patient> DeletePatient(Patient patient)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(_entityTypePatient, _context, patient.Id);


            patient.Archived     = true;
            patient.ArchivedDate = DateTime.Now;

            await _context.SaveChangesAsync();

            return(patient);
        }
Ejemplo n.º 6
0
        public async Task <Doctor> DeleteDoctor(int id)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(_entityTypeDoctor, _context, id);

            var doctor = await GetDoctor(id);

            doctor.Archived     = true;
            doctor.ArchivedDate = DateTime.Now;


            await _context.SaveChangesAsync();

            return(doctor);
        }
Ejemplo n.º 7
0
        public async Task <PagedList <PatientsForListDto> > GetUnAssignedPatientsToDoctor(int doctorId, PaginationParams paginationParams)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctorId);


            var unAssignedPatients = _context.Patients.Where(P => !P.PatientDoctors.Any(pd => pd.DoctorId == doctorId && pd.Archived == false));

            var pagedList = await PagedList <Patient?> .CreatePagedAsync(unAssignedPatients, paginationParams.PageNumber, paginationParams.PageSize);

            var pagedPatientsList = PagedListConverter <Patient?, PatientsForListDto> .Convert(pagedList, in _mapper);


            return(pagedPatientsList);
        }
Ejemplo n.º 8
0
        public async Task <List <PatientDoctor> > GetAssignedDoctorsToPatientId(int patientId, bool?withPatientRecords)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.patinet, _context, patientId);

            var patientsForThisDoctor = _context.PatientDoctors.Where(d => d.PatientId == patientId)
                                        .Include(d => d.Doctor)
                                        .Include(p => p.Patient);

            if (withPatientRecords == true)
            {
                patientsForThisDoctor.Include(p => p.Patient.Records);
            }

            return(await patientsForThisDoctor.ToListAsync());
        }
Ejemplo n.º 9
0
        public Patient AddPatient(Patient patient)
        {
            ExceptionThrowers.ThrowErrorIfEntiryExist(_entityTypePatient, _context, patient.OfficialId);

            patient.Created = DateTime.Now;
            // TODO Test this
            patient.Records = new Collection <Record>()
            {
            };
            var patientToBeCreated = _context.Patients.Add(patient).Entity;

            _context.SaveChanges();

            return(patientToBeCreated);
        }
Ejemplo n.º 10
0
        public async Task <Doctor> GetDoctor(int id)
        {
            ExceptionThrowers.ThrowErrorIfNotValidId(id);

            var doctor = await _context.Doctors.Include(d => d.PatientDoctors)
                         .ThenInclude(d => d.Patient)
                         .FirstOrDefaultAsync(d => d.Id == id);

            if (doctor == null)
            {
                throw new NotFoundException("Doctor not found");
            }


            return(doctor);
        }
Ejemplo n.º 11
0
        public async Task <PagedList <PatientWithAssignedDoctorsDto> > GetAssignedPatientsToDoctorId(int doctorId, PaginationParams paginationParams)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctorId);

            var patientsForThisDoctor = _context.PatientDoctors
                                        .Where(d => d.DoctorId == doctorId && d.Archived == false)
                                        .Select(d => d.Patient)
                                        .IncludeFilter(x => x.PatientDoctors.Where(pd => pd.Archived == false))
                                        .Include(i => i.Records);

            var pagedList = await PagedList <Patient> .CreatePagedAsync(patientsForThisDoctor, paginationParams.PageNumber, paginationParams.PageSize);


            var pagedPatientsList = PagedListConverter <Patient, PatientWithAssignedDoctorsDto> .Convert(pagedList, in _mapper);


            return(pagedPatientsList);
        }
Ejemplo n.º 12
0
        public async Task <PatientDoctor> GetPatientDoctor(int doctorId, int patientId, bool?withPatientRecords = false)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctorId);

            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.patinet, _context, patientId);


            var patientDoctor = _context.PatientDoctors
                                .Where(p => p.PatientId == patientId && p.DoctorId == doctorId && p.Archived == false)
                                .Include(p => p.Patient);


            if (withPatientRecords == true)
            {
                patientDoctor.Include(p => p.Patient.Records);
            }

            return(await patientDoctor.FirstOrDefaultAsync());
        }
Ejemplo n.º 13
0
        public async Task <Patient> EditPatient(Patient patient)

        {
            if (patient.OfficialId == null)
            {
                throw new InternalServerException("OfficialId is null");
            }

            ExceptionThrowers.ThrowErrorIfEntityNotExist(_entityTypePatient, _context, patient.Id);

            var patientToBeUpdated = await _context.Patients.FirstOrDefaultAsync(p => p.Id == patient.Id);

            _context.Attach(patientToBeUpdated).CurrentValues.SetValues(patient);


            await _context.SaveChangesAsync();

            return(patient);
        }
Ejemplo n.º 14
0
        public async Task <Patient> GetPatient(int id, bool?withRecords = false)
        {
            ExceptionThrowers.ThrowErrorIfNotValidId(id);

            Patient patient;

            if (withRecords == true)
            {
                patient = await _context.Patients.Include(p => p.Records).FirstOrDefaultAsync(p => p.Id == id);
            }
            else
            {
                patient = await _context.Patients.FindAsync(id);
            }

            if (patient == null)
            {
                throw new BadRequestException("Patient not found");
            }


            return(patient);
        }
Ejemplo n.º 15
0
        public async Task <PatientDoctor> RemovePatientDoctor(int doctorId, int patientId)
        {
            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.doctor, _context, doctorId);

            ExceptionThrowers.ThrowErrorIfEntityNotExist(EntityType.patinet, _context, patientId);


            var patientDoctor = await GetPatientDoctor(doctorId, patientId, false);


            if (patientDoctor == null || patientDoctor.Archived == true)
            {
                throw new BadRequestException("There is no assigned relatoinship");
            }



            patientDoctor.Archived     = true;
            patientDoctor.ArchivedDate = DateTime.Now;

            await _context.SaveChangesAsync();

            return(patientDoctor);
        }