-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
213 lines (162 loc) · 6.61 KB
/
database.py
File metadata and controls
213 lines (162 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Randevu Sistemi - Database Module (Supabase Sync)
Cloud PostgreSQL with polling-based updates (PyQt6 compatible)
"""
from dataclasses import dataclass
from datetime import datetime
from typing import List, Optional
from enum import Enum
from supabase import create_client, Client
from config import AppConfig
class AppointmentStatus(Enum):
PENDING = "Beklemede"
ACCEPTED = "Kabul Edildi"
REJECTED = "Reddedildi"
@dataclass
class Appointment:
id: int
queue_number: int
note: str
status: AppointmentStatus
created_at: datetime
updated_at: datetime
@classmethod
def from_supabase(cls, data: dict) -> "Appointment":
"""Create Appointment from Supabase row data"""
created_at = data["created_at"]
updated_at = data["updated_at"]
# Handle ISO format with or without Z
if isinstance(created_at, str):
created_at = created_at.replace("Z", "+00:00")
if isinstance(updated_at, str):
updated_at = updated_at.replace("Z", "+00:00")
return cls(
id=data["id"],
queue_number=data["queue_number"],
note=data.get("note", ""),
status=AppointmentStatus(data["status"]),
created_at=datetime.fromisoformat(created_at),
updated_at=datetime.fromisoformat(updated_at)
)
class DatabaseManager:
"""Sync Supabase database manager (PyQt6 compatible)"""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self._initialized = True
try:
self.client: Client = create_client(AppConfig.SUPABASE_URL, AppConfig.SUPABASE_KEY)
except Exception as e:
print(f"Supabase connection error: {e}")
self.client = None
def is_connected(self) -> bool:
"""Check if database is connected"""
return self.client is not None
def get_next_queue_number(self) -> int:
"""Get next available queue number for today"""
if not self.client:
return 1
today = datetime.now().strftime("%Y-%m-%d")
result = self.client.table("appointments") \
.select("queue_number") \
.gte("created_at", f"{today}T00:00:00") \
.order("queue_number", desc=True) \
.limit(1) \
.execute()
if result.data:
return result.data[0]["queue_number"] + 1
return 1
def create_appointment(self, note: str = "") -> Optional[Appointment]:
"""Create a new appointment and return it"""
if not self.client:
return None
queue_number = self.get_next_queue_number()
data = {
"queue_number": queue_number,
"note": note,
"status": AppointmentStatus.PENDING.value
}
result = self.client.table("appointments").insert(data).execute()
if result.data:
return Appointment.from_supabase(result.data[0])
return None
def get_appointment_by_id(self, appointment_id: int) -> Optional[Appointment]:
"""Get appointment by ID"""
if not self.client:
return None
result = self.client.table("appointments") \
.select("*") \
.eq("id", appointment_id) \
.execute()
if result.data:
return Appointment.from_supabase(result.data[0])
return None
def get_all_appointments(self, status: Optional[AppointmentStatus] = None, descending: bool = True) -> List[Appointment]:
"""Get all appointments, optionally filtered by status and sorted"""
if not self.client:
return []
query = self.client.table("appointments").select("*")
if status:
query = query.eq("status", status.value)
result = query.order("created_at", desc=descending).execute()
return [Appointment.from_supabase(row) for row in result.data]
def get_pending_appointments(self) -> List[Appointment]:
"""Get pending appointments"""
return self.get_all_appointments(AppointmentStatus.PENDING)
def update_appointment_status(self, appointment_id: int, status: AppointmentStatus) -> bool:
"""Update appointment status"""
if not self.client:
return False
result = self.client.table("appointments") \
.update({"status": status.value}) \
.eq("id", appointment_id) \
.execute()
return len(result.data) > 0
def delete_appointment(self, appointment_id: int) -> bool:
"""Delete appointment by ID"""
if not self.client:
return False
result = self.client.table("appointments") \
.delete() \
.eq("id", appointment_id) \
.execute()
return len(result.data) > 0
def get_statistics(self) -> dict:
"""Get appointment statistics"""
if not self.client:
return {"total": 0, "pending": 0, "accepted": 0, "rejected": 0}
result = self.client.table("appointments") \
.select("status") \
.execute()
stats = {"total": 0, "pending": 0, "accepted": 0, "rejected": 0}
for row in result.data:
stats["total"] += 1
status = row["status"]
if status == AppointmentStatus.PENDING.value:
stats["pending"] += 1
elif status == AppointmentStatus.ACCEPTED.value:
stats["accepted"] += 1
elif status == AppointmentStatus.REJECTED.value:
stats["rejected"] += 1
return stats
def get_today_appointments(self) -> List[Appointment]:
"""Get appointments for today"""
if not self.client:
return []
today = datetime.now().strftime("%Y-%m-%d")
result = self.client.table("appointments") \
.select("*") \
.gte("created_at", f"{today}T00:00:00") \
.order("created_at", desc=True) \
.execute()
return [Appointment.from_supabase(row) for row in result.data]
# Global instance
def get_db() -> DatabaseManager:
"""Get the global database manager instance"""
return DatabaseManager()