user can be attend and unatted to events
This commit is contained in:
parent
536dc0fd97
commit
fe899f97ad
5 changed files with 102 additions and 6 deletions
1
app.py
1
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()
|
||||
|
|
|
@ -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
|
||||
|
@ -70,3 +70,26 @@ def delete_event(event_id):
|
|||
return jsonify({'error': 'Event not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
# User atended to event with id event_id and user must be loggedin
|
||||
@eventRoutes.route('/<int:event_id>/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('/<int:event_id>/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
|
|
@ -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():
|
||||
|
|
|
@ -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'}
|
|
@ -25,6 +25,10 @@ class UserService:
|
|||
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):
|
||||
user = UserService.get_user_by_email(data['email'])
|
||||
|
|
Loading…
Reference in a new issue