-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_store.py
55 lines (35 loc) · 1.63 KB
/
scanner_store.py
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
import boto3
import json
from utils import send_response
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
print("event: ", event)
print("context: ", context)
if event["resource"] == "/addtocart/{userId}":
print("this is an add to cart operation")
product_id = event["pathParameters"]["userId"]
products_table = dynamodb.Table("rfid_store")
cart = dynamodb.Table("cart")
res = products_table.get_item(Key = {"product_id": product_id})
requested_item = res['Item']
print("requested item: ", requested_item)
temp = requested_item["product_id"]
requested_item["id"] = temp
del requested_item["product_id"]
if not requested_item:
return send_response({"message": 'no product found'}, 401)
# cart.put_item(Item= requested_item, Key = {"product_id": requested_item["product_id"]})
cart.put_item(Item = requested_item)
return send_response({"message": "item added to cart"}, 200)
if event["resource"] == "/getitem/{itemId}":
product_id = event["pathParameters"]["itemId"]
products_table = dynamodb.Table("rfid_store")
res = products_table.get_item(Key={
'product_id': product_id,
})
item = res['Item']
if not item:
return send_response({"message": "no product found"}, 401)
print("dynamo item: ", item)
return send_response(item, 200)
return send_response("unhandled route", "500")