-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff2db50
commit e2c5589
Showing
7 changed files
with
482 additions
and
370 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
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,26 @@ | ||
using Amazon.DynamoDBv2.DataModel; | ||
using DynamoDBGenerator.Attributes; | ||
|
||
[DynamoDBMarshaller(AccessName = "GetByEmail", ArgumentType = typeof(string))] | ||
[DynamoDBMarshaller( | ||
AccessName = "GetEmployee", | ||
ArgumentType = typeof((string department, string email)) | ||
)] | ||
[DynamoDBMarshaller( | ||
AccessName = "Query", | ||
ArgumentType = typeof((string Department, string EmailPrefix, DateTime MustBeLessThan)) | ||
)] | ||
[DynamoDBMarshaller(AccessName = "Create")] | ||
[DynamoDBMarshaller( | ||
AccessName = "UpdateLastname", | ||
ArgumentType = typeof((string Department, string Email, string NewLastname)) | ||
)] | ||
public partial record Employee( | ||
[property: DynamoDBHashKey] string Department, | ||
[property: DynamoDBRangeKey, DynamoDBGlobalSecondaryIndexHashKey("EmailLookup")] string Email, | ||
string LastName, | ||
string[] Skills, | ||
Metadata Metadata | ||
); | ||
|
||
public record Metadata(DateTime Timestamp); |
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,150 @@ | ||
using Amazon.DynamoDBv2; | ||
using Dynatello; | ||
using Dynatello.Builders; | ||
using Dynatello.Builders.Types; | ||
using Dynatello.Handlers; | ||
|
||
public partial class EmployeeRepository | ||
{ | ||
private readonly IRequestHandler<(string department, string email), Employee?> _getEmployee; | ||
private readonly IRequestHandler<(string department, string email), Employee?> _deleteEmployee; | ||
private readonly IRequestHandler<string, IReadOnlyList<Employee>> _queryByEmail; | ||
private readonly IRequestHandler<Employee, Employee?> _createEmployee; | ||
private readonly IRequestHandler< | ||
(string Department, string EmailPrefix, DateTime MustBeLessThan), | ||
IReadOnlyList<Employee> | ||
> _queryByDepartment; | ||
private readonly IRequestHandler< | ||
(string Department, string Email, string NewLastname), | ||
Employee? | ||
> _updateLastname; | ||
|
||
private readonly IAmazonDynamoDB _database; | ||
|
||
public EmployeeRepository(IAmazonDynamoDB dynamoDB) | ||
{ | ||
_database = dynamoDB; | ||
var middleware = new RequestLogAnalyzer(); | ||
_deleteEmployee = Employee | ||
.GetEmployee.OnTable(TableName) | ||
.ToDeleteRequestHandler( | ||
x => x.ToDeleteRequestBuilder(y => y.department, y => y.email), | ||
x => | ||
{ | ||
x.AmazonDynamoDB = dynamoDB; | ||
x.RequestsPipelines.Add(middleware); | ||
} | ||
); | ||
|
||
_getEmployee = Employee | ||
.GetEmployee.OnTable(TableName) | ||
.ToGetRequestHandler( | ||
x => x.ToGetRequestBuilder(y => y.department, y => y.email), | ||
x => | ||
{ | ||
x.AmazonDynamoDB = dynamoDB; | ||
x.RequestsPipelines.Add(middleware); | ||
} | ||
); | ||
|
||
_queryByEmail = Employee | ||
.GetByEmail.OnTable(TableName) | ||
.ToQueryRequestHandler( | ||
x => | ||
x.WithKeyConditionExpression((x, y) => $"{x.Email} = {y} ") | ||
.ToQueryRequestBuilder() with | ||
{ | ||
IndexName = "EmailLookup", | ||
}, | ||
x => | ||
{ | ||
x.AmazonDynamoDB = dynamoDB; | ||
x.RequestsPipelines.Add(middleware); | ||
} | ||
); | ||
|
||
_createEmployee = Employee | ||
.Create.OnTable(TableName) | ||
.ToPutRequestHandler( | ||
x => | ||
x.WithConditionExpression( | ||
(x, y) => $"{x.Department} <> {y.Department} AND {x.Email} <> {y.Email}" | ||
) | ||
.ToPutRequestBuilder(), | ||
x => | ||
{ | ||
x.AmazonDynamoDB = dynamoDB; | ||
x.RequestsPipelines.Add(middleware); | ||
} | ||
); | ||
|
||
_queryByDepartment = Employee | ||
.Query.OnTable(TableName) | ||
.ToQueryRequestHandler( | ||
x => | ||
x.WithKeyConditionExpression( | ||
(x, y) => | ||
$"{x.Department} = {y.Department} and begins_with({x.Email}, {y.EmailPrefix})" | ||
) | ||
.WithFilterExpression( | ||
(x, y) => $"{x.Metadata.Timestamp} < {y.MustBeLessThan}" | ||
) | ||
.ToQueryRequestBuilder(), | ||
x => | ||
{ | ||
x.RequestsPipelines.Add(new RequestLogAnalyzer()); | ||
x.AmazonDynamoDB = dynamoDB; | ||
} | ||
); | ||
|
||
_updateLastname = Employee | ||
.UpdateLastname.OnTable(TableName) | ||
.ToUpdateRequestHandler( | ||
x => | ||
x.WithUpdateExpression((x, y) => $"SET {x.LastName} = {y.NewLastname}") | ||
.ToUpdateItemRequestBuilder((x, y) => x.Keys(y.Department, y.Email)) with | ||
{ | ||
ReturnValues = ReturnValue.ALL_NEW, | ||
}, | ||
x => | ||
{ | ||
x.AmazonDynamoDB = dynamoDB; | ||
x.RequestsPipelines.Add(middleware); | ||
} | ||
); | ||
} | ||
|
||
public Task<Employee?> GetPersonById( | ||
string department, | ||
string email, | ||
CancellationToken cancellationToken | ||
) => _getEmployee.Send((department, email), cancellationToken); | ||
|
||
public Task<IReadOnlyList<Employee>> SearchByEmail( | ||
string email, | ||
CancellationToken cancellationToken | ||
) => _queryByEmail.Send(email, cancellationToken); | ||
|
||
public Task<Employee?> CreateEmployee(Employee employee, CancellationToken cancellationToken) => | ||
_createEmployee.Send(employee, cancellationToken); | ||
|
||
public Task<IReadOnlyList<Employee>> QueryByDepartment( | ||
string department, | ||
string emailStartsWith, | ||
DateTime updatedBefore, | ||
CancellationToken cancellationToken | ||
) => _queryByDepartment.Send((department, emailStartsWith, updatedBefore), cancellationToken); | ||
|
||
public Task<Employee?> UpdateLastName( | ||
string department, | ||
string email, | ||
string lastname, | ||
CancellationToken cancellationToken | ||
) => _updateLastname.Send((department, email, lastname), cancellationToken); | ||
|
||
public Task DeleteEmployee( | ||
string department, | ||
string email, | ||
CancellationToken cancellationToken | ||
) => _deleteEmployee.Send((department, email), cancellationToken); | ||
} |
Oops, something went wrong.