From b5bb111a3c0cba8ffea6d8aa96a6c4c118dc80f4 Mon Sep 17 00:00:00 2001 From: tharoosha Date: Sat, 2 Nov 2024 09:22:40 +0530 Subject: [PATCH] update event registration table and event routes --- backend/app/models/sql_models.py | 3 + backend/app/routes/event_routes.py | 116 +++++++++++++++--- ..._add_universityid_and_batchid_to_event_.py | 34 +++++ 3 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 backend/migrations/versions/652ccca54df2_add_universityid_and_batchid_to_event_.py diff --git a/backend/app/models/sql_models.py b/backend/app/models/sql_models.py index 79b81ab..da8d44e 100644 --- a/backend/app/models/sql_models.py +++ b/backend/app/models/sql_models.py @@ -57,3 +57,6 @@ class EventRegistration(db.Model): PhoneNumber = db.Column(db.String(20), nullable=True) DateOfBirth = db.Column(db.Date, nullable=True) Gender = db.Column(db.String(10), nullable=True) + + UniversityID = db.Column(db.String(50), nullable=True) + BatchID = db.Column(db.String(50), nullable=True) diff --git a/backend/app/routes/event_routes.py b/backend/app/routes/event_routes.py index 9ad98ac..5184852 100644 --- a/backend/app/routes/event_routes.py +++ b/backend/app/routes/event_routes.py @@ -99,6 +99,8 @@ def register_event(): phone_number = data.get('phone_number') date_of_birth = data.get('date_of_birth') gender = data.get('gender') + batch_id = data.get('batch_id') + university_id = data.get('university_id') # Get admin ID from JWT token user_id = get_jwt_identity() @@ -113,7 +115,9 @@ def register_event(): Country=country, PhoneNumber=phone_number, DateOfBirth=date_of_birth, - Gender=gender + Gender=gender, + BatchID=batch_id, + UniversityID=university_id ) if team_name is not None: @@ -329,7 +333,9 @@ def get_event_registrations(event_id): 'nickname': player.nickname, 'date_of_birth': reg.DateOfBirth, 'gender': reg.Gender, - 'phone_number': reg.PhoneNumber + 'phone_number': reg.PhoneNumber, + 'batch_id': reg.BatchID, + 'university_id': reg.UniversityID }) # Return the list of registration details as JSON response @@ -343,6 +349,7 @@ def get_event_registrations(event_id): return jsonify({ 'error': str(e) }), 500 + @event_blueprint.route('/user_events', methods=['GET']) @user_required def get_registered_events(): @@ -354,21 +361,100 @@ def get_registered_events(): registrations = EventRegistration.query.filter_by(UserID=user_id).all() # Format the response - registered_events = [ - { - 'registration_id': reg.RegistrationID, - 'event_id': reg.EventID, - 'country': reg.Country, - 'phone_number': reg.PhoneNumber, - 'date_of_birth': reg.DateOfBirth, - 'gender': reg.Gender, - 'registered_date': reg.RegisteredDate, - # 'team_id': reg.teamid - } - for reg in registrations - ] + registered_events = [] + for reg in registrations: + try: + # Query the database for the event by event ID for each registration + event = Event.query.filter_by(eventid=reg.EventID).first() + + # Check if the event exists + if event is None: + continue # Skip this registration if the event doesn't exist + + # Append the formatted registration data to the list + registered_events.append({ + 'registration_id': reg.RegistrationID, + 'event_id': reg.EventID, + 'gamename': event.gamename, + 'country': reg.Country, + 'phone_number': reg.PhoneNumber, + 'date_of_birth': reg.DateOfBirth, + 'gender': reg.Gender, + 'registered_date': reg.RegisteredDate, + 'batch_id': reg.BatchID, + 'university_id': reg.UniversityID + # 'team_id': reg.teamid + }) + + except Exception as e: + return jsonify({"error": f"Error processing event ID {reg.EventID}: {str(e)}"}), 500 return jsonify(registered_events), 200 except Exception as e: return jsonify({"error": str(e)}), 500 + +@event_blueprint.route('/update_user_event/', methods=['PUT']) +@user_required +def update_event_registration(registration_id): + # Get the user ID from the JWT token + user_id = get_jwt_identity() + + try: + # Query the registration to update + registration = EventRegistration.query.filter_by(RegistrationID=registration_id, UserID=user_id).first() + + # Check if the registration exists + if registration is None: + return jsonify({"message": "Registration not found"}), 404 + + # Get the request data + data = request.get_json() + + # Update fields as provided in the request + if 'country' in data: + registration.Country = data['country'] + if 'phone_number' in data: + registration.PhoneNumber = data['phone_number'] + if 'date_of_birth' in data: + registration.DateOfBirth = data['date_of_birth'] + if 'gender' in data: + registration.Gender = data['gender'] + if 'university_id' in data: + registration.UniversityID = data['university_id'] + if 'batch_id' in data: + registration.BatchID = data['batch_id'] + + # Commit the changes to the database + db.session.commit() + + return jsonify({"message": "Registration updated successfully"}), 200 + + except Exception as e: + return jsonify({"error": str(e)}), 500 + +@event_blueprint.route('/delete_user_event/', methods=['DELETE']) +@user_required +def delete_event_registration(registration_id): + # Get the user ID from the JWT token + user_id = get_jwt_identity() + + try: + # Query the registration to delete + registration = EventRegistration.query.filter_by(RegistrationID=registration_id, UserID=user_id).first() + + # Check if the registration exists + if registration is None: + return jsonify({"message": "Registration not found"}), 404 + + # Delete the registration + db.session.delete(registration) + db.session.commit() + + return jsonify({"message": "Registration deleted successfully"}), 200 + + except Exception as e: + return jsonify({"error": str(e)}), 500 + + + diff --git a/backend/migrations/versions/652ccca54df2_add_universityid_and_batchid_to_event_.py b/backend/migrations/versions/652ccca54df2_add_universityid_and_batchid_to_event_.py new file mode 100644 index 0000000..70b1cac --- /dev/null +++ b/backend/migrations/versions/652ccca54df2_add_universityid_and_batchid_to_event_.py @@ -0,0 +1,34 @@ +"""Add UniversityID and BatchID to event_registration + +Revision ID: 652ccca54df2 +Revises: 2d8467e1b8cf +Create Date: 2024-11-02 09:05:46.507060 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '652ccca54df2' +down_revision = '2d8467e1b8cf' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('event_registration', schema=None) as batch_op: + batch_op.add_column(sa.Column('UniversityID', sa.String(length=50), nullable=True)) + batch_op.add_column(sa.Column('BatchID', sa.String(length=50), nullable=True)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('event_registration', schema=None) as batch_op: + batch_op.drop_column('BatchID') + batch_op.drop_column('UniversityID') + + # ### end Alembic commands ###