Skip to content

Commit b8572b5

Browse files
committed
Bug fix.
1 parent 3ff6f49 commit b8572b5

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/Infrastructure/Repositories/DynamoDb/UserConnectionRepository.cs

+36-32
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public UserConnectionRepository(IAmazonDynamoDB amazonDynamoDb)
3232
KeyConditionExpression = "pk = :pk and sk = :sk",
3333
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
3434
{
35-
{":pk", new AttributeValue {S = UserConnectionPk}},
36-
{":sk", new AttributeValue {S = userId}}
35+
{ ":pk", new AttributeValue { S = UserConnectionPk } },
36+
{ ":sk", new AttributeValue { S = userId } }
3737
},
3838
};
3939

@@ -64,32 +64,34 @@ public UserConnectionRepository(IAmazonDynamoDB amazonDynamoDb)
6464
public async Task<bool> SaveAsync(UserConnection userConnection, CancellationToken cancellationToken = default)
6565
{
6666
var ttl = DateTime.UtcNow;
67-
if(userConnection.Connections.Any())
67+
if (userConnection.Connections.Any())
6868
ttl = userConnection.Connections.Max(q => q.Time);
69-
69+
7070
var request = new PutItemRequest
7171
{
7272
TableName = _tableName,
7373
Item = new Dictionary<string, AttributeValue>
7474
{
75-
{"pk", new AttributeValue {S = UserConnectionPk}},
76-
{"sk", new AttributeValue {S = userConnection.UserId}},
75+
{ "pk", new AttributeValue { S = UserConnectionPk } },
76+
{ "sk", new AttributeValue { S = userConnection.UserId } },
77+
{ "ttl", new AttributeValue { N = ttl.AddMinutes(30).ToUnixTimeSeconds().ToString() } }
78+
}
79+
};
80+
81+
if (userConnection.Connections.Any())
82+
{
83+
request.Item.Add("connections", new AttributeValue
84+
{
85+
L = userConnection.Connections.Select(q => new AttributeValue
7786
{
78-
"connections", new AttributeValue
87+
M = new Dictionary<string, AttributeValue>
7988
{
80-
L = userConnection.Connections.Select(q => new AttributeValue
81-
{
82-
M = new Dictionary<string, AttributeValue>
83-
{
84-
{"id", new AttributeValue {S = q.Id}},
85-
{"time", new AttributeValue {S = q.Time.ToString("O")}}
86-
}
87-
}).ToList()
89+
{ "id", new AttributeValue { S = q.Id } },
90+
{ "time", new AttributeValue { S = q.Time.ToString("O") } }
8891
}
89-
},
90-
{"ttl",new AttributeValue {N = ttl.AddMinutes(30).ToUnixTimeSeconds().ToString()}}
91-
}
92-
};
92+
}).ToList()
93+
});
94+
}
9395

9496
var response = await _amazonDynamoDb.PutItemAsync(request, cancellationToken);
9597
return response.HttpStatusCode == HttpStatusCode.OK;
@@ -102,15 +104,16 @@ public async Task<bool> DeleteAsync(string userId, CancellationToken cancellatio
102104
TableName = _tableName,
103105
Key = new Dictionary<string, AttributeValue>
104106
{
105-
{"pk", new AttributeValue {S = UserConnectionPk}},
106-
{"sk", new AttributeValue {S = userId}}
107+
{ "pk", new AttributeValue { S = UserConnectionPk } },
108+
{ "sk", new AttributeValue { S = userId } }
107109
}
108110
};
109111
var response = await _amazonDynamoDb.DeleteItemAsync(request, cancellationToken);
110112
return response.HttpStatusCode == HttpStatusCode.OK;
111113
}
112114

113-
public async Task<List<string>> GetOnlineListAsync(List<string> userIds, CancellationToken cancellationToken = default)
115+
public async Task<List<string>> GetOnlineListAsync(List<string> userIds,
116+
CancellationToken cancellationToken = default)
114117
{
115118
if (!userIds.Any())
116119
{
@@ -126,8 +129,8 @@ public async Task<List<string>> GetOnlineListAsync(List<string> userIds, Cancell
126129
{
127130
Keys = userIds.Select(q => new Dictionary<string, AttributeValue>
128131
{
129-
{"pk", new AttributeValue {S = UserConnectionPk}},
130-
{"sk", new AttributeValue {S = q}}
132+
{ "pk", new AttributeValue { S = UserConnectionPk } },
133+
{ "sk", new AttributeValue { S = q } }
131134
}).ToList(),
132135
ProjectionExpression = "sk"
133136
}
@@ -146,7 +149,7 @@ public async Task<List<string>> GetOnlineListAsync(CancellationToken cancellatio
146149
KeyConditionExpression = "pk = :pk",
147150
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
148151
{
149-
{":pk", new AttributeValue {S = UserConnectionPk}}
152+
{ ":pk", new AttributeValue { S = UserConnectionPk } }
150153
},
151154
}, cancellationToken);
152155
return queryResponse.Items.Select(q => q["sk"].S).ToList();
@@ -159,10 +162,10 @@ public async Task<bool> SaveLastActivityAsync(string userId, CancellationToken c
159162
TableName = _tableName,
160163
Item = new Dictionary<string, AttributeValue>
161164
{
162-
{"pk", new AttributeValue {S = LastActivityPk}},
163-
{"sk", new AttributeValue {S = userId}},
164-
{"time", new AttributeValue {S = DateTime.UtcNow.ToString("O")}},
165-
{"ttl", new AttributeValue {N = DateTimeOffset.UtcNow.AddMonths(6).ToUnixTimeSeconds().ToString()}}
165+
{ "pk", new AttributeValue { S = LastActivityPk } },
166+
{ "sk", new AttributeValue { S = userId } },
167+
{ "time", new AttributeValue { S = DateTime.UtcNow.ToString("O") } },
168+
{ "ttl", new AttributeValue { N = DateTimeOffset.UtcNow.AddMonths(6).ToUnixTimeSeconds().ToString() } }
166169
}
167170
};
168171

@@ -171,7 +174,8 @@ public async Task<bool> SaveLastActivityAsync(string userId, CancellationToken c
171174
return response.HttpStatusCode == HttpStatusCode.OK;
172175
}
173176

174-
public async Task<List<UserLastActivity>> GetLastActivityAsync(List<string> userIds, CancellationToken cancellationToken = default)
177+
public async Task<List<UserLastActivity>> GetLastActivityAsync(List<string> userIds,
178+
CancellationToken cancellationToken = default)
175179
{
176180
if (!userIds.Any())
177181
return new List<UserLastActivity>();
@@ -185,8 +189,8 @@ public async Task<List<UserLastActivity>> GetLastActivityAsync(List<string> user
185189
{
186190
Keys = userIds.Select(q => new Dictionary<string, AttributeValue>
187191
{
188-
{"pk", new AttributeValue {S = LastActivityPk}},
189-
{"sk", new AttributeValue {S = q}}
192+
{ "pk", new AttributeValue { S = LastActivityPk } },
193+
{ "sk", new AttributeValue { S = q } }
190194
}).ToList()
191195
}
192196
}

0 commit comments

Comments
 (0)