-
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.
created new working api and lambda func
- Loading branch information
Showing
2 changed files
with
54 additions
and
23 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
import json | ||
import boto3 | ||
|
||
# Initialize the DynamoDB client | ||
dynamodb = boto3.client('dynamodb') | ||
|
||
# DynamoDB table name | ||
table_name = 'course_createDB' | ||
|
||
def lambda_handler(event, context): | ||
# Extract the JSON data from the POST request | ||
try: | ||
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 'professor' not in course_data or 'days' not in course_data or 'time' not in course_data or 'seats' not in course_data or 'location' not in course_data: | ||
return { | ||
'statusCode': 401, | ||
'body': json.dumps({'error': 'Required fields are missing in the request body'}) | ||
} | ||
|
||
# Create an item for the DynamoDB table | ||
item = { | ||
'name': {'S': course_data['name']}, | ||
'professor': {'S': course_data['professor']}, | ||
'days': {'S': course_data['days']}, | ||
'time': {'S': course_data['time']}, | ||
'seats': {'S': course_data['seats']}, | ||
'location': {'S': course_data['location']} | ||
} | ||
|
||
for key, value in course_data.items(): | ||
item[key] = {'S' if isinstance(value, str) else 'N': str(value)} | ||
|
||
# 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)}) | ||
} |