-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from CSE2102-Fall23/schedule
Schedule
- Loading branch information
Showing
4 changed files
with
310 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)}' | ||
} | ||
} |
Oops, something went wrong.