Skip to content

Commit ba14bd5

Browse files
Add group listing endpoint to API with optional user filtering
1 parent 5bf9465 commit ba14bd5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

api/auth/group/list.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# FILE: auth/group/list.py
2+
from flask import Blueprint, request, jsonify
3+
from firebase_admin import firestore
4+
5+
list_group_bp = Blueprint('list_group', __name__)
6+
7+
db = firestore.client()
8+
9+
@list_group_bp.route('/auth/group/list', methods=['GET'])
10+
def list_groups():
11+
"""
12+
Route to retrieve the list of public groups. Optionally filter by user email if a 'user' query parameter is provided.
13+
"""
14+
try:
15+
# Get the optional 'user' query parameter
16+
user_email = request.args.get('user')
17+
18+
# Query Firestore for public groups
19+
groups_ref = db.collection('groups').where("privacy", "==", "public")
20+
public_groups = groups_ref.stream()
21+
22+
groups_list = []
23+
for group in public_groups:
24+
group_data = group.to_dict()
25+
if user_email:
26+
# If filtering by user email, only include groups where the user is a member
27+
if user_email in group_data.get('members', []):
28+
groups_list.append({"id": group.id, **group_data})
29+
else:
30+
# Include all public groups if no user filter is applied
31+
groups_list.append({"id": group.id, **group_data})
32+
33+
return jsonify({
34+
"success": True,
35+
"groups": groups_list
36+
}), 200
37+
38+
except Exception as e:
39+
return jsonify({"error": str(e)}), 500

api/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@
6969
from auth.group.create import create_group_bp
7070
from auth.group.delete import delete_group_bp
7171
from auth.group.privacy import privacy_group_bp
72+
from auth.group.list import list_group_bp
7273

7374
app.register_blueprint(signup_bp)
7475
app.register_blueprint(login_bp)
7576
app.register_blueprint(create_group_bp)
7677
app.register_blueprint(delete_group_bp)
7778
app.register_blueprint(privacy_group_bp)
79+
app.register_blueprint(list_group_bp)
7880
print("Auth blueprints registered successfully.")
7981
except ImportError as e:
8082
print(f"Error importing blueprints: {e}")

0 commit comments

Comments
 (0)