forked from CodieTamida/CPSC-449-Project2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_query.py
547 lines (490 loc) · 22.2 KB
/
database_query.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
from sqlite3 import Connection
from typing import List, Union
from fastapi import HTTPException, status
from loguru import logger
from typing import Optional
from .models import (
AvailableClass,
EnrollmentRequest,
EnrollmentResponse,
QueryStatus,
Registration,
RegistrationStatus,
RemoveFromWaitlistRes,
UserRole,
EnrollmentListResponse,
WaitlistStudents,
WaitlistPositionList
)
LIST_AVAILABLE_SQL_QUERY = """
SELECT available_classes.name as 'course_name', available_classes.coursecode as 'course_code',
available_classes.department, available_classes.currentenrollment as 'current_enrollment',
available_classes.waitlist, available_classes.maxenrollment as "max_enrollment",
available_classes.sectionnumber as "section_number",
ur.name as "instructor_first_name", ur.lastname as "instructor_last_name"
FROM Users ur, (SELECT cl.coursecode, cl.name, cl.department, sc.currentenrollment,
sc.maxenrollment, sc.waitlist, sc.sectionnumber, sc.instructorid FROM "Class" as cl
join section as sc on cl.coursecode = sc.coursecode WHERE cl.Department = '{department_name}')
as available_classes where ur.cwid = available_classes.instructorid
"""
WAITLIST_ALLOWED = 15
class DBException(Exception):
def __init__(self, error_detail:str) -> None:
self.error_detail = error_detail
def get_available_classes(db_connection: Connection, department_name: str) -> List[AvailableClass]:
"""Query database to get available classes for a given department name
Args:
db_connection (Connection): SQLite Connection
department_name (str): Department name
Returns:
List[AvailableClass]: List of available classes
"""
result = []
query = LIST_AVAILABLE_SQL_QUERY.format(department_name=department_name)
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found for given department_name:{department_name}')
for row in rows:
available_class = AvailableClass(course_name=row[0],
course_code=row[1],
department=row[2],
current_enrollment=row[3],
waitlist=row[4],
max_enrollment=row[5],
section_number=row[6],
instructor_first_name=row[7],
instructor_last_name=row[8])
result.append(available_class)
cursor.close()
return result
def check_user_role(db_connection: Connection, student_id: int)-> Union[str, None]:
logger.info('Checking user role')
query = f"""
SELECT role FROM Users where CWID = {student_id}
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found for given student_id:{student_id}')
result = UserRole.NOT_FOUND
for row in rows:
result = row[0]
return result
def count_waitlist_registration(db_connection: Connection, section_id: int)->int:
logger.info('Checking waitlist registration')
query = f"""SELECT COUNT(*) FROM RegistrationList WHERE ClassID = {section_id} and Status = 'waitlisted'
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found for given section_id:{section_id}')
result = 0
for row in rows:
result = row[0]
return result
def check_enrollment_eligibility(db_connection: Connection, section_number: int, course_code: str)->str:
logger.info('Checking enrollment eligibility')
query = f"""SELECT CurrentEnrollment as 'current_enrollment', MaxEnrollment as 'max_enrollment', Waitlist as 'waitlist' FROM "Section" WHERE CourseCode = '{course_code}' and SectionNumber = {section_number}
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found for given section_number:{section_number} and course_code:{course_code}')
query_result = {}
for row in rows:
query_result['current_enrollment'] = row[0]
query_result['max_enrollment'] = row[1]
query_result['waitlist'] = row[2]
# First check whether there is capacity to enroll in a section
if query_result['max_enrollment'] - query_result['current_enrollment'] >= 1:
return RegistrationStatus.ENROLLED
if query_result['waitlist'] <= WAITLIST_ALLOWED:
return RegistrationStatus.WAITLISTED
return RegistrationStatus.NOT_ELIGIBLE
def check_status_query(db_connection: Connection, enrollment_request: EnrollmentRequest) -> Union[EnrollmentResponse, None]:
check_status_query = f""" SELECT Status, EnrollmentDate FROM RegistrationList where StudentID = {enrollment_request.student_id} and SectionNumber = {enrollment_request.section_number} and CourseCode = '{enrollment_request.course_code}'"""
cursor = db_connection.cursor()
try:
rows = cursor.execute(check_status_query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found')
row = rows.fetchone()
if row[0] == RegistrationStatus.ENROLLED:
return EnrollmentResponse(enrollment_status="already enrolled", enrollment_date=row[1])
except Exception as err:
logger.error(err)
raise DBException(error_detail = 'Fail to register')
return None
def complete_registration(db_connection: Connection, registration: Registration) -> str:
logger.info('Starting registration')
insert_query = f"""
INSERT INTO RegistrationList (StudentID,CourseCode, SectionNumber, Status) VALUES ({registration.student_id},'{registration.course_code}', {registration.section_number}, '{registration.enrollment_status}')
"""
update_current_enrollment_query = f"""
UPDATE "Section" SET CurrentEnrollment = CurrentEnrollment + 1 WHERE SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}'
"""
update_waitlist_query = f"""
UPDATE "Section" SET Waitlist = Waitlist + 1 WHERE SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}'
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(insert_query)
if registration.enrollment_status == RegistrationStatus.ENROLLED:
cursor.execute(update_current_enrollment_query)
elif registration.enrollment_status == RegistrationStatus.WAITLISTED:
cursor.execute(update_waitlist_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to register')
finally:
cursor.close()
return QueryStatus.SUCCESS
def update_student_registration_status(db_connection:Connection, registration: Registration)-> str:
logger.info('Upadting the registration status')
check_status_query = f""" SELECT Status FROM RegistrationList where StudentID = {registration.student_id} and SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}'"""
update_status_query = f""" UPDATE RegistrationList SET Status = 'dropped' where StudentID = {registration.student_id} and
SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}' and status = 'enrolled' """
update_current_enrollment_query = f"""UPDATE SECTION set CurrentEnrollment = CurrentEnrollment -1 where SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}'"""
update_waitlist_count_query = f"""UPDATE "Section" SET Waitlist = Waitlist - 1 WHERE SectionNumber = {registration.section_number} and CourseCode = '{registration.course_code}'"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
rows = cursor.execute(check_status_query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found')
row = rows.fetchone()
if row[0] == RegistrationStatus.DROPPED:
return RegistrationStatus.DROPPED
else:
cursor.execute(update_status_query)
if row[0] == RegistrationStatus.ENROLLED:
cursor.execute(update_current_enrollment_query)
elif row[0] == RegistrationStatus.WAITLISTED:
cursor.execute(update_waitlist_count_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to drop the class')
finally:
cursor.close()
return QueryStatus.SUCCESS
def check_class_exists(db_connection: Connection, course_code: str)-> bool:
logger.info('Checking if class exists')
result = False
query = f"""
SELECT CourseCode FROM Class where CourseCode = '{course_code}'
"""
cursor = db_connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
if len(rows) > 0:
result = True
return result
def check_section_exists(db_connection: Connection, course_code: str, section_number: int)-> bool:
logger.info('Checking if section exists')
result = False
query = f"""
SELECT SectionNumber FROM Section where CourseCode = '{course_code}' and SectionNumber = {section_number}
"""
cursor = db_connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
if len(rows) > 0:
result = True
return result
def check_is_instructor(db_connection: Connection, instructor_id: int)-> Union[str, None]:
logger.info('Checking if user is instructor')
query = f"""
SELECT role FROM Users where CWID = {instructor_id}
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found for given student_id:{student_id}')
result = UserRole.NOT_FOUND
for row in rows:
result = row[0]
return result
def addClass(db_connection: Connection, course_code, class_name, department) -> str:
logger.info('Starting to add class')
insert_query = f"""
INSERT INTO Class (CourseCode, Name, Department) VALUES ('{course_code}', '{class_name}', '{department}')
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(insert_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to add class')
finally:
cursor.close()
return QueryStatus.SUCCESS
def addSection(db_connection: Connection, section_number, course_code, instructor_id, max_enrollment) -> str:
logger.info('Starting to add section')
insert_query = f"""
INSERT INTO Section (SectionNumber, CourseCode, InstructorID, MaxEnrollment, CurrentEnrollment, Waitlist, SectionStatus) VALUES ({section_number}, '{course_code}', {instructor_id}, {max_enrollment}, 0, 0, 'open')
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(insert_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to add section')
finally:
cursor.close()
return QueryStatus.SUCCESS
def deleteSection(db_connection: Connection, course_code: str, section_number: int) -> str:
logger.info('Starting to delete section')
delete_query = f"""
DELETE FROM Section WHERE CourseCode = '{course_code}' and SectionNumber = {section_number}
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(delete_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to delete section')
finally:
cursor.close()
return QueryStatus.SUCCESS
def changeSectionInstructor(db_connection: Connection, course_code: str, section_number: int, instructor_id: int) -> str:
logger.info('Starting to change instructor for section ', str(section_number))
update_query = f"""
UPDATE Section SET InstructorID = {instructor_id} WHERE SectionNumber = {section_number} and CourseCode = '{course_code}'
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(update_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to change instructor')
finally:
cursor.close()
return QueryStatus.SUCCESS
def freezeEnrollment(db_connection: Connection, course_code: str, section_number: int) -> str:
logger.info('Starting to freeze enrollment for section ', str(section_number))
update_query = f"""
UPDATE Section SET SectionStatus = 'closed' WHERE SectionNumber = {section_number} and CourseCode = '{course_code}'
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
try:
cursor.execute(update_query)
cursor.execute("COMMIT")
except Exception as err:
logger.error(err)
cursor.execute("ROLLBACK")
logger.info('Rolling back transaction')
raise DBException(error_detail = 'Fail to freeze enrollment')
finally:
cursor.close()
return QueryStatus.SUCCESS
def get_enrolled_students(db_connection: Connection, instructor_id: int, course_code: Optional[str] = None, section_number: Optional[int] = None) -> List[EnrollmentListResponse]:
logger.info('Getting enrolled students for instructor with CWID:')
query = """
SELECT
Users.CWID AS StudentCWID,
Users.Name AS StudentFirstName,
Users.LastName AS StudentLastName,
Class.CourseCode AS CourseCode,
Section.SectionNumber AS SectionNumber,
Class.Name AS ClassName,
RegistrationList.Status AS Status
FROM
RegistrationList
JOIN Users ON RegistrationList.StudentID = Users.CWID
JOIN Section ON RegistrationList.CourseCode = Section.CourseCode AND RegistrationList.SectionNumber = Section.SectionNumber
JOIN Class ON Section.CourseCode = Class.CourseCode
WHERE
Section.InstructorID = ?
AND RegistrationList.Status = 'enrolled'
"""
params = [instructor_id]
if course_code is not None:
query += " AND Section.CourseCode = ?"
params.append(course_code)
if section_number is not None:
query += " AND Section.SectionNumber = ?"
params.append(section_number)
else:
query += " ORDER BY Class.CourseCode, Section.SectionNumber, Users.LastName, Users.Name"
cur = db_connection.execute(query, tuple(params))
enrollment = cur.fetchall()
if not enrollment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Enrollment for instructor not found"
)
results = [{"student_cwid": row[0],
"student_first_name": row[1],
"student_last_name": row[2],
"course_code": row[3],
"section_number": row[4],
"class_name": row[5],
"status": row[6]} for row in enrollment]
return results
# dropped students
def get_dropped_students(db_connection: Connection, instructor_id: int,course_code: Optional[str] = None, section_number: Optional[int] = None) -> List[EnrollmentListResponse]:
logger.info('Getting dropped students for instructor')
query = """
SELECT
Users.CWID AS StudentCWID,
Users.Name AS StudentFirstName,
Users.LastName AS StudentLastName,
Class.CourseCode AS CourseCode,
Section.SectionNumber AS SectionNumber,
Class.Name AS ClassName,
RegistrationList.Status AS Status
FROM
RegistrationList
JOIN Users ON RegistrationList.StudentID = Users.CWID
JOIN Section ON RegistrationList.CourseCode = Section.CourseCode AND RegistrationList.SectionNumber = Section.SectionNumber
JOIN Class ON Section.CourseCode = Class.CourseCode
WHERE
Section.InstructorID = ?
AND RegistrationList.Status = 'dropped'
"""
params = [instructor_id]
if course_code is not None:
query += " AND Section.CourseCode = ?"
params.append(course_code)
if section_number is not None:
query += " AND Section.SectionNumber = ?"
params.append(section_number)
else:
query += " ORDER BY Class.CourseCode, Section.SectionNumber, Users.LastName, Users.Name"
cur = db_connection.execute(query, tuple(params))
enrollment = cur.fetchall()
if not enrollment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="No students that dropped found for instructor"
)
results = [{"student_cwid": row[0],
"student_first_name": row[1],
"student_last_name": row[2],
"course_code": row[3],
"section_number": row[4],
"class_name": row[5],
"status": row[6]} for row in enrollment]
return results
def get_waitlist_status(db_connection: Connection, student_id: int) -> str:
logger.info('Checking waitlist position for student ', str(student_id))
query = f"""
WITH WaitlistPosition AS (
SELECT
rl.StudentID,
rl.CourseCode,
rl.SectionNumber,
rl.Status,
ROW_NUMBER() OVER (PARTITION BY rl.CourseCode, rl.SectionNumber ORDER BY rl.EnrollmentDate) AS Position
FROM
RegistrationList rl
WHERE
rl.Status = 'waitlisted'
)
SELECT
wlp.Position,
wlp.CourseCode,
wlp.SectionNumber
FROM
WaitlistPosition wlp
WHERE
wlp.StudentID = {student_id}
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Record not found.')
result = []
for row in rows:
logger.info(str(row))
waitlist = WaitlistPositionList(
waitlist_position = row[0],
section_number = row[2],
course_code = row[1]
)
result.append(waitlist)
print(result)
return result
def get_waitlist(db_connection: Connection, course_code: str, section_number: int) -> list:
logger.info(f'fetching the students on the waitlist with coursecode and section no {course_code}, {section_number}')
query = f"""
SELECT
r.StudentID,
u.Name AS StudentName,
r.EnrollmentDate,
r.Status
FROM
RegistrationList r
JOIN
Users u ON r.StudentID = u.CWID
WHERE
r.CourseCode = "{course_code}"
AND r.SectionNumber = {section_number}
AND r.Status = 'waitlisted'
ORDER BY
r.EnrollmentDate;
"""
cursor = db_connection.cursor()
rows = cursor.execute(query)
if rows.arraysize == 0:
raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail= f'Records not found.')
# todo: throw appropriate error messages
result = []
for row in rows:
logger.info(str(row))
student = WaitlistStudents(
student_id = row[0],
student_name = row[1],
enrollment_date = row[2]
)
result.append(student)
logger.info(result)
return result
#Remove from Waitlist
def remove_student_from_waitlist(db_connection: Connection, student_id: int, course_code: str, section_number: int) -> str:
try:
# Check if the student is on the waitlist for the specified course and section
is_on_waitlist = check_student_on_waitlist(db_connection, student_id, course_code, section_number)
if not is_on_waitlist:
return RemoveFromWaitlistRes(status="Student is not on the waitlist")
# Remove the student from the waitlist
remove_query = f"""
DELETE FROM RegistrationList WHERE StudentID = {student_id} AND CourseCode = '{course_code}' AND SectionNumber = {section_number} AND Status = 'waitlisted'
"""
cursor = db_connection.cursor()
cursor.execute("BEGIN")
cursor.execute(remove_query)
cursor.execute("COMMIT")
return RemoveFromWaitlistRes(status="Successfully removed student from the waitlist")
except Exception as err:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err))
def check_student_on_waitlist(db_connection: Connection, student_id: int, course_code: str, section_number: int) -> bool:
query = f"""
SELECT 1 FROM RegistrationList WHERE StudentID = {student_id} AND CourseCode = '{course_code}' AND SectionNumber = {section_number} AND Status = 'waitlisted'
"""
cursor = db_connection.cursor()
cursor.execute(query)
return cursor.fetchone() is not None