2017-10-04 8 views
0

私はデータベースからアイテムを編集するために使うメソッドを作った。 これは私の方法がどのように見えるかです:DynamoDB - エンドポイントクラスを作成するためにデータベースからプライマリキー(ランダムID)を取得する方法は?

public Product editProduct(PrimaryKey primaryKey, Product content) { 

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withValueMap(createValueMap(content)); 

    UpdateItemOutcome itemOutcome = databaseController.getTable(PRODUCT_TABLE).updateItem(updateItemSpec); 

    return convertToProduct(itemOutcome); 

} 

private Map<String, Object> createValueMap(Product content) { 

    Map<String, Object> result = new HashMap<>(); 

    result.put("name", content.getName()); 
    result.put("calories", content.getCalories()); 
    result.put("fat", content.getFat()); 
    result.put("carbo", content.getCarbo()); 
    result.put("protein", content.getProtein()); 
    result.put("productKinds", content.getProductKinds()); 
    result.put("author", content.getAuthor()); 
    result.put("media", content.getMedia()); 
    result.put("approved", content.getApproved()); 

    return result; 
} 

private Product convertToProduct(UpdateItemOutcome itemOutcome) { 

    Product product = new Product(); 
    product.setName(itemOutcome.getItem().get("name").toString()); 
    product.setCalories(itemOutcome.getItem().getInt("calories")); 
    product.setFat(itemOutcome.getItem().getDouble("fat")); 
    product.setCarbo(itemOutcome.getItem().getDouble("carbo")); 
    product.setProtein(itemOutcome.getItem().getDouble("protein")); 
    product.setProductKinds(itemOutcome.getItem().getList("productKinds")); 

    ObjectMapper objectMapper = new ObjectMapper(); 
    try { 
     Author productAuthor = objectMapper.readValue(itemOutcome.getItem().getString("author"), Author.class); 
     product.setAuthor(productAuthor); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     Media productMedia = objectMapper.readValue(itemOutcome.getItem().getString("media"), Media.class); 
     product.setMedia(productMedia); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return product; 

} 

今、私はこの方法のためのエンドポイントクラスを作成したいが、私は問題を抱えている、私は例えば、このようなパラメータ(それのルックスとして主キーを取得する必要があります:2567763a-D21E-4146 -8d61-9d52c2561fc0)、これを行う方法はわかりません。

現時点では私のクラスは以下のようになります。

public class EditProductLambda implements RequestHandler<Map<String, Object>, ApiGatewayResponse> { 
private LambdaLogger logger; 
@Override 
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) { 
    logger = context.getLogger(); 
    logger.log(input.toString()); 

    try{ 
     Product product = RequestUtil.parseRequest(input, Product.class); 
     //PrimaryKey primaryKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
     KitchenService kitchenService = new KitchenService(new DatabaseController(context, Regions.EU_CENTRAL_1), logger); 
     Product editedProduct = kitchenService.editProduct(primaryKey, product); 
     return ResponseUtil.generateResponse(HttpStatus.SC_CREATED, editedProduct); 
    } catch (IllegalArgumentException e){ 
     return ResponseUtil.generateResponse(HttpStatus.SC_BAD_REQUEST, e.getMessage()); 

    } 


} 

誰かが私にそれを行うにはどのようにいくつかのアドバイスを与えることはできますか?または私の方法は間違って行われますか?

+0

APIゲートウェイとラムダを使用していますか? –

+0

はい、私はendpoindを作成する必要があるEditProductLambdaクラスです。このクラスではPrimaryKeyを取得する方法がわかりません –

+0

okです。どのようにプライマリキーを渡したいですか?クエリ文字列またはPOST要求の本文として? –

答えて

0

まず、ラムダ関数のトリガを作成する必要があります。理想的なのは、APIゲートウェイです。データをクエリ文字列として渡すことも、リクエスト本体としてAPIゲートウェイに渡すこともできます。

APIゲートウェイの統合要求セクションでボディマッピングテンプレートを使用し、要求ボディ/クエリ文字列を取得できます。要求ボディ/クエリ文字列からのデータを持つボディマッピングテンプレートで新しいjsonを構築します。ボディマッピングテンプレートを追加すると、ビジネスロジックはボディマッピングテンプレートで作成したjsonを取得します。ボディマッピングテンプレート内

は( 'querystringkey')ボディマッピングテンプレート内の例えば

、クエリ文字列が行ってください

$ input.paramsを取得するには、(クエリ文字列を使用している場合)

#set($inputRoot = $input.path('$')) 
{ 
"primaryKey" : "$input.params('$.primaryKey')" 
} 

直してください

#set($inputRoot = $input.path('$')) 
{ 
"primaryKey" : "$input.path('$.primaryKey')" 
} 

、そして体としてデータを渡す場合広告https://aws.amazon.com/blogs/compute/tag/mapping-templates/ボディマッピングテンプレートの詳細について

関連する問題