- Implement an update handler to update an existing movie item.
Answer: The handler expects a movie item in a JSON format; the input will be encoded to a Movie struct. The PutItem method is used to insert the movie to the table as follows:
func update(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var movie Movie
err := json.Unmarshal([]byte(request.Body), &movie)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 400,
Body: "Invalid payload",
}, nil
}
...
svc := dynamodb.New(cfg)
req := svc.PutItemRequest(&dynamodb.PutItemInput{
TableName: aws.String(os.Getenv("TABLE_NAME")),
Item: map[string]dynamodb.AttributeValue{
"ID": dynamodb.AttributeValue{
S: aws.String(movie.ID),
},
"Name"...