From 66ae00d096d84bc7d2339e6a24c972409fd4c1fa Mon Sep 17 00:00:00 2001 From: Nicholas Couillard Date: Wed, 11 Oct 2023 12:32:43 -0400 Subject: [PATCH] added more DynamoDB course functions --- CourseFunctions.py | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 CourseFunctions.py diff --git a/CourseFunctions.py b/CourseFunctions.py new file mode 100644 index 0000000..1f4633b --- /dev/null +++ b/CourseFunctions.py @@ -0,0 +1,106 @@ +from boto3 import resource +from boto3.dynamodb.conditions import Attr, Key +import math + +course_table = resource('dynamodb').Table('Courses') +# NOTE: another table for users and professors may be necessary. In order to do this we might need functionality to introduce salt and hashing for security + +def courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, prerequisites, courseStartTime=0, courseEndTime=0, courseScheduledDays='None', courseLocation='None'): + courseInsertion = course_table.put_item( + Item = { + 'CourseId' : courseId, # partition key + 'Section' : courseSection, # sort key? + 'Level' : courseLevel, # useful attribute for searching + 'Department' : courseDepartment, # useful attribute for searching + 'Professor' : courseProfessor, # TODO: support multiple instructors + 'StudentsEnrolled' : courseEnrolledStudents, # useful attribute for professor + 'MaxStudents' : courseMaxStudents, # can calculate OpenStatus using some logic + 'StartTime' : courseStartTime, # is storing times in the form of 24-hour time easier? it's simple to convert when necessary + 'EndTime' : courseEndTime, + 'OpenStatus' : courseIsOpen, # this attribute could be useful for hiding unavailable courses + 'CourseFormat' : courseFormat, + 'CourseScheduledDays' : courseScheduledDays, # this will likely be a list, but im lazy so for now it's a string + 'PreRequisites' : prerequisites, # this will be a list of courses + 'CourseLocation' : courseLocation # could increase functionality by separating into building and room number, to allow searching by building + } + ) + + print(f"Insert response: {courseInsertion}") + +def courseRemove(courseId, courseSection): + courseRemoval = course_table.delete_item( + Key={ + 'CourseId' : courseId, # partition key + 'Section' : courseSection # sort key + } + ) + + print(f"Removal response: {courseRemoval}") + +def enrollStudent(courseId, courseSection): + studentEnroller = course_table.get_item( + Key={ + 'CourseId' : courseId, + 'Section' : courseSection + }, + AttributesToGet = ['OpenStatus', 'StudentsEnrolled', 'MaxStudents'] + ) + + isOpen = (studentEnroller.get('Item')).get('OpenStatus') + numStudents = (studentEnroller.get('Item')).get('StudentsEnrolled') + maxStudents = (studentEnroller.get('Item')).get('MaxStudents') + if maxStudents - (numStudents + 1) > 0: + newOpenStatus = True + else: + newOpenStatus = False + + if isOpen == True: + numStudents += 1 + student_enrolled = course_table.update_item( + Key={ + 'CourseId' : courseId, + 'Section' : courseSection + }, + UpdateExpression='SET StudentsEnrolled = :val1, OpenStatus = :val2', + ExpressionAttributeValues={ + ':val1': numStudents, + ':val2': newOpenStatus + } + ) + + print(f"{student_enrolled}") + + +if __name__ == '__main__': + + # NOTE: NONE OF THE INFO COLLECTING WILL LIKELY BE DONE IN PYTHON, THIS IS PROBABLY VUE STUFF. JUST FOR DEMO + # courseDepartment = input("Enter department: ").upper() + # courseNumber = int(input("\nEnter course number: ")) # NOTE: In vue, make text box that only accepts integers to avoid errors + # courseSection = input("\nEnter course section: ") + # if len(courseSection) == 1: + # courseSection = '00' + courseSection + # elif len(courseSection) == 2: + # courseSection = '0' + courseSection + # courseType = input("\nSelect the type of course if needed: Laboratory, Discussion: ") + # if courseType == "Laboratory": + # courseSection += 'L' + # elif courseType == "Discussion": + # courseSection += 'D' + # courseLevel = math.floor(courseNumber / 1000) * 1000 + # courseId = courseDepartment + str(courseNumber) + # courseProfessor = input("\nEnter professor: ") # TODO: support multiple instructors + # courseMaxStudents = int(input("\nEnrollment slots: ")) + # courseFormat = input("\nIs the course online asynchronous, online synchronous, or in-person?: ") # this will be easier to integrate with a few select vue options + # if courseFormat == "In-person" or courseFormat == "Online synchronous": + # courseScheduledDays = input("\nDays the course will meet: ") + # courseStartTime = input("\nStart time: ") # TODO: Turn 12-hour time into 24-hour time integer. Will be easy with vue + # courseEndTime = input("\nEnd time: ") # TODO: Turn 12-hour time into 24-hour time integer. Will be easy with vue + # courseEnrolledStudents = 0 + # courseIsOpen = bool(courseMaxStudents - courseEnrolledStudents) + # if courseFormat == "In-person": + # courseLocation = input("Where will the course be held?: ") + + # courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseStartTime, courseEndTime, courseIsOpen, courseFormat, courseScheduledDays, courseLocation) + + #courseRemove('COMM1000', '001D') + enrollStudent('CSE1010', '001L') \ No newline at end of file