|
23 | 23 | from src.models.report import Report as ReportModel |
24 | 24 | from src.models.hourly_average_capacity import HourlyAverageCapacity as HourlyAverageCapacityModel |
25 | 25 | from src.database import db_session |
26 | | - |
| 26 | +import requests |
| 27 | +import json |
| 28 | +import os |
27 | 29 |
|
28 | 30 | # MARK: - Gym |
29 | 31 |
|
@@ -427,21 +429,87 @@ class Arguments: |
427 | 429 | name = graphene.String(required=True) |
428 | 430 | net_id = graphene.String(required=True) |
429 | 431 | email = graphene.String(required=True) |
| 432 | + encoded_image = graphene.String(required=False) |
430 | 433 |
|
431 | 434 | Output = User |
432 | 435 |
|
433 | | - def mutate(self, info, name, net_id, email): |
| 436 | + def mutate(self, info, name, net_id, email, encoded_image=None): |
434 | 437 | # Check if a user with the given NetID already exists |
435 | 438 | existing_user = db_session.query(UserModel).filter(UserModel.net_id == net_id).first() |
| 439 | + final_photo_url = None |
436 | 440 | if existing_user: |
437 | 441 | raise GraphQLError("NetID already exists.") |
438 | 442 |
|
439 | | - new_user = UserModel(name=name, net_id=net_id, email=email) |
| 443 | + if encoded_image: |
| 444 | + upload_url = os.getenv("DIGITAL_OCEAN_URL") |
| 445 | + payload = { |
| 446 | + "bucket": os.getenv("BUCKET_NAME"), |
| 447 | + "image": encoded_image # Base64-encoded image string |
| 448 | + } |
| 449 | + headers = {"Content-Type": "application/json"} |
| 450 | + try: |
| 451 | + response = requests.post(upload_url, json=payload, headers=headers) |
| 452 | + response.raise_for_status() |
| 453 | + json_response = response.json() |
| 454 | + final_photo_url = json_response.get("data") |
| 455 | + if not final_photo_url: |
| 456 | + raise GraphQLError("No URL returned from upload service.") |
| 457 | + except requests.exceptions.RequestException as e: |
| 458 | + print(f"Request failed: {e}") |
| 459 | + raise GraphQLError("Failed to upload photo.") |
| 460 | + |
| 461 | + new_user = UserModel(name=name, net_id=net_id, email=email, encoded_image=final_photo_url) |
440 | 462 | db_session.add(new_user) |
441 | 463 | db_session.commit() |
442 | 464 |
|
443 | 465 | return new_user |
| 466 | + |
| 467 | +class EditUser(graphene.Mutation): |
| 468 | + class Arguments: |
| 469 | + name = graphene.String(required=False) |
| 470 | + net_id = graphene.String(required=True) |
| 471 | + email = graphene.String(required=False) |
| 472 | + encoded_image = graphene.String(required=False) |
444 | 473 |
|
| 474 | + Output = User |
| 475 | + |
| 476 | + def mutate(self, info, net_id, name=None, email=None, encoded_image=None): |
| 477 | + existing_user = db_session.query(UserModel).filter(UserModel.net_id == net_id).first() |
| 478 | + if not existing_user: |
| 479 | + raise GraphQLError("User with given net id does not exist.") |
| 480 | + |
| 481 | + if name is not None: |
| 482 | + existing_user.name = name |
| 483 | + if email is not None: |
| 484 | + existing_user.email = email |
| 485 | + if encoded_image is not None: |
| 486 | + upload_url = os.getenv("DIGITAL_OCEAN_URL") # Base URL for upload endpoint |
| 487 | + if not upload_url: |
| 488 | + raise GraphQLError("Upload URL not configured.") |
| 489 | + |
| 490 | + payload = { |
| 491 | + "bucket": os.getenv("BUCKET_NAME", "DEV_BUCKET"), |
| 492 | + "image": encoded_image # Base64-encoded image string |
| 493 | + } |
| 494 | + headers = {"Content-Type": "application/json"} |
| 495 | + |
| 496 | + print(f"Uploading image with payload: {payload}") |
| 497 | + |
| 498 | + try: |
| 499 | + response = requests.post(upload_url, json=payload, headers=headers) |
| 500 | + response.raise_for_status() |
| 501 | + json_response = response.json() |
| 502 | + print(f"Upload API response: {json_response}") |
| 503 | + final_photo_url = json_response.get("data") |
| 504 | + if not final_photo_url: |
| 505 | + raise GraphQLError("No URL returned from upload service.") |
| 506 | + existing_user.encoded_image = final_photo_url |
| 507 | + except requests.exceptions.RequestException as e: |
| 508 | + print(f"Request failed: {e}") |
| 509 | + raise GraphQLError("Failed to upload photo.") |
| 510 | + |
| 511 | + db_session.commit() |
| 512 | + return existing_user |
445 | 513 |
|
446 | 514 | class EnterGiveaway(graphene.Mutation): |
447 | 515 | class Arguments: |
@@ -598,6 +666,7 @@ def mutate(self, info, user_id): |
598 | 666 | class Mutation(graphene.ObjectType): |
599 | 667 | create_giveaway = CreateGiveaway.Field(description="Creates a new giveaway.") |
600 | 668 | create_user = CreateUser.Field(description="Creates a new user.") |
| 669 | + edit_user = EditUser.Field(description="Edit a new user.") |
601 | 670 | enter_giveaway = EnterGiveaway.Field(description="Enters a user into a giveaway.") |
602 | 671 | set_workout_goals = SetWorkoutGoals.Field(description="Set a user's workout goals.") |
603 | 672 | log_workout = logWorkout.Field(description="Log a user's workout.") |
|
0 commit comments