Skip to content

Commit

Permalink
Started adding DynamoDB implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeastham1993 committed Dec 18, 2021
1 parent 9e248fa commit 9a13ffe
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 23 deletions.
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ services:
container_name: plant-based-pizza
ports:
- 5113:80
localstack:
container_name: localstack
image: localstack/localstack
environment:
- SERVICES=dynamodb
ports:
- "4566:4566"
Original file line number Diff line number Diff line change
@@ -1,54 +1,98 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using MongoDB.Driver;
using Newtonsoft.Json;
using PlantBasedPizza.Deliver.Core.Entities;
using PlantBasedPizza.Deliver.Infrastructure.Extensions;

namespace PlantBasedPizza.Deliver.Infrastructure
{
public class DeliveryRequestRepository : IDeliveryRequestRepository
{
private readonly IMongoDatabase _database;
private readonly IMongoCollection<DeliveryRequest> _collection;
private readonly AmazonDynamoDBClient _dynamoDbClient;

public DeliveryRequestRepository(MongoClient client)
public DeliveryRequestRepository(MongoClient client, AmazonDynamoDBClient dynamoDbClient)
{
this._database = client.GetDatabase("PlantBasedPizza");
this._collection = this._database.GetCollection<DeliveryRequest>("DeliveryRequests");
this._dynamoDbClient = dynamoDbClient;
}

public async Task AddNewDeliveryRequest(DeliveryRequest request)
{
await this._collection.InsertOneAsync(request).ConfigureAwait(false);
await this._dynamoDbClient.PutItemAsync(InfrastructureConstants.TableName,
request.AsAttributeMap());
}

public async Task UpdateDeliveryRequest(DeliveryRequest request)
{
var queryBuilder = Builders<DeliveryRequest>.Filter.Eq(ord => ord.OrderIdentifier, request.OrderIdentifier);

await this._collection.ReplaceOneAsync(queryBuilder, request);
await this._dynamoDbClient.PutItemAsync(InfrastructureConstants.TableName,
request.AsAttributeMap());
}

public async Task<DeliveryRequest?> GetDeliveryStatusForOrder(string orderIdentifier)
{
var queryBuilder = Builders<DeliveryRequest>.Filter.Eq(p => p.OrderIdentifier, orderIdentifier);
var getResult = await this._dynamoDbClient.GetItemAsync(InfrastructureConstants.TableName,
new Dictionary<string, AttributeValue>(2)
{
{ "PK", new AttributeValue($"DELIVERY#{orderIdentifier.ToUpper()}") },
{ "SK", new AttributeValue($"DELIVERY#{orderIdentifier.ToUpper()}") },
});

var request = await this._collection.Find(queryBuilder).FirstOrDefaultAsync().ConfigureAwait(false);
if (!getResult.IsItemSet)
{
throw new Exception("Order not found");
}

var result = JsonConvert.DeserializeObject<DeliveryRequest>(getResult.Item["Data"].S);

return request;
return result;
}

public async Task<List<DeliveryRequest>> GetAwaitingDriver()
{
var requests = await this._collection.Find(p => p.DriverCollectedOn == null).ToListAsync();
var queryResults = await this._dynamoDbClient.QueryAsync(new QueryRequest()
{
TableName = InfrastructureConstants.TableName,
IndexName = "GSI2",
KeyConditionExpression = "GSI2PK = :PK and GSI2SK = :SK",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":PK", new AttributeValue { S = "AWAITINGCOLLECTION" }},
{":SK", new AttributeValue { S = "AWAITINGCOLLECTION" }}
}
});

var results = new List<DeliveryRequest>();

foreach (var queryResult in queryResults.Items)
{
results.Add(JsonConvert.DeserializeObject<DeliveryRequest>(queryResult["Data"].S));
}

return requests;
return results;
}

public async Task<List<DeliveryRequest>> GetOrdersWithDriver(string driverName)
{
var requests = await this._collection.Find(p => p.DeliveredOn == null && p.DriverCollectedOn != null && p.Driver == driverName).ToListAsync();
var queryResults = await this._dynamoDbClient.QueryAsync(new QueryRequest()
{
TableName = InfrastructureConstants.TableName,
IndexName = "GSI1",
KeyConditionExpression = "GSI1PK = :PK and GSI1SK = :SK",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":PK", new AttributeValue { S = $"DRIVER#{driverName.ToUpper()}" }},
{":SK", new AttributeValue { S = $"DRIVER#{driverName.ToUpper()}" }}
}
});

var results = new List<DeliveryRequest>();

foreach (var queryResult in queryResults.Items)
{
results.Add(JsonConvert.DeserializeObject<DeliveryRequest>(queryResult["Data"].S));
}

return requests;
return results;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Amazon.DynamoDBv2.Model;
using Newtonsoft.Json;
using PlantBasedPizza.Deliver.Core.Entities;

namespace PlantBasedPizza.Deliver.Infrastructure.Extensions;

public static class DeliveryRequestExtensions
{
public static Dictionary<string, AttributeValue> AsAttributeMap(this DeliveryRequest request)
{
var attributeMap = new Dictionary<string, AttributeValue>(4)
{
{ "PK", new AttributeValue($"DELIVERY#{request.OrderIdentifier.ToUpper()}") },
{ "SK", new AttributeValue($"DELIVERY#{request.OrderIdentifier.ToUpper()}") },
{ "Type", new AttributeValue("DeliveryRequest") },
{ "Data", new AttributeValue(JsonConvert.SerializeObject(request)) },
};

if (string.IsNullOrEmpty(request.Driver))
{
attributeMap.Add("GSI1PK", new AttributeValue($"DRIVER#{request.Driver.ToUpper()}"));
attributeMap.Add("GSI1SK", new AttributeValue($"DRIVER#{request.Driver.ToUpper()}"));
}
else
{
attributeMap.Add("GSI2PK", new AttributeValue($"AWAITINGCOLLECTION"));
attributeMap.Add("GSI2SK", new AttributeValue($"AWAITINGCOLLECTION"));
}

return attributeMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PlantBasedPizza.Deliver.Infrastructure;

public static class InfrastructureConstants
{
public static string TableName => Environment.GetEnvironmentVariable("TABLE_NAME") ?? "plant-based-pizza";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.2.2" />
<PackageReference Include="MongoDB.Driver" Version="2.14.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Amazon;
using Amazon.DynamoDBv2;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson.Serialization;
Expand Down Expand Up @@ -27,11 +29,11 @@ public static IServiceCollection AddDeliveryModuleInfrastructure(this IServiceCo
map.SetIgnoreExtraElements(true);
map.SetIgnoreExtraElementsIsInherited(true);
});

// or use a connection string
var client = new MongoClient(configuration["DatabaseConnection"]);

services.AddSingleton<MongoClient>(client);
services.AddSingleton<AmazonDynamoDBClient>(new AmazonDynamoDBClient(new AmazonDynamoDBConfig()
{
RegionEndpoint = RegionEndpoint.EUWest1,
}));

services.AddTransient<IDeliveryRequestRepository, DeliveryRequestRepository>();
services.AddTransient<Handles<OrderReadyForDeliveryEvent>, OrderReadyForDeliveryEventHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Amazon.DynamoDBv2.Model;
using PlantBasedPizza.Kitchen.Core.Entities;

namespace PlantBasedPizza.Kitchen.Infrastructure.Extensions;

public static class KitchenRequestExtensions
{
public static Dictionary<string, AttributeValue> AsAttributeMap(this KitchenRequest request)
{
var attributes = new Dictionary<string, AttributeValue>();

return attributes;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using MongoDB.Driver;
using PlantBasedPizza.Kitchen.Core.Entities;
using PlantBasedPizza.Shared.Logging;

public class KitchenRequestRepository : IKitchenRequestRepository
{
private readonly IMongoDatabase _database;
private readonly IMongoCollection<KitchenRequest> _kitchenRequests;
private readonly AmazonDynamoDBClient _dynamoDbClient;
private readonly IObservabilityService _observability;

public KitchenRequestRepository(MongoClient client)
public KitchenRequestRepository(AmazonDynamoDBClient dynamoDbClient, IObservabilityService observability)
{
this._database = client.GetDatabase("PlantBasedPizza");
this._kitchenRequests = this._database.GetCollection<KitchenRequest>("kitchen");
_dynamoDbClient = dynamoDbClient;
_observability = observability;
}

public async Task AddNew(KitchenRequest kitchenRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.2.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.14.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
Expand Down

0 comments on commit 9a13ffe

Please sign in to comment.