From d2ed29dbe2155277588d4c0edaab938074b464eb Mon Sep 17 00:00:00 2001 From: Jack Psaras Date: Sat, 25 Nov 2023 21:57:31 -0500 Subject: [PATCH] added lambda functions + made schedule searcher --- lambdafuncs/lambda_enroll.py | 50 ++++++++++++++++++++++++ lambdafuncs/lambda_searchCourse.py | 62 ++++++++++++++++++++++++++++++ lambdafuncs/lambda_searchUsers.py | 46 ++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 lambdafuncs/lambda_enroll.py create mode 100644 lambdafuncs/lambda_searchCourse.py create mode 100644 lambdafuncs/lambda_searchUsers.py diff --git a/lambdafuncs/lambda_enroll.py b/lambdafuncs/lambda_enroll.py new file mode 100644 index 0000000..e4de760 --- /dev/null +++ b/lambdafuncs/lambda_enroll.py @@ -0,0 +1,50 @@ +import json +import boto3 + +# Initialize the DynamoDB client +dynamodb = boto3.client('dynamodb') + +# DynamoDB table name +table_name = 'enroll_course' + +def lambda_handler(event, context): + # Extract the JSON data from the event + try: + if 'body' in event: + course_data = json.loads(event['body']) + else: + course_data = event + except Exception as e: + return { + 'statusCode': 400, + 'body': json.dumps({'error': 'Invalid JSON format in the request body'}) + } + + # Check if the required fields are present in the request + if 'name' not in course_data or 'courseName' not in course_data: + return { + 'statusCode': 400, + 'body': json.dumps({'error': 'Required fields ("name" and "courseName") are missing in the request body'}) + } + + # Create an item for the DynamoDB table + item = { + 'name': {'S': course_data['name']}, + 'courseName': {'S': course_data['courseName']} + } + + # Put the item into the DynamoDB table + try: + dynamodb.put_item( + TableName=table_name, + Item=item + ) + return { + 'statusCode': 200, + 'body': json.dumps({'message': 'Course added successfully'}) + } + except Exception as e: + return { + 'statusCode': 500, + 'body': json.dumps({'error': str(e)}) + } diff --git a/lambdafuncs/lambda_searchCourse.py b/lambdafuncs/lambda_searchCourse.py new file mode 100644 index 0000000..68bff93 --- /dev/null +++ b/lambdafuncs/lambda_searchCourse.py @@ -0,0 +1,62 @@ +import boto3 +import json + +# Initialize the DynamoDB client +dynamodb = boto3.client('dynamodb') + +# DynamoDB table name +table_name = 'course_createDB' + +def lambda_handler(event, context): + # Extract 'name' parameter from the query string + if event.get('queryStringParameters') and 'name' in event['queryStringParameters']: + name = event['queryStringParameters']['name'] + try: + response = dynamodb.get_item( + TableName=table_name, + Key={'name': {'S': name}} + ) + item = response.get('Item', {}) + if not item: + return { + 'statusCode': 404, + 'headers': { + 'Access-Control-Allow-Origin': 'http://localhost:5173', # Replace with your frontend URL + 'Access-Control-Allow-Methods': 'GET', # Adjust based on allowed methods + 'Access-Control-Allow-Headers': 'Content-Type', # Include necessary headers + 'Access-Control-Allow-Credentials': 'true', # Include if credentials (cookies, etc.) are needed + }, + 'body': json.dumps({'error': 'Item not found'}) + } + return { + 'statusCode': 200, + 'headers': { + 'Access-Control-Allow-Origin': 'http://localhost:5173', # Replace with your frontend URL + 'Access-Control-Allow-Methods': 'GET', # Adjust based on allowed methods + 'Access-Control-Allow-Headers': 'Content-Type', # Include necessary headers + 'Access-Control-Allow-Credentials': 'true', # Include if credentials (cookies, etc.) are needed + }, + 'body': json.dumps(item) + } + except Exception as e: + return { + 'statusCode': 500, + 'headers': { + 'Access-Control-Allow-Origin': 'http://localhost:5173', # Replace with your frontend URL + 'Access-Control-Allow-Methods': 'GET', # Adjust based on allowed methods + 'Access-Control-Allow-Headers': 'Content-Type', # Include necessary headers + 'Access-Control-Allow-Credentials': 'true', # Include if credentials (cookies, etc.) are needed + }, + 'body': json.dumps({'error': str(e)}) + } + else: + return { + 'statusCode': 400, + 'headers': { + 'Access-Control-Allow-Origin': 'http://localhost:5173', # Replace with your frontend URL + 'Access-Control-Allow-Methods': 'GET', # Adjust based on allowed methods + 'Access-Control-Allow-Headers': 'Content-Type', # Include necessary headers + 'Access-Control-Allow-Credentials': 'true', # Include if credentials (cookies, etc.) are needed + }, + 'body': json.dumps({'error': 'Missing or invalid "name" parameter in the query string'}) + } diff --git a/lambdafuncs/lambda_searchUsers.py b/lambdafuncs/lambda_searchUsers.py new file mode 100644 index 0000000..b47ebf3 --- /dev/null +++ b/lambdafuncs/lambda_searchUsers.py @@ -0,0 +1,46 @@ +import boto3 + +def lambda_handler(event, context): + # Assuming the partition key value is provided in the event + partition_key_value = event['name'] + + # Creating a DynamoDB client + dynamodb = boto3.resource('dynamodb') + + # Specify the table name + table_name = 'enroll_course' + + # Get a reference to the table + table = dynamodb.Table(table_name) + + try: + # Query the table using the partition key + response = table.query( + KeyConditionExpression=boto3.dynamodb.conditions.Key('name').eq(partition_key_value) + ) + + # Print the items found + items = response['Items'] + print(items) + + # You can do further processing with the items here + + # Modify the response to include the items + return { + 'statusCode': 200, + 'body': { + 'message': 'Query executed successfully!', + 'items': items + } + } + + except Exception as e: + print(f"Error querying DynamoDB table: {e}") + + # Modify the response to include the error message + return { + 'statusCode': 500, + 'body': { + 'message': f'Error querying DynamoDB table: {str(e)}' + } + }