From fe899f97ad45694cab3fba666c69194df5300785 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Mon, 8 Jan 2024 12:56:45 +0200 Subject: [PATCH] user can be attend and unatted to events --- app.py | 1 + routes/eventRoutes.py | 27 +++++++++++++-- routes/userRoutes.py | 1 - services/EventService.py | 75 ++++++++++++++++++++++++++++++++++++++-- services/UserService.py | 4 +++ 5 files changed, 102 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index c45bb06..16404c7 100644 --- a/app.py +++ b/app.py @@ -58,4 +58,5 @@ app_class_instance = App() app_instance = app_class_instance.app if __name__ == '__main__': + app_class_instance.print_endpoints() app_class_instance.run() diff --git a/routes/eventRoutes.py b/routes/eventRoutes.py index cfc99eb..4946ea0 100644 --- a/routes/eventRoutes.py +++ b/routes/eventRoutes.py @@ -29,7 +29,7 @@ def create_event(): @authenticate_user def get_events(): try: - user_events = EventService.get_all_user_events(g.user_id) + user_events = EventService.get_upcoming_events(g.user_id) return {"events": user_events, "count": len(user_events)}, 200 except Exception as e: return {"error": str(e)}, 500 @@ -69,4 +69,27 @@ def delete_event(event_id): else: return jsonify({'error': 'Event not found'}), 404 except Exception as e: - return jsonify({'error': str(e)}), 500 \ No newline at end of file + return jsonify({'error': str(e)}), 500 + + +# User atended to event with id event_id and user must be loggedin +@eventRoutes.route('//attend', methods=['POST']) +@authenticate_user +def attend_event(event_id): + response = EventService.attend_event(event_id) + # Check if there's an error in the response + if 'error' in response: + return jsonify({'error': response['error']}), 400 # Or another appropriate status code + + # If no error, return success response + return jsonify({'message': response['message']}), 200 + +@eventRoutes.route('//unattend', methods=['POST']) +@authenticate_user +def unattend_event(event_id): + response = EventService.unattend_event(event_id) + # Check if there's an error in the response + if 'error' in response: + return jsonify({'error': response['error']}), 400 # Or another appropriate status code + + return jsonify({'message': response['message']}), 200 \ No newline at end of file diff --git a/routes/userRoutes.py b/routes/userRoutes.py index 6a8d912..0f2b39c 100644 --- a/routes/userRoutes.py +++ b/routes/userRoutes.py @@ -5,7 +5,6 @@ from middlewares.userMiddlewares import validate_user_post_request, validate_use userRoutes = Blueprint('userRoutes', __name__) - @userRoutes.route('/', methods=['GET']) @userRoutes.route('', methods=['GET']) def allUsers(): diff --git a/services/EventService.py b/services/EventService.py index 42e9720..6723d24 100644 --- a/services/EventService.py +++ b/services/EventService.py @@ -1,5 +1,8 @@ -from models import db, Event +from sqlalchemy.sql import exists +from models import db, Event, user_event_association +from services.UserService import UserService from datetime import datetime +from flask import g class EventService: @staticmethod @@ -16,8 +19,8 @@ class EventService: return new_event @staticmethod - def get_all_user_events(user_id): - events=Event.query.filter_by(user_id=user_id, deleted=False).all() + def get_upcoming_events(user_id): + events=Event.query.filter_by(deleted=False).all() if(events): return [event.to_dict() for event in events] else: @@ -52,3 +55,69 @@ class EventService: else: return None return event + + + @staticmethod + def attend_event(event_id): + user_id = g.user_id # Assuming user_id is stored in Flask's global g object + # Check if the event is valid and in the future + event = Event.query.filter( + Event.id == event_id, + Event.duedate > datetime.now(), + Event.deleted == False + ).first() + + if not event: + return {'error': 'Event not found or already passed'} + + # Check if the user is already associated with the event + is_already_attending = db.session.query(exists().where( + user_event_association.c.user_id == user_id, + user_event_association.c.event_id == event_id + )).scalar() + + if is_already_attending: + return {'error': 'User already attending this event'} + + # Add the user to the event + user = UserService.get_user_by_id(user_id) + if not user: + return {'error': 'User not found'} + + event.users.append(user) + db.session.commit() + + return {'message': 'User successfully added to the event'} + + + @staticmethod + def unattend_event(event_id): + user_id = g.user_id # Assuming user_id is stored in Flask's global g object + # Check if the event is valid and in the future + event = Event.query.filter( + Event.id == event_id, + Event.duedate > datetime.now(), + Event.deleted == False + ).first() + + if not event: + return {'error': 'Event not found or already passed'} + + # Check if the user is already associated with the event + is_already_attending = db.session.query(exists().where( + user_event_association.c.user_id == user_id, + user_event_association.c.event_id == event_id + )).scalar() + + if not is_already_attending: + return {'error': 'User not attending this event'} + + # Remove the user from the event + user = UserService.get_user_by_id(user_id) + if not user: + return {'error': 'User not found'} + + event.users.remove(user) + db.session.commit() + + return {'message': 'User successfully removed from the event'} \ No newline at end of file diff --git a/services/UserService.py b/services/UserService.py index 7ce4d4a..22f11cb 100644 --- a/services/UserService.py +++ b/services/UserService.py @@ -24,6 +24,10 @@ class UserService: @staticmethod def get_user_by_email(email): return User.query.filter_by(email=email).first() + + @staticmethod + def get_user_by_id(user_id): + return User.query.get(user_id) @staticmethod def verify_user(data):