-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamoDBEmployeeRepository.cs
150 lines (136 loc) · 5.41 KB
/
DynamoDBEmployeeRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Amazon.DynamoDBv2;
using Dynatello;
using Dynatello.Builders;
using Dynatello.Builders.Types;
using Dynatello.Handlers;
public partial class DynamoDBEmployeeRepository : IEmployeeRepository
{
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 DynamoDBEmployeeRepository(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);
}