Skip to content

Commit

Permalink
update finals
Browse files Browse the repository at this point in the history
added get requests for finals by semester, also added cache for the finals table
  • Loading branch information
Tevetron committed Nov 5, 2024
1 parent 88ca81e commit f2975a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
db_conn = connection.db
class_info = ClassInfo.ClassInfo(db_conn)
courses = Courses.Courses(db_conn, FastAPICache)
finals = Finals.Finals(db_conn)
finals = Finals.Finals(db_conn, FastAPICache)
date_range_map = DateMapping.semester_date_mapping(db_conn)
admin_info = AdminInfo.Admin(db_conn)
course_select = CourseSelect.student_course_selection(db_conn)
Expand Down Expand Up @@ -229,6 +229,15 @@ async def uploadHandler(
error = finals.populate_from_csv(csv_file)
return Response(error.__str__(), status_code=500) if error else Response("Upload Successful", status_code=200)

@app.get('/api/final/{semester}')
@cache(expire=Constants.DAY_IN_SECONDS, coder=PickleCoder, namespace="API_CACHE")
async def getHandler(semester: str):
if not semester:
return Response("No semester received", 400)
print(semester)
final, error = finals.get_by_semester(semester)
return final if not error else Response(error, status_code=500)

@app.delete('/api/final/{semester}')
async def deleteHandler(semester: str):
if not semester:
Expand Down
28 changes: 26 additions & 2 deletions src/api/db/finals.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@


class Finals:
def __init__(self, db_wrapper):
def __init__(self, db_wrapper, cache):
self.db = db_wrapper
self.cache = cache

def populate_from_csv(self, csv_text):
conn = self.db.get_connection()
Expand All @@ -28,7 +29,7 @@ def populate_from_csv(self, csv_text):
INSERT INTO
final(
semester,
course,
course,
section,
"start",
"end",
Expand Down Expand Up @@ -58,9 +59,20 @@ def populate_from_csv(self, csv_text):
conn.rollback()
return e
conn.commit()
self.clear_cache()
return None

def get_by_semester(self, semester):
return self.db.execute("""
SELECT * FROM final
WHERE semester=%(Semester)s
ORDER BY start ASC;
""", {
"Semester": semester
}, isSELECT=True)

def delete_by_semester(self, semester):
self.clear_cache()
return self.db.execute("""
BEGIN TRANSACTION;
DELETE FROM final
Expand All @@ -76,8 +88,20 @@ def bulk_delete(self, semesters):
if error:
print(error)
return error
self.clear_cache()
return None

def clear_cache(self):
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None

if loop and loop.is_running():
loop.create_task(self.cache.clear(namespace="API_CACHE"))
else:
asyncio.run(self.cache.clear("API_CACHE"))

if __name__ == "__main__":
# os.chdir(os.path.abspath("../rpi_data"))
# fileNames = glob.glob("*.csv")
Expand Down

0 comments on commit f2975a6

Please sign in to comment.