-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
226 lines (183 loc) · 8.45 KB
/
db.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
import pymysql
import datetime
class Database:
def __init__(self):
host = "localhost"
user = "root"
pwd = "100709100tT."
db = "mysql"
self.con = pymysql.connect(
host=host, user=user, password=pwd, db=db, cursorclass=pymysql.cursors.DictCursor)
self.cur = self.con.cursor()
def make_reservation(self, roomNo, startTime, endTime, date, occupancy, username):
try:
self.cur.execute(
"SELECT * FROM room WHERE roomNo = %s", roomNo)
room = self.cur.fetchone()
if not room:
return False, "Room does not exist"
available, msg = isAvailable(
date, startTime, endTime, room['maxReservationDuration'])
if not available:
return False, msg
self.cur.execute("INSERT INTO reservation (roomNo, start_time, end_time, date, occupancy, username) VALUES (%s, %s, %s, %s, %s, %s)", (
roomNo, startTime.strftime('%H:%M:%S'), endTime.strftime('%H:%M:%S'), date.strftime('%Y-%m-%d'), occupancy, username))
reservation_id = self.cur.lastrowid
self.con.commit()
self.cur.execute(
"SELECT * FROM reservation WHERE reservation_id = %s", reservation_id)
data = self.cur.fetchall()
return True, data
except Exception as e:
print(e)
self.con.rollback()
return False, "Failed to make reservation"
def get_reservations(self, username):
try:
self.cur.execute(
"SELECT * FROM reservation WHERE username = %s order by date desc", username)
data = self.cur.fetchall()
return True, data
except Exception as e:
print(e)
return False, "Failed to get reservations"
def get_all_reservations(self):
try:
self.cur.execute(
"SELECT * FROM reservation order by date desc")
data = self.cur.fetchall()
return True, data
except Exception as e:
print(e)
return False, "Failed to get reservations"
def get_reservation(self, reservation_id):
try:
self.cur.execute(
"SELECT * FROM reservation WHERE reservation_id = %s", reservation_id)
data = self.cur.fetchone()
if data is None:
return False, "Reservation does not exist"
return True, data
except Exception as e:
print(e)
return False, "Failed to get reservation"
def cancel_reservation(self, reservation_id, username):
try:
succes, reservation = self.get_reservation(reservation_id)
if not succes and reservation is None:
return False, "Reservation does not exist"
else:
if reservation['username'] != username:
return False, "You do not have permission to cancel this reservation"
if reservation['status'] == 'cancelled':
return False, f"Reservation for room {reservation['roomNo']} is already cancelled"
self.cur.execute(
"UPDATE reservation SET status = 'cancelled' WHERE reservation_id = %s", reservation_id
)
self.con.commit()
return True, None
except Exception as e:
print(e)
self.con.rollback()
return False, "Failed to cancel reservation"
# admin cancel reservation
def admin_cancel_reservation(self, reservation_id):
try:
succes, reservation = self.get_reservation(reservation_id)
if not succes and reservation is None:
return False, "Reservation does not exist"
else:
if reservation['status'] == 'cancelled':
return False, f"Reservation for room {reservation['roomNo']} is already cancelled"
self.cur.execute(
"UPDATE reservation SET status = 'cancelled' WHERE reservation_id = %s", reservation_id
)
self.con.commit()
return True, None
except Exception as e:
print(e)
self.con.rollback()
return False, "Failed to cancel reservation"
def modify_reservation(self, reservation_id, roomNo, startTime, endTime, date, occupancy, username):
try:
succes, reservation = self.get_reservation(reservation_id)
if not succes and reservation is None:
return False, "Reservation does not exist"
else:
if reservation['username'] != username:
return False, "You do not have permission to modify this reservation"
if reservation['status'] == 'cancelled':
return False, f"Reservation for room {reservation['roomNo']} is already cancelled"
self.cur.execute(
"SELECT * FROM room WHERE roomNo = %s", roomNo)
rooms = self.cur.fetchall()
available, msg = isAvailable(
date, startTime, endTime, rooms[0]['maxReservationDuration'])
if not available:
return False, msg
self.cur.execute(
"UPDATE reservation SET roomNo = %s, start_time = %s, end_time = %s, date = %s, occupancy = %s WHERE reservation_id = %s", (
roomNo, startTime.strftime('%H:%M:%S'), endTime.strftime('%H:%M:%S'), date.strftime('%Y-%m-%d'), occupancy, reservation_id)
)
self.con.commit()
return True, None
except Exception as e:
print(e)
self.con.rollback()
return False, "Failed to modify reservation"
# admin modify reservation
def admin_modify_reservation(self, reservation_id, roomNo, startTime, endTime, date, occupancy, username):
try:
succes, reservation = self.get_reservation(reservation_id)
if not succes and reservation is None:
return False, "Reservation does not exist"
else:
if reservation['status'] == 'cancelled':
return False, f"Reservation for room {reservation['roomNo']} is already cancelled"
self.cur.execute(
"SELECT * FROM room WHERE roomNo = %s", roomNo)
rooms = self.cur.fetchall()
available, msg = isAvailable(
date, startTime, endTime, rooms[0]['maxReservationDuration'])
if not available:
return False, msg
self.cur.execute(
"UPDATE reservation SET roomNo = %s, start_time = %s, end_time = %s, date = %s, occupancy = %s WHERE reservation_id = %s", (
roomNo, startTime.strftime('%H:%M:%S'), endTime.strftime('%H:%M:%S'), date.strftime('%Y-%m-%d'), occupancy, reservation_id)
)
self.con.commit()
return True, None
except Exception as e:
print(e)
self.con.rollback()
return False, "Failed to modify reservation"
def get_all_users(self):
try:
self.cur.execute("SELECT * FROM useraccount")
data = self.cur.fetchall()
return True, data
except Exception as e:
print(e)
return False, "Failed to get all users"
def get_all_rooms(self):
try:
self.cur.execute(
"SELECT r.roomNo, r.roomType, t.name FROM room r join roomtype t on r.roomType = t.roomType")
data = self.cur.fetchall()
return True, data
except Exception as e:
print(e)
return False, "Failed to get all rooms"
def isAvailable(date, startTime, endTime, maxReservationDuration):
start = datetime.datetime.combine(date, startTime)
end = datetime.datetime.combine(date, endTime)
now = datetime.datetime.now()
duration = (end - start).seconds / 3600
if (end - start).seconds / 60 < 15:
return False, "Reservations must be at least 15 minutes long"
# duration must be less than max duration allowed
if duration > maxReservationDuration:
return False, "Exceeded max duration allowed"
if (now - start).seconds / 60 < 30:
return False, "Reservations must be made at least 30 minutes in advance"
return True, None