-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started adding DynamoDB implementation
- Loading branch information
1 parent
9e248fa
commit 9a13ffe
Showing
11 changed files
with
136 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 60 additions & 16 deletions
76
src/modules/deliver/PlantBasedPizza.Deliver.Infrastructure/DeliveryRequestRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...es/deliver/PlantBasedPizza.Deliver.Infrastructure/Extensions/DeliveryRequestExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/modules/deliver/PlantBasedPizza.Deliver.Infrastructure/InfrastructureConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...les/kitchen/PlantBasedPizza.Kitchen.Infrastructure/Extensions/KitchenRequestExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
src/modules/kitchen/PlantBasedPizza.Kitchen.Infrastructure/KitchenRequestRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters