Weirui-Leo commited on
Commit
6e73939
·
1 Parent(s): c6fdccf

fixed with reference to Raymond's work

Browse files
app/app.py CHANGED
@@ -4,7 +4,7 @@ import logging
4
  from flask import Flask
5
  from flask_apscheduler import APScheduler
6
  from asgiref.wsgi import WsgiToAsgi
7
- from routes.category import category_bp
8
 
9
  # from routes.summary import summary_bp
10
 
 
4
  from flask import Flask
5
  from flask_apscheduler import APScheduler
6
  from asgiref.wsgi import WsgiToAsgi
7
+ from controllers.category import category_bp
8
 
9
  # from routes.summary import summary_bp
10
 
{routes → app/controllers}/__init__.py RENAMED
File without changes
app/controllers/category.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from database.mongodb import category_collection
2
+ def get_categories():
3
+
4
+ """
5
+ Returns all categories and their corresponding sites
6
+
7
+ """
8
+ categories = list(category_collection.find({}, {"_id": 0, "category": 1, "site": 1}))
9
+ return categories
app/controllers/finfast-summary-backend.code-workspace ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "../.."
5
+ }
6
+ ]
7
+ }
app/database/__init__.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ """Module for Mongodb database"""
2
+ from database.mongodb import MongodbClient, FinFastMongodbClient
3
+
4
+ MongoClient = MongodbClient
5
+ FinFastMongoClient = FinFastMongodbClient
6
+
7
+ __all__ = [
8
+ "MongoClient",
9
+ "FinFastMongoClient"
10
+ ]
app/database/mongodb.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ """MongoDB database interaction module."""
2
+ import os
3
+ from pymongo import MongoClient
4
+
5
+ MongodbClient = MongoClient(os.getenv('MONGODB_URI'))
6
+ FinFastMongodbClient = MongoClient(os.getenv("MONGODB_FINFAST_URI"))
7
+ category_collection = FinFastMongodbClient["FinFAST_China"]["Category"]
app/routes/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ """This module sets up the main Blueprint for the Flask application."""
2
+ from flask import Blueprint
3
+
4
+ category_bp = Blueprint("category", __name__)
app/routes/category_router.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from controllers.category import get_categories
2
+ from flask import jsonify
3
+ from . import category_bp
4
+
5
+ @category_bp.route("/api/category", methods=["GET"])
6
+ def fetch_categories():
7
+ """
8
+ Handles GET requests to retrieve all categories.
9
+
10
+ Returns:
11
+ list[dict]: JSON response containing categories and their associated sites.
12
+
13
+ Raises:
14
+ tbd
15
+
16
+ Endpoint:
17
+ GET /api/category
18
+
19
+ """
20
+ category_data = get_categories()
21
+ return jsonify(category_data)
routes/category.py DELETED
@@ -1,22 +0,0 @@
1
- from flask import Blueprint, Flask, jsonify, abort
2
- from pymongo.mongo_client import MongoClient
3
- from pymongo.server_api import ServerApi
4
- from pymongo.errors import PyMongoError
5
- import os
6
-
7
- category_bp = Blueprint("category", __name__)
8
-
9
- uri = os.getenv("uri")
10
-
11
- client = MongoClient(uri)
12
-
13
- db = client["FinFAST_China"]
14
- collection = db["Category"]
15
-
16
- @category_bp.route("/api/category", methods=["GET"])
17
- def get_categories():
18
- # fetched all docs in 'Category' collection excluding _id field
19
- categories = list(db["Category"].find({}, {"_id": 0, "category": 1, "site": 1}))
20
- if not categories:
21
- abort(404, description="No categories found")
22
- return jsonify(categories)