in app/app.py [0:0]
def patch(cls, event: EventType) -> Tuple[int, Dict[str, str]]:
user_id = event['pathParameters']['user_id']
parameters = json.loads(event['body'])
try:
response = table.update_item(
Key={'user_id': user_id},
# Assign an alias to a reserved word.
UpdateExpression='SET #nm = :newname',
ExpressionAttributeNames={'#nm': 'name'},
# Condition to avoid creating new item if a datum that has specified id is already stored
ConditionExpression='user_id = :user_id',
ExpressionAttributeValues={':newname': parameters['name'], ':user_id': user_id},
ReturnValues='UPDATED_NEW',
)
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
return 404, {'message': 'No such user found'}
raise
return 200, {'user_id': user_id, 'name': response['Attributes']['name']}