From e38aac041e5af8cb1028c5dd8d58413120bad47d Mon Sep 17 00:00:00 2001 From: TheoKnoll Date: Sat, 26 Feb 2022 16:56:21 -0500 Subject: [PATCH 1/2] Created get-course-info endpoint to return all desired course information, when given a term and a course-id --- README.md | 12 ++++++++++- app.py | 13 +++++++++++- database_api.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 80e97d9..1f4b370 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -# course-selection \ No newline at end of file +# course-selection +# Running the Program -- Frontend. +To run the program, cd into the front_end directory. +Run ```npm install``` to install the required packages. +Use ```npm start``` to run the program. It will run on localhost:3000 + + +# Running the Program -- Backend. +To run the program, ensure that your .env files are set up. See back_end README.md for instructions. + +Then, use ```flask run``` to run the program. It will run on localhost:5000 \ No newline at end of file diff --git a/app.py b/app.py index af2a6be..65a0f09 100644 --- a/app.py +++ b/app.py @@ -64,6 +64,7 @@ def test_database(): logger.info("Reached /api/test-database") return jsonify(db.get_all_test()) + @app.route("/search", methods=["GET"]) def search(): args = request.args @@ -91,4 +92,14 @@ def search(): # See database_api.py for structure of input query res = do_search(query_dict) - return jsonify(res) \ No newline at end of file + return jsonify(res) + + +@app.route("/api/get-course-info", methods=["GET"]) +def course_info(): + logger.info("Reached /api/course-info") + args = request.args + course_id = args.get("course_id", "") + semester = args.get("semester", "") + res = db.get_course_info(course_id, semester) + return jsonify(res) diff --git a/database_api.py b/database_api.py index ffa905e..292656b 100644 --- a/database_api.py +++ b/database_api.py @@ -1,3 +1,4 @@ +from flask import jsonify from pymongo import MongoClient from dotenv import load_dotenv import logging @@ -27,9 +28,11 @@ def __init__(self): self.db = self.client.course_selection def connect(self): - self.client = MongoClient(os.getenv("MONGO"), tlsCAFile=certifi.where()) + self.client = MongoClient(os.getenv("MONGO"), + tlsCAFile=certifi.where()) self.db_admin = self.client.admin - logger.info(f"MongoDB server status: {self.db_admin.command('serverStatus')}") + logger.info( + f"MongoDB server status: {self.db_admin.command('serverStatus')}") def get_all_test(self): # note that when you are returning, you want it to be jsonify-able, @@ -48,11 +51,57 @@ def get_all_test(self): ret = None return ret + def get_course_info(self, course_id, semester): + """ + Retrieves course info from the database for a specific + course_id, from a given semester + + Semester is of format Spring/Summer/Fall Year + + This information includes all course evaluation data across + different terms for this course_id + + params: query = { + "course_id": "the course_id", + "semester = "semester_in_words" + } + + Example from COS 126, Fall 2021: + query = { "course_id": "002051", "semester": "Fall 2021" } + """ + if course_id == "": + return [], [] + + try: + # Assume semester is passed in as "[Fall/Spring/Summer] [Year]" + term_data = self.db.semesters.find_one( + {"name": semester}, {"_id": 0, "code": 1} + ) + if term_data is not None: + term = term_data["code"] + + course_db = self.db.courses + evals_db = self.db.evaluations + course_query_string = {'course_id': course_id, 'term': term} + course_res = course_db.find(course_query_string, + {"_id": 0}) + eval_query_string = {'course_id': course_id} + eval_res = evals_db.find(eval_query_string, {"_id": 0}) + course_res = list(course_res) + eval_res = list(eval_res) + ret = course_res + eval_res + except Exception as e: + logger.error( + f"Failed to get information for course_id {course_id}. " + f"Error displayed below:\n{e}" + ) + ret = None + return ret + def close(self): self.client.close() - if __name__ == "__main__": # a basic example of how to use, can remove later db = DatabaseAPI() From 5e9926278a23f249fce672dc92ef7717d96fdd35 Mon Sep 17 00:00:00 2001 From: TheoKnoll Date: Sat, 26 Feb 2022 17:02:38 -0500 Subject: [PATCH 2/2] Added helpful comment about return of get-course-info --- database_api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/database_api.py b/database_api.py index 292656b..8a14e1d 100644 --- a/database_api.py +++ b/database_api.py @@ -96,6 +96,9 @@ def get_course_info(self, course_id, semester): f"Error displayed below:\n{e}" ) ret = None + # Note, right now this returns a ton of information + # If this information is difficult to use on the front end + # This code can be modified going forward to meet this need return ret def close(self):