rakib72642 commited on
Commit
1ddf755
·
1 Parent(s): f84481c

added new session new user

Browse files
Files changed (2) hide show
  1. db_view/db.html +206 -0
  2. db_view/dbapi.py +118 -0
db_view/db.html ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Doctor & Patient Management</title>
7
+ <style>
8
+ body {
9
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
10
+ margin: 0;
11
+ padding: 20px;
12
+ background-color: #f8f9fa;
13
+ }
14
+ h1 {
15
+ text-align: center;
16
+ color: #2c3e50;
17
+ }
18
+ .tabs {
19
+ display: flex;
20
+ justify-content: center;
21
+ margin: 20px 0;
22
+ border-bottom: 2px solid #ddd;
23
+ }
24
+ .tab-button {
25
+ padding: 12px 30px;
26
+ font-size: 16px;
27
+ cursor: pointer;
28
+ background: none;
29
+ border: none;
30
+ border-bottom: 3px solid transparent;
31
+ margin: 0 10px;
32
+ transition: all 0.3s;
33
+ }
34
+ .tab-button.active {
35
+ border-bottom: 3px solid #3498db;
36
+ color: #3498db;
37
+ font-weight: bold;
38
+ }
39
+ .table-container {
40
+ max-width: 1200px;
41
+ margin: 20px auto;
42
+ background: white;
43
+ padding: 20px;
44
+ border-radius: 8px;
45
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
46
+ }
47
+ table {
48
+ width: 100%;
49
+ border-collapse: collapse;
50
+ }
51
+ th, td {
52
+ padding: 12px;
53
+ text-align: left;
54
+ border-bottom: 1px solid #ddd;
55
+ }
56
+ th {
57
+ background-color: #3498db;
58
+ color: white;
59
+ }
60
+ tr:hover {
61
+ background-color: #f1f1f1;
62
+ }
63
+ .loading {
64
+ text-align: center;
65
+ padding: 20px;
66
+ color: #666;
67
+ }
68
+ .error {
69
+ color: red;
70
+ text-align: center;
71
+ }
72
+ </style>
73
+ </head>
74
+ <body>
75
+
76
+ <h1>Doctor & Patient Management System</h1>
77
+
78
+ <div class="tabs">
79
+ <button class="tab-button active" onclick="showTab(0)">Doctors</button>
80
+ <button class="tab-button" onclick="showTab(1)">Patients</button>
81
+ </div>
82
+
83
+ <!-- Doctors Tab -->
84
+ <div id="doctors-tab" class="table-container">
85
+ <h2>Doctors List</h2>
86
+ <div id="doctors-loading" class="loading">Loading doctors...</div>
87
+ <table id="doctors-table" style="display:none;">
88
+ <thead>
89
+ <tr>
90
+ <th>ID</th>
91
+ <th>Doctor Name</th>
92
+ <th>Category</th>
93
+ <th>Visiting Days</th>
94
+ <th>Visiting Time</th>
95
+ <th>Fee (BDT)</th>
96
+ </tr>
97
+ </thead>
98
+ <tbody></tbody>
99
+ </table>
100
+ </div>
101
+
102
+ <!-- Patients Tab -->
103
+ <div id="patients-tab" class="table-container" style="display:none;">
104
+ <h2>Patients List</h2>
105
+ <div id="patients-loading" class="loading">Loading patients...</div>
106
+ <table id="patients-table" style="display:none;">
107
+ <thead>
108
+ <tr>
109
+ <th>ID</th>
110
+ <th>Patient Name</th>
111
+ <th>Age</th>
112
+ <th>Phone</th>
113
+ <th>Email</th>
114
+ <th>Doctor</th>
115
+ <th>Category</th>
116
+ <th>Visiting Date</th>
117
+ </tr>
118
+ </thead>
119
+ <tbody></tbody>
120
+ </table>
121
+ </div>
122
+
123
+ <script>
124
+ const API_BASE = "http://127.0.0.1:8000";
125
+
126
+ function showTab(tabIndex) {
127
+ const doctorsTab = document.getElementById('doctors-tab');
128
+ const patientsTab = document.getElementById('patients-tab');
129
+ const buttons = document.querySelectorAll('.tab-button');
130
+
131
+ buttons.forEach((btn, index) => {
132
+ btn.classList.toggle('active', index === tabIndex);
133
+ });
134
+
135
+ doctorsTab.style.display = tabIndex === 0 ? 'block' : 'none';
136
+ patientsTab.style.display = tabIndex === 1 ? 'block' : 'none';
137
+ }
138
+
139
+ async function fetchDoctors() {
140
+ const loading = document.getElementById('doctors-loading');
141
+ const table = document.getElementById('doctors-table');
142
+ const tbody = table.querySelector('tbody');
143
+
144
+ try {
145
+ const res = await fetch(`${API_BASE}/api/doctors`);
146
+ if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);
147
+
148
+ const doctors = await res.json();
149
+
150
+ tbody.innerHTML = doctors.map(doc => `
151
+ <tr>
152
+ <td>${doc.id}</td>
153
+ <td>${doc.doctor_name}</td>
154
+ <td>${doc.category}</td>
155
+ <td>${doc.visiting_days}</td>
156
+ <td>${doc.visiting_time}</td>
157
+ <td>${doc.visiting_money}</td>
158
+ </tr>`).join('');
159
+
160
+ loading.style.display = 'none';
161
+ table.style.display = 'table';
162
+ } catch (err) {
163
+ console.error(err);
164
+ loading.innerHTML = `<span class="error">Failed to fetch doctors.<br>${err.message}</span>`;
165
+ }
166
+ }
167
+
168
+ async function fetchPatients() {
169
+ const loading = document.getElementById('patients-loading');
170
+ const table = document.getElementById('patients-table');
171
+ const tbody = table.querySelector('tbody');
172
+
173
+ try {
174
+ const res = await fetch(`${API_BASE}/api/patients`);
175
+ if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);
176
+
177
+ const patients = await res.json();
178
+
179
+ tbody.innerHTML = patients.map(patient => `
180
+ <tr>
181
+ <td>${patient.id}</td>
182
+ <td>${patient.patient_name}</td>
183
+ <td>${patient.patient_age}</td>
184
+ <td>${patient.patient_num}</td>
185
+ <td>${patient.patient_mail}</td>
186
+ <td>${patient.doctor_name}</td>
187
+ <td>${patient.doctor_category}</td>
188
+ <td>${patient.visiting_date}</td>
189
+ </tr>`).join('');
190
+
191
+ loading.style.display = 'none';
192
+ table.style.display = 'table';
193
+ } catch (err) {
194
+ console.error(err);
195
+ loading.innerHTML = `<span class="error">Failed to fetch patients.<br>${err.message}</span>`;
196
+ }
197
+ }
198
+
199
+ // Load data when page loads
200
+ window.onload = () => {
201
+ fetchDoctors();
202
+ fetchPatients(); // Preload both for better UX
203
+ };
204
+ </script>
205
+ </body>
206
+ </html>
db_view/dbapi.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import aiosqlite, uvicorn, os
4
+ from typing import List, Optional
5
+
6
+
7
+ from fastapi import FastAPI, HTTPException
8
+ from fastapi.middleware.cors import CORSMiddleware # ← Add this line
9
+ from pydantic import BaseModel
10
+ import aiosqlite
11
+ import uvicorn
12
+ import os
13
+ from typing import List
14
+
15
+ app = FastAPI(title="Doctor-Patient API")
16
+
17
+ # ====================== ADD THIS BLOCK ======================
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+ # ===========================================================
26
+
27
+ # Database connection
28
+ DB_PATH = os.path.join(os.getcwd(), "core/daa.db")
29
+
30
+ # Pydantic Models for Response
31
+ class Doctor(BaseModel):
32
+ id: int
33
+ doctor_name: str
34
+ category: str
35
+ visiting_days: str
36
+ visiting_time: str
37
+ visiting_money: int
38
+
39
+ class Patient(BaseModel):
40
+ id: int
41
+ doctor_name: str
42
+ doctor_category: str
43
+ patient_name: str
44
+ patient_age: str
45
+ patient_num: str
46
+ visiting_date: str
47
+ patient_mail: str
48
+
49
+ class AllDataResponse(BaseModel):
50
+ doctors: List[Doctor]
51
+ patients: List[Patient]
52
+
53
+
54
+ async def get_db():
55
+ """Get database connection"""
56
+ conn = await aiosqlite.connect(DB_PATH)
57
+ conn.row_factory = aiosqlite.Row # Return rows as dictionaries
58
+ return conn
59
+
60
+
61
+ @app.get("/api/all-data", response_model=AllDataResponse)
62
+ async def get_all_data():
63
+ """Fetch all doctors and patients data"""
64
+ conn = None
65
+ try:
66
+ conn = await get_db()
67
+
68
+ # Fetch all doctors
69
+ async with conn.execute("SELECT * FROM doctors") as cursor:
70
+ doctors_rows = await cursor.fetchall()
71
+ doctors = [dict(row) for row in doctors_rows]
72
+
73
+ # Fetch all patients
74
+ async with conn.execute("SELECT * FROM patients") as cursor:
75
+ patients_rows = await cursor.fetchall()
76
+ patients = [dict(row) for row in patients_rows]
77
+
78
+ return AllDataResponse(
79
+ doctors=doctors,
80
+ patients=patients
81
+ )
82
+
83
+ except Exception as e:
84
+ raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
85
+ finally:
86
+ if conn:
87
+ await conn.close()
88
+
89
+
90
+ # Optional: Separate endpoints
91
+ @app.get("/api/doctors", response_model=List[Doctor])
92
+ async def get_all_doctors():
93
+ """Get all doctors only"""
94
+ conn = None
95
+ try:
96
+ conn = await get_db()
97
+ async with conn.execute("SELECT * FROM doctors") as cursor:
98
+ rows = await cursor.fetchall()
99
+ return [dict(row) for row in rows]
100
+ finally:
101
+ if conn:
102
+ await conn.close()
103
+
104
+
105
+ @app.get("/api/patients", response_model=List[Patient])
106
+ async def get_all_patients():
107
+ """Get all patients only"""
108
+ conn = None
109
+ try:
110
+ conn = await get_db()
111
+ async with conn.execute("SELECT * FROM patients") as cursor:
112
+ rows = await cursor.fetchall()
113
+ return [dict(row) for row in rows]
114
+ finally:
115
+ if conn:
116
+ await conn.close()
117
+ if __name__ == "__main__":
118
+ uvicorn.run("dbapi:app", host="127.0.0.1", port=8000, reload=True)