diff --git a/lib/cli.py b/lib/cli.py index edd1b62ad..5a2141e6d 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -2,7 +2,19 @@ from helpers import ( exit_program, - helper_1 + list_patients, + find_patient_by_user_input, + find_patient_by_id, + create_patient, + update_patient, + delete_patient, + list_doctors, + find_doctor_by_name, + find_doctor_by_id, + create_doctor, + update_doctor, + delete_doctor, + list_patient_doctors ) @@ -13,16 +25,103 @@ def main(): if choice == "0": exit_program() elif choice == "1": - helper_1() + patients() + elif choice == "2": + doctors() + elif choice == "3": + patient_doctors() else: print("Invalid choice") def menu(): + print("Patient and Doctor Management System") print("Please select an option:") print("0. Exit the program") - print("1. Some useful function") + print("1. Patients") + print("2. Doctors") + print("3. Patient-Doctor Associations") +def patients(): + while True: + patient_options() + choice = input("> ") + if choice == "0": + exit_program() + elif choice == "1": + list_patients() + elif choice == "2": + find_patient_by_user_input() + elif choice == "3": + find_patient_by_id() + elif choice == "4": + create_patient() + elif choice == "5": + update_patient() + elif choice == "6": + delete_patient() + else: + print("Invalid choice") + +def patient_options(): + print("Patient Options:") + print("0. Back to main menu") + print("1. List all patients") + print("2. Find patient by name") + print("3. Find patient by ID") + print("4. Create new patient") + print("5. Update patient") + print("6. Delete patient") + print("Please select an option:") + +def doctors(): + while True: + doctor_options() + choice = input("> ") + if choice == "0": + exit_program() + elif choice == "1": + list_doctors() + elif choice == "2": + find_doctor_by_name() + elif choice == "3": + find_doctor_by_id() + elif choice == "4": + create_doctor() + elif choice == "5": + update_doctor() + elif choice == "6": + delete_doctor() + else: + print("Invalid choice") + +def doctor_options(): + print("Doctor Options:") + print("0. Back to main menu") + print("1. List all doctors") + print("2. Find doctor by name") + print("3. Find doctor by ID") + print("4. Create new doctor") + print("5. Update doctor") + print("6. Delete doctor") + print("Please select an option:") + +def patient_doctors(): + while True: + patient_doctor_options() + choice = input("> ") + if choice == "0": + exit_program() + elif choice == "1": + list_patient_doctors() + else: + print("Invalid choice") + +def patient_doctor_options(): + print("Patient and Doctor Options:") + print("0. Back to main menu") + print("1. List patient doctors") + print("Please select an option:") if __name__ == "__main__": main() diff --git a/lib/debug.py b/lib/debug.py index 6fadf6e66..665d36b8d 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -2,7 +2,22 @@ # lib/debug.py from models.__init__ import CONN, CURSOR +from models.doctor import Doctor +from models.patient import Patient + import ipdb +Doctor.drop_table() +Doctor.create_table() +Patient.drop_table() +Patient.create_table() + +Patient.create_individual_patient("Joel Smith", "male", '123-45-6789', 23, "Queens NY") +Patient.create_individual_patient("Jane Doe", "female", '987-65-4321', 25, "Brentwood NY") +Patient.create_individual_patient("John Doe", "male", '111-11-1111', 30, "Flushing NY") + +Doctor.create_individual_doctor("Dr. John", "Cardiologist", 1) +Doctor.create_individual_doctor("Dr. Jane", "Dermatologist", 2) +Doctor.create_individual_doctor("Dr. Bob", "Neurologist", 3) ipdb.set_trace() diff --git a/lib/helpers.py b/lib/helpers.py index f10df049c..29bf75cf3 100644 --- a/lib/helpers.py +++ b/lib/helpers.py @@ -1,9 +1,149 @@ # lib/helpers.py +from models.patient import Patient +from models.doctor import Doctor -def helper_1(): - print("Performing useful function#1.") +# def helper_1(): +# print("Performing useful function#1.") def exit_program(): print("Goodbye!") exit() + +def list_patients(): + patients = Patient.get_all() + print (patients) + for patient in patients: + print(patient) + +def find_patient_by_user_input(): + name = input("Enter patient name: ") + patient = Patient.find_by_name(name) + print(patient) if patient else print(f'Patient {name} not found') + +def find_patient_by_id(): + patient_id = input("Enter patient ID: ") + patient = Patient.find_by_id(patient_id) + print(patient) if patient else print(f'Patient with ID {patient_id} not found') + +def create_patient(): + name = input("Enter patient name: ") + gender = input("Enter patient gender: ") + ssn = input("Enter patient ssn: ") + age = input("Enter patient age: ") + address = input("Enter patient address: ") + try: + patient = Patient.create_individual_patient(name, gender, ssn, age, address) + print(f'Success: {patient}') + + except Exception as exc: + print(f'Error creating patient:', exc) + +def update_patient(): + patient_id = input("Enter patient ID: ") + patient = Patient.find_by_id(patient_id) + if patient: + try: + name = input("Enter new patient name: ") + patient.name = name + gender = input("Enter new patient gender: ") + patient.gender = gender + ssn = input("Enter new patient ssn: ") + patient.ssn = ssn + age = input("Enter new patient age: ") + patient.age = age + address = input("Enter new patient address: ") + patient.address = address + patient.update() + print(f'Success: {patient_id}') + except Exception as exc: + print(f'Error updating patient:', exc) + else: + print(f'Patient with ID {patient_id} not found') + +def delete_patient(): + patient_id = input("Enter patient ID: ") + patient = Patient.find_by_id(patient_id) + if patient: + patient.delete() + print(f'Patient with ID {patient_id} deleted') + else: + print(f'Patient with ID {patient_id} not found') + + +def list_doctors(): + doctors = Doctor.get_all_doctor() + print (doctors) + for doctor in doctors: + print(f'ID: {doctor.id}, Name: {doctor.name}, Specialty: {doctor.specialization}') + # for doctor in doctors: + # print(doctor) + +def find_doctor_by_name(): + name = input("Enter doctor name: ") + doctor = Doctor.find_by_name(name) + print(doctor) if doctor else print(f'Doctor {name} not found') + +def find_doctor_by_id(): + doctor_id = input("Enter doctor ID: ") + doctor = Doctor.find_by_id(doctor_id) + print(doctor) if doctor else print(f'Doctor with ID {doctor_id} not found') + +def create_doctor(): + name = input("Enter the doctor's name: ") + specialization = input("Enter the doctor's specialization: ") + patient_id = input("Enters the patient_id: ") + try: + doctor = Doctor.create_individual_doctor(name, specialization, int(patient_id)) + print(f'Success: {doctor}') + except Exception as exc: + print(f'Error creating doctor:', exc) + +def update_doctor(): + doctor_id = input("Enter doctor ID: ") + doctor = Doctor.find_by_id(doctor_id) + if doctor: + try: + name = input("Enter new doctor name: ") + doctor.name = name + specialization = input("Enter new doctor specialization: ") + doctor.specialization = specialization + patient_id = input("Enter new patient id: ") + doctor.patient_id = int(patient_id) + + doctor.update() + print(f'Success {doctor}') + except Exception as exc: + print(f'Error updating doctor:', exc) + else: + print(f'Doctor with ID {doctor_id} not found') + +def delete_doctor(): + doctor_id = input("Enter doctor ID: ") + doctor = Doctor.find_by_id(doctor_id) + if doctor: + doctor.delete() + print(f'Doctor with ID {doctor_id} deleted') + else: + print(f'Doctor with ID {doctor_id} not found') + +def list_patient_doctors(): + id_ = input("Enter patient ID: ") + patient = Patient.find_by_id(id_) + if patient: + try: + for doctor in patient.doctors(): + print(doctor) + except Exception as exc: + print("Error listing doctors", exc) + else : + print(f"Can not find patient by id of {id_}") + + + + + + + + + diff --git a/lib/models/__init__.py b/lib/models/__init__.py index d5b061e1e..7410fc36d 100644 --- a/lib/models/__init__.py +++ b/lib/models/__init__.py @@ -1,4 +1,4 @@ import sqlite3 -CONN = sqlite3.connect('company.db') +CONN = sqlite3.connect('medical.db') CURSOR = CONN.cursor() diff --git a/lib/models/doctor.py b/lib/models/doctor.py new file mode 100644 index 000000000..bf56d6450 --- /dev/null +++ b/lib/models/doctor.py @@ -0,0 +1,150 @@ +# from models.patient import Patient +from models.__init__ import CURSOR, CONN + +class Doctor: + all = {} + specializations = ['Cardiologist', 'Neurologist', 'Pediatrician', 'Dermatologist', 'Radiologist'] + + def __init__(self, name, specialization, patient_id, id=None): + self.name = name + self.specialization = specialization + self.patient_id = patient_id + self.id = id + Doctor.all[id] = self + + def __repr__(self): + return f'' + + @property + def patient_id(self): + return self._patient_id + + @patient_id.setter + def patient_id(self, patient_id): + if isinstance(patient_id, int): + self._patient_id = patient_id + else: + raise TypeError("Patient ID must be an integer") + + @property + def specialization(self): + return self._specialization + + @specialization.setter + def specialization(self, new_specialization): + if new_specialization in Doctor.specializations: + self._specialization = new_specialization + +# Create a table for doctor + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS doctors ( + id INTEGER PRIMARY KEY, + name TEXT, + specialization TEXT, + patient_id INTEGER + ) + """ + CURSOR.execute(sql) + CONN.commit() + +# save db to table + # def save(self): + # sql = """ + # INSERT INTO patients (name, specialization, patient_id) + # VALUES (?, ?, ?) + # """ + # CURSOR.execute(sql,(self.name, self.specialization, self.patient_id)) + # CONN.commit() + # self.id = CURSOR.lastrowid + +# save a doctor to db + def save_doctor(self): + sql = """ + INSERT INTO doctors (name, specialization, patient_id) + VALUES (?, ?, ?) + """ + CURSOR.execute(sql, (self.name, self.specialization, self.patient_id)) + CONN.commit() + self.id = CURSOR.lastrowid + type(self).all[self.id] = self + +# create a new doctor + @classmethod + def create_individual_doctor(cls, name, specialization, patient_id): + new_doctor = cls(name=name, specialization=specialization, patient_id=patient_id) + new_doctor.save_doctor() + return new_doctor + + @classmethod + def instance_from_db(cls,row): + doctor = cls.all.get(row[0]) + if doctor: + doctor.name = row[1] + doctor.specialization = row[2] + doctor.patient_id = row[3] + else: + doctor = cls(name=row[1], specialization=row[2], patient_id=row[3]) + doctor.id = row[0] + cls.all[doctor.id] = doctor + return doctor + +#get all doctors from db + @classmethod + def get_all_doctor(cls): + sql = "SELECT * FROM doctors" + CURSOR.execute(sql) + rows = CURSOR.fetchall() + # for row in rows: + # cls.instance_from_db(row) + # return list(cls.all.values()) + rows = CURSOR.execute(sql).fetchall() + return [cls.instance_from_db(row) for row in rows] + + +# find doctor by name in db + @classmethod + def find_by_name(cls, name): + sql = """ + SELECT * FROM doctors + WHERE name = ? + """ + row = CURSOR.execute(sql, (name,)).fetchone() + return cls.instance_from_db(row) if row else None + +#find doctor by id in db + @classmethod + def find_by_id(cls, id): + sql = """ + SELECT * FROM doctors + WHERE id = ? + """ + row = CURSOR.execute(sql, (id,)).fetchone() + return cls.instance_from_db(row) if row else None + +# delete table from db + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS doctors" + CURSOR.execute(sql) + CONN.commit() + +# update doctor to db + def update(self): + sql = """ + UPDATE doctors + SET name = ?, specialization = ?, patient_id = ? + WHERE id = ? + """ + CURSOR.execute(sql, (self.name, self.specialization, self.patient_id, self.id)) + CONN.commit() + return self + +# delete dr from db + def delete(self): + sql = "DELETE FROM doctors WHERE id = ?" + CURSOR.execute(sql, (self.id,)) + CONN.commit() + return self + \ No newline at end of file diff --git a/lib/models/model_1.py b/lib/models/model_1.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/models/patient.py b/lib/models/patient.py new file mode 100644 index 000000000..14ef49075 --- /dev/null +++ b/lib/models/patient.py @@ -0,0 +1,150 @@ +from models.__init__ import CURSOR,CONN +from models.doctor import Doctor + + +class Patient: + all = {} + + def __init__(self, name, gender, ssn, age, address, id=None): + self.name = name + self.gender = gender + self.ssn = ssn + self.age = age + self.address = address + + def __repr__(self): + return f'' + + @property + def name(self): + return self._name + + @name.setter + def name(self, new_name): + if isinstance(new_name, str) and 0 < len(new_name) <= 20: + self._name = new_name + +# Create a patient table if it doesnt exist + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS patients ( + id INTEGER PRIMARY KEY, + name TEXT, + gender TEXT, + ssn INTEGER, + age INTEGER, + address TEXT + ) + """ + CURSOR.execute(sql) + CONN.commit() + +# Delete a patient table + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS patients" + CURSOR.execute(sql) + CONN.commit() + +# Save patient to the database + def save(self): + sql = """ + INSERT INTO patients (name,gender,ssn,age,address) + VALUES (?, ?, ?, ?, ?) + """ + CURSOR.execute(sql,(self.name, self.gender, self.ssn, self.age, self.address)) + CONN.commit() + self.id = CURSOR.lastrowid + type(self).all[self.id] = self + +# Create new patients into the database + @classmethod + def create_individual_patient(cls,name,gender,ssn,age,address): + new_patient = cls(name=name, gender=gender, ssn=ssn, age=age, address=address) + new_patient.save() + return new_patient + +# Delete patients from database + def delete(self): + sql = """ + DELETE FROM patients + WHERE id = ? + """ + CURSOR.execute(sql, (self.id,)) + CONN.commit() + self.id = None + # del type(self).all[self.id] + + @classmethod + def instance_from_db(cls,row): + patient = cls.all.get(row[0]) + if patient: + patient.name = row[1] + patient.gender = row[2] + patient.ssn = row[3] + patient.age = row[4] + patient.address = row[5] + else: + patient = cls(name=row[1], gender=row[2], ssn=row[3], age=row[4], address=row[5]) + patient.id = row[0] + cls.all[patient.id] = patient + return patient + + +# Get all patients from database + @classmethod + def get_all(cls): + sql = "SELECT * FROM patients" + rows = CURSOR.execute(sql).fetchall() + return [cls.instance_from_db(row) for row in rows] + +# Finds a patient in the database + @classmethod + def find_by_id(cls,id): + sql = """ + SELECT * FROM patients + WHERE id = ? + """ + row = CURSOR.execute(sql, (id,)).fetchone() + return cls.instance_from_db(row) if row else None + +# Finds patients by their name + @classmethod + def find_by_name(cls,name): + sql = """ + SELECT * FROM patients + WHERE name = ? + """ + rows = CURSOR.execute(sql, (name,)).fetchone() + return cls.instance_from_db(rows) + +# Update a patient + def update(self): + sql = """ + UPDATE patients + SET name = ?, gender = ?, ssn = ?, age = ?, address = ? + WHERE id = ? + """ + CURSOR.execute(sql, (self.name, self.gender, self.ssn, self.age, self.address, self.id)) + CONN.commit() + return self + +# list of doctors + def doctors(self): + breakpoint() + sql = """ + SELECT * FROM doctors + WHERE patient_id = ? + """ + rows = CURSOR.execute(sql, (self.id,)).fetchall() + breakpoint() + CONN.commit() + breakpoint() + return [Doctor.instance_from_db(row) for row in rows] + + + + + + \ No newline at end of file