-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddEvent.py
More file actions
125 lines (112 loc) · 4.51 KB
/
addEvent.py
File metadata and controls
125 lines (112 loc) · 4.51 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import boto3
import uuid
import boto3.dynamodb.conditions as conditions
from botocore.exceptions import ClientError
import datetime
def lambda_handler(event, context):
# this will create dynamodb resource object and
# here dynamodb is resource name
client = boto3.resource('dynamodb')
#generate UUID
recordId = str(uuid.uuid4()).replace("-", "")
# this will search for dynamoDB table
# your table name may be different
table = client.Table("events")
userTable = client.Table("users")
usersAttending = []
usersMaybe = []
if event['queryStringParameters']['RSVP'] == "Attending":
usersAttending.append(event['queryStringParameters']['organizer'])
try:
userTable.update_item(
Key={
"userID": event['queryStringParameters']['organizer'],
},
UpdateExpression="SET #o = list_append(#o, :eventID)",
ExpressionAttributeNames={
"#o": "eventsAttending",
},
ExpressionAttributeValues={
":eventID": [recordId]
},
ConditionExpression=conditions.Attr("userID").exists()
)
except ClientError as e:
return {
'statusCode': 500,
'body': "Server Failed to Update User Table (eventsAttending)"
}
elif event['queryStringParameters']['RSVP'] == "Maybe":
usersMaybe.append(event['queryStringParameters']['organizer'])
try:
userTable.update_item(
Key={
"userID": event['queryStringParameters']['organizer'],
},
UpdateExpression="SET #o = list_append(#o, :eventID)",
ExpressionAttributeNames={
"#o": "eventsMaybe",
},
ExpressionAttributeValues={
":eventID": [recordId]
},
ConditionExpression=conditions.Attr("userID").exists()
)
except ClientError as e:
return {
'statusCode': 500,
'body': "Server Failed to Update User Table (eventsMaybe)"
}
count = 0
success = False
while count<3:
try:
table.put_item(
Item={
"eventID" : recordId,
"eventName" : event['queryStringParameters']['eventName'],
"organizer" : event['queryStringParameters']['organizer'],
"locationCords" : event['queryStringParameters']['locationCords'],
"city" : event['queryStringParameters']['city'],
"country" : event['queryStringParameters']['country'],
"entryFee" : event['queryStringParameters']['entryFee'],
"dressCode" : event['queryStringParameters']['dressCode'],
"description" : event['queryStringParameters']['description'],
"datetime" : str(datetime.datetime.utcnow()),
"iconPictureAddress" : "https://stumeetwebapp.s3.eu-central-1.amazonaws.com/event-profile-pics/" + recordId + ".jpg",
"usersAttending" : usersAttending,
"usersMaybe" : usersMaybe,
"postIDs" : []
},
ConditionExpression='attribute_not_exists(eventID)'
)
try:
userTable.update_item(
Key={
"userID": event['queryStringParameters']['organizer'],
},
UpdateExpression="SET #o = list_append(#o, :eventID)",
ExpressionAttributeNames={
"#o": "eventsOrganized",
},
ExpressionAttributeValues={
":eventID": [recordId]
},
ConditionExpression=conditions.Attr("userID").exists()
)
except ClientError as e:
return {
'statusCode': 500,
'body': "Server Failed to Update User Table (eventsOrganized)"
}
return {
'statusCode': 200,
'body': recordId
}
except ClientError as e:
recordId = str(uuid.uuid4()).replace("-", "")
count += 1
return {
'statusCode': 500,
'body': "Server Failed to Generate Unique ID. Please Try Again."
}