From 699ec32683147ec4c7f89621acab6b124523983c Mon Sep 17 00:00:00 2001 From: Robert Andersson Date: Fri, 12 Jul 2024 11:59:59 +0200 Subject: [PATCH] Update sample --- samples/Repository/Program.cs | 20 ++++++++++++++----- .../Handlers/DeleteRequestHandler.cs | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/samples/Repository/Program.cs b/samples/Repository/Program.cs index 9c7b38c..86a975a 100644 --- a/samples/Repository/Program.cs +++ b/samples/Repository/Program.cs @@ -13,10 +13,11 @@ public class ProductRepository { - private readonly IRequestHandler _getProductByIdRequest; + private readonly IRequestHandler _getById; private readonly IRequestHandler<(string Id, decimal NewPrice, DateTime TimeStamp), Product?> _updatePrice; private readonly IRequestHandler _createProduct; private readonly IRequestHandler> _queryByPrice; + private readonly IRequestHandler _deleteById; private class RequestLogger : IRequestPipeLine { @@ -33,7 +34,7 @@ public async Task Invoke(RequestContext requestContext public ProductRepository(string tableName, IAmazonDynamoDB amazonDynamoDb) { var requestLogger = new RequestLogger(); - _getProductByIdRequest = Product.GetById + _getById = Product.WithIdArgument .OnTable(tableName) .ToGetRequestHandler( x => x.ToGetRequestBuilder(), @@ -45,6 +46,13 @@ public ProductRepository(string tableName, IAmazonDynamoDB amazonDynamoDb) } ); + _deleteById = Product.WithIdArgument + .OnTable(tableName) + .ToDeleteRequestHandler( + x => x.ToDeleteRequestBuilder(), + x => x.AmazonDynamoDB = amazonDynamoDb + ); + _updatePrice = Product.UpdatePrice .OnTable(tableName) .ToUpdateRequestHandler( @@ -74,7 +82,7 @@ public ProductRepository(string tableName, IAmazonDynamoDB amazonDynamoDb) ); // You can also use a RequestBuilder if you want to handle the response yourself. - GetRequestBuilder getProductByIdRequestBuilder = Product.GetById + GetRequestBuilder getProductByIdRequestBuilder = Product.WithIdArgument .OnTable(tableName) .ToRequestBuilderFactory() .ToGetRequestBuilder(); @@ -84,14 +92,16 @@ public ProductRepository(string tableName, IAmazonDynamoDB amazonDynamoDb) public Task Create(Product product) => _createProduct.Send(product, default); - public Task GetById(string id) => _getProductByIdRequest.Send(id, default); + public Task GetById(string id) => _getById.Send(id, default); + + public Task DeleteById(string id) => _deleteById.Send(id, default); public Task UpdatePrice(string id, decimal price) => _updatePrice.Send((id, price, DateTime.UtcNow), default); } // These attributes is what makes the source generator kick in. Make sure to have the class 'partial' as well. [DynamoDBMarshaller(AccessName = "Put")] -[DynamoDBMarshaller(AccessName = "GetById", ArgumentType = typeof(string))] +[DynamoDBMarshaller(AccessName = "WithIdArgument", ArgumentType = typeof(string))] [DynamoDBMarshaller(AccessName = "UpdatePrice", ArgumentType = typeof((string Id, decimal NewPrice, DateTime TimeStamp)))] [DynamoDBMarshaller(AccessName = "QueryByPrice", ArgumentType = typeof(decimal))] public partial record Product( diff --git a/src/Dynatello/Handlers/DeleteRequestHandler.cs b/src/Dynatello/Handlers/DeleteRequestHandler.cs index b505f6e..1313553 100644 --- a/src/Dynatello/Handlers/DeleteRequestHandler.cs +++ b/src/Dynatello/Handlers/DeleteRequestHandler.cs @@ -19,6 +19,7 @@ Func, T> createItem _createRequest = createRequest; _createItem = createItem; } + public async Task Send(TArg arg, CancellationToken cancellationToken) { var request = _createRequest(arg);