Skip to content

Commit

Permalink
Merge pull request #9 from CSE2102-Fall23/schedule
Browse files Browse the repository at this point in the history
Schedule
  • Loading branch information
jap19015 authored Dec 2, 2023
2 parents d3c20f2 + 4aff227 commit c5b9743
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 99 deletions.
50 changes: 50 additions & 0 deletions lambdafuncs/lambda_enroll.py
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)})
}
62 changes: 62 additions & 0 deletions lambdafuncs/lambda_searchCourse.py
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'})
}
46 changes: 46 additions & 0 deletions lambdafuncs/lambda_searchUsers.py
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)}'
}
}
Loading

0 comments on commit c5b9743

Please sign in to comment.