ViannyCruz commited on
Commit
0b2b11a
·
verified ·
1 Parent(s): 15c658c

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +62 -42
database.py CHANGED
@@ -438,26 +438,36 @@ class DatabaseManager:
438
  conn.close()
439
 
440
  def get_patients(self, user_id: int, role: str) -> List[Dict]:
441
- """Admin ve todos; Doctor ve solo los suyos."""
442
- conn = self.get_connection()
443
- try:
444
- if role == "Admin":
445
- rows = conn.execute(
446
- """SELECT p.*, u.username as doctorName
447
- FROM Patients p JOIN Users u ON p.createdByUserID = u.userID
448
- ORDER BY p.creationDate DESC"""
449
- ).fetchall()
450
- else:
451
- rows = conn.execute(
452
- """SELECT p.*, u.username as doctorName
453
- FROM Patients p JOIN Users u ON p.createdByUserID = u.userID
454
- WHERE p.createdByUserID = ?
455
- ORDER BY p.creationDate DESC""",
456
- (user_id,)
457
- ).fetchall()
458
- return [self._serialize(r) for r in rows]
459
- finally:
460
- conn.close()
 
 
 
 
 
 
 
 
 
 
461
 
462
  def get_patient(self, patient_id: int) -> Optional[Dict]:
463
  conn = self.get_connection()
@@ -473,28 +483,38 @@ class DatabaseManager:
473
  conn.close()
474
 
475
  def search_patients(self, search_term: str, user_id: int, role: str) -> List[Dict]:
476
- conn = self.get_connection()
477
- try:
478
- like = f"%{search_term}%"
479
- if role == "Admin":
480
- rows = conn.execute(
481
- """SELECT p.*, u.username as doctorName
482
- FROM Patients p JOIN Users u ON p.createdByUserID = u.userID
483
- WHERE p.name LIKE ?
484
- ORDER BY p.name""",
485
- (like,)
486
- ).fetchall()
487
- else:
488
- rows = conn.execute(
489
- """SELECT p.*, u.username as doctorName
490
- FROM Patients p JOIN Users u ON p.createdByUserID = u.userID
491
- WHERE p.name LIKE ? AND p.createdByUserID = ?
492
- ORDER BY p.name""",
493
- (like, user_id)
494
- ).fetchall()
495
- return [self._serialize(r) for r in rows]
496
- finally:
497
- conn.close()
 
 
 
 
 
 
 
 
 
 
498
 
499
  def update_patient(self, patient_id: int, **kwargs) -> bool:
500
  conn = self.get_connection()
 
438
  conn.close()
439
 
440
  def get_patients(self, user_id: int, role: str) -> List[Dict]:
441
+ """Admin ve todos; Doctor ve solo los suyos."""
442
+ conn = self.get_connection()
443
+ try:
444
+ if role == "Admin":
445
+ rows = conn.execute(
446
+ """SELECT p.*, u.username as doctorName,
447
+ COUNT(c.consultationID) as consultationCount,
448
+ MAX(c.consultationDate) as lastConsultationDate
449
+ FROM Patients p
450
+ JOIN Users u ON p.createdByUserID = u.userID
451
+ LEFT JOIN Consultations c ON c.patientID = p.patientID
452
+ GROUP BY p.patientID
453
+ ORDER BY p.creationDate DESC"""
454
+ ).fetchall()
455
+ else:
456
+ rows = conn.execute(
457
+ """SELECT p.*, u.username as doctorName,
458
+ COUNT(c.consultationID) as consultationCount,
459
+ MAX(c.consultationDate) as lastConsultationDate
460
+ FROM Patients p
461
+ JOIN Users u ON p.createdByUserID = u.userID
462
+ LEFT JOIN Consultations c ON c.patientID = p.patientID
463
+ WHERE p.createdByUserID = ?
464
+ GROUP BY p.patientID
465
+ ORDER BY p.creationDate DESC""",
466
+ (user_id,)
467
+ ).fetchall()
468
+ return [self._serialize(r) for r in rows]
469
+ finally:
470
+ conn.close()
471
 
472
  def get_patient(self, patient_id: int) -> Optional[Dict]:
473
  conn = self.get_connection()
 
483
  conn.close()
484
 
485
  def search_patients(self, search_term: str, user_id: int, role: str) -> List[Dict]:
486
+ conn = self.get_connection()
487
+ try:
488
+ like = f"%{search_term}%"
489
+ if role == "Admin":
490
+ rows = conn.execute(
491
+ """SELECT p.*, u.username as doctorName,
492
+ COUNT(c.consultationID) as consultationCount,
493
+ MAX(c.consultationDate) as lastConsultationDate
494
+ FROM Patients p
495
+ JOIN Users u ON p.createdByUserID = u.userID
496
+ LEFT JOIN Consultations c ON c.patientID = p.patientID
497
+ WHERE p.name LIKE ?
498
+ GROUP BY p.patientID
499
+ ORDER BY p.name""",
500
+ (like,)
501
+ ).fetchall()
502
+ else:
503
+ rows = conn.execute(
504
+ """SELECT p.*, u.username as doctorName,
505
+ COUNT(c.consultationID) as consultationCount,
506
+ MAX(c.consultationDate) as lastConsultationDate
507
+ FROM Patients p
508
+ JOIN Users u ON p.createdByUserID = u.userID
509
+ LEFT JOIN Consultations c ON c.patientID = p.patientID
510
+ WHERE p.name LIKE ? AND p.createdByUserID = ?
511
+ GROUP BY p.patientID
512
+ ORDER BY p.name""",
513
+ (like, user_id)
514
+ ).fetchall()
515
+ return [self._serialize(r) for r in rows]
516
+ finally:
517
+ conn.close()
518
 
519
  def update_patient(self, patient_id: int, **kwargs) -> bool:
520
  conn = self.get_connection()