Skip to content

Commit

Permalink
add profile page backend functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
tharoosha committed Oct 29, 2024
1 parent 74075fd commit b433c4a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 3 deletions.
11 changes: 11 additions & 0 deletions backend/app/recommendation/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ def add_game(game_name, genre):



def get_game_details(game_id):
# Fetch the game by ID from your database (assuming you have a `Game` model)
game = Game.nodes.get(gameId=game_id)
if game:
return {
"gameId": game.gameId,
"game": game.game,
"genre": game.genre
}
else:
return {"error": "Game not found"}
15 changes: 15 additions & 0 deletions backend/app/recommendation/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ def add_player_relationships(player_id, games):
return relationships


def get_player_relationships(player_id):
# Find the player node
player = Player.nodes.get(playerId=player_id)

relationships = []

# Retrieve all games the player has a 'PLAYS' relationship with
for game in player.plays:
relationships.append({
"playerId": player.playerId,
"gameId": game.gameId
})

return relationships


29 changes: 29 additions & 0 deletions backend/app/routes/event_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,32 @@ 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():
# Get the user ID from the JWT token
user_id = get_jwt_identity()

try:
# Query the database for events registered by this user
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
]

return jsonify(registered_events), 200

except Exception as e:
return jsonify({"error": str(e)}), 500
50 changes: 50 additions & 0 deletions backend/app/routes/recommendation_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,56 @@ def add_relation():
relationships = player.add_player_relationships( player_id, games)
return jsonify(relationships), 201

# @recommendation_blueprint.route('/getrelationship', methods=['GET'])
# def get_relationship():
# player_id = request.args.get('player_id', type=int)

# if not player_id:
# return jsonify({"error": "Missing player ID"}), 400

# try:
# relationships = player.get_player_relationships(player_id)
# except Exception as e:
# return jsonify({"error": str(e)}), 500

# return jsonify(relationships), 200

@recommendation_blueprint.route('/getrelationship', methods=['GET'])
def get_relationship():
player_id = request.args.get('player_id', type=int)

if not player_id:
return jsonify({"error": "Missing player ID"}), 400

try:
# Get the relationships for the player
relationships = player.get_player_relationships(player_id)

# Prepare a list to store relationships with game details
detailed_relationships = []

# Loop through each relationship to fetch game details
for relationship in relationships:
game_id = relationship.get("gameId")

# Fetch the game details (assuming `get_game_details` function exists)
game_details = game.get_game_details(game_id) # You may need to implement this function

# Combine relationship and game details
detailed_relationship = {
"gameId": game_id,
"playerId": relationship.get("playerId"),
"game": game_details.get("game"),
"genre": game_details.get("genre")
}
detailed_relationships.append(detailed_relationship)

except Exception as e:
return jsonify({"error": str(e)}), 500

return jsonify(detailed_relationships), 200


@recommendation_blueprint.route('/getallgames')
def get_all_games():
games = get_games.get_all_games()
Expand Down
72 changes: 69 additions & 3 deletions backend/app/routes/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

# @user_blueprint.route('/signup', methods=['POST', 'OPTIONS'])
@user_blueprint.route('/signup', methods=['POST'])

# @cross_origin(origin='*')
def signup_user():
data = request.get_json()
email = data.get('email')
Expand Down Expand Up @@ -108,4 +106,72 @@ def validate_user(user_id):
if user:
return jsonify({'message': 'User is valid', 'user_id': user_id}), 200
else:
return jsonify({'message': 'User not found'}), 404
return jsonify({'message': 'User not found'}), 404

@user_blueprint.route('/user/<int:user_id>', methods=['GET'])
@jwt_required()
def get_user_details(user_id):
# Retrieve the user from the database
user = Player.query.get(user_id)
print(user.userid)
if not user:
return jsonify({'message': 'User not found'}), 404

# Return the user details
return jsonify({
'user_id': user.userid,
'email': user.email,
'nickname': user.nickname,
# 'country': user.country,
# 'teamid': user.teamid
}), 200

@user_blueprint.route('/user/<int:user_id>', methods=['PUT'])
@jwt_required()
def update_user_details(user_id):
# Ensure the user can only update their own information
current_user_id = get_jwt_identity()
if current_user_id != user_id:
return jsonify({'message': 'Unauthorized access'}), 403

# Get the user from the database
user = Player.query.get(user_id)
if not user:
return jsonify({'message': 'User not found'}), 404

# Get the update data from the request
data = request.get_json()
nickname = data.get('nickname')
email = data.get('email')
# country = data.get('country')
# team_id = data.get('teamid')

# Update fields if they are provided in the request
if nickname:
user.nickname = nickname
if email:
# Check if the email is already in use by another user
existing_user = Player.query.filter_by(email=email).first()
if existing_user and existing_user.userid != user_id:
return jsonify({'message': 'Email is already in use'}), 409
user.email = email
# if country:
# user.country = country
# if team_id is not None:
# # Validate if the team ID exists
# team = Team.query.get(team_id)
# if not team:
# return jsonify({'message': 'Invalid team ID'}), 404
# user.teamid = team_id

# Commit the updates to the database
db.session.commit()

# Return the updated user details
return jsonify({
'user_id': user.userid,
'email': user.email,
'nickname': user.nickname,
# 'country': user.country,
# 'teamid': user.teamid
}), 200

0 comments on commit b433c4a

Please sign in to comment.