favorite_transport_create
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("map_filter")
def lambda_handler(event, context):
count = table.scan()["Count"]
if count == 3:
return {'isFull': 1}
First = event["body-json"]["First"]
Second = event["body-json"]["Second"]
Third = event["body-json"]["Third"]
Year = event["body-json"]["Year"]
Emissions = event["body-json"]["Emissions"]
response = table.put_item(
Item = {
'idx': count,
'First': First,
'Second': Second,
'Third': Third,
'Year': Year,
'Emissions': Emissions,
}
)
response["isFull"] = 0
return response
POST
favorite_transport_delete
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("map_filter")
def idx_sort():
count = table.scan()['Count']
items = table.scan()['Items']
if count == 2:
tmp = table.scan()['Items']
for t in tmp:
table.delete_item(
Key = {
'idx': t['idx']
}
)
items[1]['idx'] = 0
items[0]['idx'] = 1
table.put_item(
Item = items[0]
)
table.put_item(
Item = items[1]
)
elif count == 1:
tmp = table.scan()['Items']
for t in tmp:
table.delete_item(
Key = {
'idx': t['idx']
}
)
items[0]['idx'] = 0
table.put_item(
Item = items[0]
)
else: # count == 0
return 0
def lambda_handler(event, context):
idx = int(event["body-json"]["idx"])
table.delete_item(
Key={
'idx' : idx
}
)
idx_sort()
return 0
DELETE
favorite_transport_read
import boto3
import json
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table = dynamodb.Table("map_filter")
def lambda_handler(event, context):
response = table.scan()
item = response["Items"]
return item
READ