Skip to content

Commit 0a1fdcc

Browse files
committed
Added notification routes
1 parent 4c8f43b commit 0a1fdcc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

backend/PyMatcha/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
from PyMatcha.routes.api.search import search_bp
181181
from PyMatcha.routes.api.profile.block import profile_block_bp
182182
from PyMatcha.routes.api.history import history_bp
183+
from PyMatcha.routes.api.notifications import notifications_bp
183184

184185
logging.debug("Registering Flask blueprints")
185186
application.register_blueprint(user_bp)
@@ -199,6 +200,7 @@
199200
application.register_blueprint(search_bp)
200201
application.register_blueprint(profile_block_bp)
201202
application.register_blueprint(history_bp)
203+
application.register_blueprint(notifications_bp)
202204

203205
if application.debug:
204206
logging.debug("Registering debug route")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Blueprint
2+
from flask_jwt_extended import current_user
3+
from flask_jwt_extended import jwt_required
4+
from PyMatcha.models.notification import Notification
5+
from PyMatcha.utils.errors import BadRequestError
6+
from PyMatcha.utils.errors import NotFoundError
7+
from PyMatcha.utils.success import Success
8+
from PyMatcha.utils.success import SuccessOutput
9+
10+
notifications_bp = Blueprint("notifications", __name__)
11+
12+
13+
@notifications_bp.route("/notifications", methods=["GET"])
14+
@jwt_required
15+
def get_all_notifications():
16+
return SuccessOutput("notifications", current_user.get_all_notifications())
17+
18+
19+
@notifications_bp.route("/notifications/unread", methods=["GET"])
20+
@jwt_required
21+
def get_unread_notifications():
22+
return SuccessOutput("notifications", current_user.get_unread_notifications())
23+
24+
25+
@notifications_bp.route("/notifications/read/<n_id>", methods=["POST"])
26+
@jwt_required
27+
def mark_notification_as_read(n_id):
28+
notif = Notification.get(id=n_id)
29+
if not notif:
30+
raise NotFoundError(f"Notification id {n_id} not found.")
31+
if notif.user_id != current_user.id:
32+
raise BadRequestError("Cannot mark notification as read if it isn't yours.")
33+
notif.is_seen = True
34+
notif.save()
35+
return Success("Mark as read.")

0 commit comments

Comments
 (0)