rest
This commit is contained in:
parent
f321bf883e
commit
39d4fd1d7d
74
README.md
74
README.md
@ -19,31 +19,31 @@ Instrukcja uruchomienia web serwisu RESTowego na platformie AWS przy użyciu `AW
|
|||||||
- Kliknij przycisk `Create function`.
|
- Kliknij przycisk `Create function`.
|
||||||
- Wklej poniższy kod do edytora funkcji:
|
- Wklej poniższy kod do edytora funkcji:
|
||||||
```python
|
```python
|
||||||
import json
|
import json
|
||||||
import boto3
|
import boto3
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
dynamodb = boto3.resource("dynamodb")
|
dynamodb = boto3.resource("dynamodb")
|
||||||
table = dynamodb.Table('Posts')
|
table = dynamodb.Table('Posts')
|
||||||
|
|
||||||
|
|
||||||
def lambda_handler(event, context):
|
def lambda_handler(event, context):
|
||||||
print(event)
|
print(event)
|
||||||
body = {}
|
body = {}
|
||||||
statusCode = 200
|
statusCode = 200
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Endpoint: DELETE /posts/{id}
|
# Endpoint: DELETE /posts/{id}
|
||||||
if event['routeKey'] == "DELETE /posts/{id}":
|
if event['routeKey'] == "DELETE /posts/{id}":
|
||||||
table.delete_item(
|
table.delete_item(
|
||||||
Key={'id': event['pathParameters']['id']}
|
Key={'id': event['pathParameters']['id']} # id jako string
|
||||||
)
|
)
|
||||||
body = {"message": f"Deleted item with id {event['pathParameters']['id']}"}
|
body = {"message": f"Deleted item with id {event['pathParameters']['id']}"}
|
||||||
|
|
||||||
# Endpoint: GET /posts/{id}
|
# Endpoint: GET /posts/{id}
|
||||||
elif event['routeKey'] == "GET /posts/{id}":
|
elif event['routeKey'] == "GET /posts/{id}":
|
||||||
response = table.get_item(
|
response = table.get_item(
|
||||||
Key={'id': event['pathParameters']['id']}
|
Key={'id': event['pathParameters']['id']} # id jako string
|
||||||
)
|
)
|
||||||
if 'Item' in response:
|
if 'Item' in response:
|
||||||
body = response['Item']
|
body = response['Item']
|
||||||
@ -57,24 +57,37 @@ Instrukcja uruchomienia web serwisu RESTowego na platformie AWS przy użyciu `AW
|
|||||||
items = response.get('Items', [])
|
items = response.get('Items', [])
|
||||||
body = items
|
body = items
|
||||||
|
|
||||||
# Endpoint: POST /posts
|
# Endpoint: POST /posts (automatyczne generowanie id)
|
||||||
elif event['routeKey'] == "POST /posts":
|
elif event['routeKey'] == "POST /posts":
|
||||||
requestJSON = json.loads(event['body'])
|
requestJSON = json.loads(event['body'])
|
||||||
table.put_item(
|
|
||||||
Item={
|
# Pobierz największe istniejące id
|
||||||
'id': str(requestJSON['id']), # DynamoDB keys must be strings
|
response = table.scan()
|
||||||
'userId': str(requestJSON['userId']),
|
items = response.get('Items', [])
|
||||||
'title': requestJSON['title'],
|
max_id = max((int(item['id']) for item in items if item['id'].isdigit()), default=0)
|
||||||
'body': requestJSON['body']
|
|
||||||
}
|
# Wygeneruj nowe id
|
||||||
)
|
new_id = str(max_id + 1)
|
||||||
body = {"message": f"Created item with id {requestJSON['id']}"}
|
|
||||||
|
# Utwórz nowy wpis
|
||||||
|
new_item = {
|
||||||
|
'id': new_id,
|
||||||
|
'userId': str(requestJSON['userId']),
|
||||||
|
'title': requestJSON['title'],
|
||||||
|
'body': requestJSON['body']
|
||||||
|
}
|
||||||
|
table.put_item(Item=new_item)
|
||||||
|
|
||||||
|
# Zwróć utworzony obiekt w odpowiedzi
|
||||||
|
body = new_item
|
||||||
|
|
||||||
# Endpoint: PUT /posts/{id}
|
# Endpoint: PUT /posts/{id}
|
||||||
elif event['routeKey'] == "PUT /posts/{id}":
|
elif event['routeKey'] == "PUT /posts/{id}":
|
||||||
requestJSON = json.loads(event['body'])
|
requestJSON = json.loads(event['body'])
|
||||||
|
|
||||||
|
# Aktualizacja danych w tabeli
|
||||||
table.update_item(
|
table.update_item(
|
||||||
Key={'id': event['pathParameters']['id']},
|
Key={'id': event['pathParameters']['id']}, # id jako string
|
||||||
UpdateExpression="SET userId = :userId, title = :title, body = :body",
|
UpdateExpression="SET userId = :userId, title = :title, body = :body",
|
||||||
ExpressionAttributeValues={
|
ExpressionAttributeValues={
|
||||||
':userId': str(requestJSON['userId']),
|
':userId': str(requestJSON['userId']),
|
||||||
@ -82,7 +95,16 @@ Instrukcja uruchomienia web serwisu RESTowego na platformie AWS przy użyciu `AW
|
|||||||
':body': requestJSON['body']
|
':body': requestJSON['body']
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
body = {"message": f"Updated item with id {event['pathParameters']['id']}"}
|
|
||||||
|
# Pobranie zaktualizowanego obiektu
|
||||||
|
response = table.get_item(
|
||||||
|
Key={'id': event['pathParameters']['id']}
|
||||||
|
)
|
||||||
|
if 'Item' in response:
|
||||||
|
body = response['Item']
|
||||||
|
else:
|
||||||
|
statusCode = 404
|
||||||
|
body = {"error": "Updated item not found"}
|
||||||
|
|
||||||
# Unsupported route
|
# Unsupported route
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user