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`.
|
||||
- Wklej poniższy kod do edytora funkcji:
|
||||
```python
|
||||
import json
|
||||
import boto3
|
||||
from decimal import Decimal
|
||||
|
||||
dynamodb = boto3.resource("dynamodb")
|
||||
table = dynamodb.Table('Posts')
|
||||
|
||||
|
||||
def lambda_handler(event, context):
|
||||
print(event)
|
||||
body = {}
|
||||
statusCode = 200
|
||||
import json
|
||||
import boto3
|
||||
from decimal import Decimal
|
||||
|
||||
dynamodb = boto3.resource("dynamodb")
|
||||
table = dynamodb.Table('Posts')
|
||||
|
||||
|
||||
def lambda_handler(event, context):
|
||||
print(event)
|
||||
body = {}
|
||||
statusCode = 200
|
||||
|
||||
try:
|
||||
# Endpoint: DELETE /posts/{id}
|
||||
if event['routeKey'] == "DELETE /posts/{id}":
|
||||
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']}"}
|
||||
|
||||
# Endpoint: GET /posts/{id}
|
||||
elif event['routeKey'] == "GET /posts/{id}":
|
||||
response = table.get_item(
|
||||
Key={'id': event['pathParameters']['id']}
|
||||
Key={'id': event['pathParameters']['id']} # id jako string
|
||||
)
|
||||
if 'Item' in response:
|
||||
body = response['Item']
|
||||
@ -57,24 +57,37 @@ Instrukcja uruchomienia web serwisu RESTowego na platformie AWS przy użyciu `AW
|
||||
items = response.get('Items', [])
|
||||
body = items
|
||||
|
||||
# Endpoint: POST /posts
|
||||
# Endpoint: POST /posts (automatyczne generowanie id)
|
||||
elif event['routeKey'] == "POST /posts":
|
||||
requestJSON = json.loads(event['body'])
|
||||
table.put_item(
|
||||
Item={
|
||||
'id': str(requestJSON['id']), # DynamoDB keys must be strings
|
||||
'userId': str(requestJSON['userId']),
|
||||
'title': requestJSON['title'],
|
||||
'body': requestJSON['body']
|
||||
}
|
||||
)
|
||||
body = {"message": f"Created item with id {requestJSON['id']}"}
|
||||
|
||||
# Pobierz największe istniejące id
|
||||
response = table.scan()
|
||||
items = response.get('Items', [])
|
||||
max_id = max((int(item['id']) for item in items if item['id'].isdigit()), default=0)
|
||||
|
||||
# Wygeneruj nowe id
|
||||
new_id = str(max_id + 1)
|
||||
|
||||
# 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}
|
||||
elif event['routeKey'] == "PUT /posts/{id}":
|
||||
requestJSON = json.loads(event['body'])
|
||||
|
||||
# Aktualizacja danych w tabeli
|
||||
table.update_item(
|
||||
Key={'id': event['pathParameters']['id']},
|
||||
Key={'id': event['pathParameters']['id']}, # id jako string
|
||||
UpdateExpression="SET userId = :userId, title = :title, body = :body",
|
||||
ExpressionAttributeValues={
|
||||
':userId': str(requestJSON['userId']),
|
||||
@ -82,7 +95,16 @@ Instrukcja uruchomienia web serwisu RESTowego na platformie AWS przy użyciu `AW
|
||||
':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
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user