2017-10-04 76 views
0

我制作了用于从数据库编辑项目的方法。 这是我的方法的样子:DynamoDB - 如何从数据库获得主键(这是随机的ID)使endpoind类?

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; 

} 

现在,我要为这个方法创建端点类,但我的问题,我需要得到的PrimaryKey作为参数(它的长相类似这样的例子: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​​网关和Lambda吗? –

+0

是的,它是我需要创建endpoind的EditProductLambda类,并且在这个类中,我不知道如何获得PrimaryKey –

+0

好。你想如何传递主键?作为查询字符串或POST请求中的正文? –

回答

0

因此,首先您必须创建一个触发器来实现Lambda函数,理想情况下,此处将使用API​​网关。您可以将数据作为查询字符串或作为请求主体传递给API网关。

您可以在API网关的集成请求部分使用正文映射模板,并获取请求正文/查询字符串。在身体映射模板中构建一个新的json,该模板将具有来自请求主体/查询字符串的数据。在我们添加body map模板时,您的业务逻辑将获得我们在body map模板中构建的json。

体内映射模板获得查询字符串,请做,

$ input.params( 'querystringkey')

例如身体映射模板内(如果使用的查询字符串),

#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/有关身体映射模板的更多详细信息

相关问题