Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Jul 12, 2024
1 parent 4c0f9a4 commit 699ec32
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions samples/Repository/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

public class ProductRepository
{
private readonly IRequestHandler<string, Product?> _getProductByIdRequest;
private readonly IRequestHandler<string, Product?> _getById;
private readonly IRequestHandler<(string Id, decimal NewPrice, DateTime TimeStamp), Product?> _updatePrice;
private readonly IRequestHandler<Product, Product?> _createProduct;
private readonly IRequestHandler<decimal, IReadOnlyList<Product>> _queryByPrice;
private readonly IRequestHandler<string, Product?> _deleteById;

private class RequestLogger : IRequestPipeLine
{
Expand All @@ -33,7 +34,7 @@ public async Task<AmazonWebServiceResponse> 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(),
Expand All @@ -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(
Expand Down Expand Up @@ -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<string> getProductByIdRequestBuilder = Product.GetById
GetRequestBuilder<string> getProductByIdRequestBuilder = Product.WithIdArgument
.OnTable(tableName)
.ToRequestBuilderFactory()
.ToGetRequestBuilder();
Expand All @@ -84,14 +92,16 @@ public ProductRepository(string tableName, IAmazonDynamoDB amazonDynamoDb)

public Task<Product?> Create(Product product) => _createProduct.Send(product, default);

public Task<Product?> GetById(string id) => _getProductByIdRequest.Send(id, default);
public Task<Product?> GetById(string id) => _getById.Send(id, default);

public Task<Product?> DeleteById(string id) => _deleteById.Send(id, default);

public Task<Product?> 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(
Expand Down
1 change: 1 addition & 0 deletions src/Dynatello/Handlers/DeleteRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Func<Dictionary<string, AttributeValue>, T> createItem
_createRequest = createRequest;
_createItem = createItem;
}

public async Task<T?> Send(TArg arg, CancellationToken cancellationToken)
{
var request = _createRequest(arg);
Expand Down

0 comments on commit 699ec32

Please sign in to comment.