Update database.py
Browse files- 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 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|