From ef35036bc5a5e0a8d6944d08a19322bbf6a42957 Mon Sep 17 00:00:00 2001 From: Kushal Dotel Date: Sun, 12 Jan 2025 15:10:01 +0545 Subject: [PATCH] public api schema --- backend/app.py | 2 + backend/blueprints/profile/__init__.py | 4 +- backend/blueprints/public/__init__.py | 71 ++++++++++++++++++ ...b78f-a0e8-42f2-bd9f-6792369e3e64_meme.jpg} | Bin 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 backend/blueprints/public/__init__.py rename backend/uploads/{meme.jpg => user_3e7bb78f-a0e8-42f2-bd9f-6792369e3e64_meme.jpg} (100%) diff --git a/backend/app.py b/backend/app.py index 410a7a6..54e935e 100644 --- a/backend/app.py +++ b/backend/app.py @@ -18,6 +18,7 @@ from blueprints.profile import profile as profileBlueprint from blueprints.session import session as sessionBlueprint from blueprints.admin import admin as adminBlueprint from blueprints.chat import chat as chatBlueprint +from blueprints.public import public_summary as publicBlueprint app = Flask(__name__) @@ -38,6 +39,7 @@ app.register_blueprint(profileBlueprint, url_prefix='/api/profile') app.register_blueprint(sessionBlueprint,url_prefix='/api/session') app.register_blueprint(adminBlueprint,url_prefix='/api/admin') app.register_blueprint(chatBlueprint,url_prefix='/api/chat') +app.register_blueprint(publicBlueprint,url_prefix='/api/public') @app.route('/media/') def send_file(filename): diff --git a/backend/blueprints/profile/__init__.py b/backend/blueprints/profile/__init__.py index e7082d9..7e7373d 100644 --- a/backend/blueprints/profile/__init__.py +++ b/backend/blueprints/profile/__init__.py @@ -142,8 +142,8 @@ def manage_profile(): # Update the user's profile using form data try: # email = request.form.get('email') - first_name = request.form.get('first_name') - last_name = request.form.get('last_name') + first_name = request.form.get('firstName') + last_name = request.form.get('lastName') username = request.form.get('username') dob = request.form.get('dob') bio = request.form.get('bio') diff --git a/backend/blueprints/public/__init__.py b/backend/blueprints/public/__init__.py new file mode 100644 index 0000000..f73c2fb --- /dev/null +++ b/backend/blueprints/public/__init__.py @@ -0,0 +1,71 @@ +from flask import Blueprint, jsonify +from db.model import User, Course, db +from sqlalchemy import select, func + +public_summary = Blueprint('public', __name__) + +@public_summary.route('/stats/total-users', methods=['GET']) +def get_total_users(): + """ + Fetch total user count. + """ + try: + total_users = db.session.execute( + select(func.count()).select_from(User) + ).scalar() + + return jsonify({'totalUsers': total_users}), 200 + + except Exception as e: + return jsonify({'message': f'An error occurred: {str(e)}'}), 500 + + +@public_summary.route('/stats/total-authors', methods=['GET']) +def get_total_authors(): + """ + Fetch total authors (users who have created courses). + """ + try: + total_authors = db.session.execute( + select(func.count(func.distinct(Course.authorID))) + .select_from(Course) + ).scalar() + + return jsonify({'totalAuthors': total_authors}), 200 + + except Exception as e: + return jsonify({'message': f'An error occurred: {str(e)}'}), 500 + + +@public_summary.route('/stats/total-courses', methods=['GET']) +def get_total_courses(): + """ + Fetch total course count. + """ + try: + total_courses = db.session.execute( + select(func.count()).select_from(Course) + ).scalar() + + return jsonify({'totalCourses': total_courses}), 200 + + except Exception as e: + return jsonify({'message': f'An error occurred: {str(e)}'}), 500 + + +@public_summary.route('/stats/subscribed-users', methods=['GET']) +def get_subscribed_users(): + """ + Fetch count of users subscribed to the newsletter and are activated. + """ + try: + subscribed_users = db.session.execute( + select(func.count(User.email)) + .select_from(User) + .where(User.isActivated == True) + ).scalar() + + return jsonify({'subscribedNewsletter': subscribed_users}), 200 + + except Exception as e: + return jsonify({'message': f'An error occurred: {str(e)}'}), 500 \ No newline at end of file diff --git a/backend/uploads/meme.jpg b/backend/uploads/user_3e7bb78f-a0e8-42f2-bd9f-6792369e3e64_meme.jpg similarity index 100% rename from backend/uploads/meme.jpg rename to backend/uploads/user_3e7bb78f-a0e8-42f2-bd9f-6792369e3e64_meme.jpg