Spaces:
Configuration error

File size: 2,048 Bytes
681c53e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
import requests
import boto3
import uuid
from datetime import datetime
from init import dynamodb_service

def send_data_to_dynamodb(user_data):
    try:
        lambda_url = 'https://n0u3bakgmf.execute-api.us-east-1.amazonaws.com/dev/post-item'
        
        headers = {
            'Content-Type': 'application/json'
        }

        
        response = requests.post(lambda_url, data=json.dumps(user_data), headers=headers)

        if response.status_code == 200:
            print("Data sent to DynamoDB successfully!")
            return response.json()
        else:
            print(f"Error: {response.status_code}, {response.text}")
            return None

    except Exception as e:
        print(f"An error occurred while sending data: {str(e)}")
        return None

def lambda_handler(event, context):
    try:
        
        body = json.loads(event['body'])
        
        title = body.get('title')
        keywords = body.get('keywords')

        
        if not title or not keywords:
            return {
                'statusCode': 400,
                'body': json.dumps({
                    'message': 'Missing title or keywords'
                })
            }

        
        item_id = str(uuid.uuid4())
        timestamp = datetime.now().isoformat()

        
        response = dynamodb_service.put_item(
            Item={
                'id': item_id,
                'title': title,
                'keywords': keywords,
                'createdAt': timestamp
            }
        )

        
        return {
            'statusCode': 200,
            'body': json.dumps({
                'message': 'Data successfully inserted',
                'itemId': item_id
            })
        }

    except Exception as e:
        
        return {
            'statusCode': 500,
            'body': json.dumps({
                'message': 'An error occurred',
                'error': str(e)
            })
        }