@@ -32,8 +32,8 @@ public UserConnectionRepository(IAmazonDynamoDB amazonDynamoDb)
32
32
KeyConditionExpression = "pk = :pk and sk = :sk" ,
33
33
ExpressionAttributeValues = new Dictionary < string , AttributeValue >
34
34
{
35
- { ":pk" , new AttributeValue { S = UserConnectionPk } } ,
36
- { ":sk" , new AttributeValue { S = userId } }
35
+ { ":pk" , new AttributeValue { S = UserConnectionPk } } ,
36
+ { ":sk" , new AttributeValue { S = userId } }
37
37
} ,
38
38
} ;
39
39
@@ -64,32 +64,34 @@ public UserConnectionRepository(IAmazonDynamoDB amazonDynamoDb)
64
64
public async Task < bool > SaveAsync ( UserConnection userConnection , CancellationToken cancellationToken = default )
65
65
{
66
66
var ttl = DateTime . UtcNow ;
67
- if ( userConnection . Connections . Any ( ) )
67
+ if ( userConnection . Connections . Any ( ) )
68
68
ttl = userConnection . Connections . Max ( q => q . Time ) ;
69
-
69
+
70
70
var request = new PutItemRequest
71
71
{
72
72
TableName = _tableName ,
73
73
Item = new Dictionary < string , AttributeValue >
74
74
{
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
77
86
{
78
- "connections" , new AttributeValue
87
+ M = new Dictionary < string , AttributeValue >
79
88
{
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" ) } }
88
91
}
89
- } ,
90
- { "ttl" , new AttributeValue { N = ttl . AddMinutes ( 30 ) . ToUnixTimeSeconds ( ) . ToString ( ) } }
91
- }
92
- } ;
92
+ } ) . ToList ( )
93
+ } ) ;
94
+ }
93
95
94
96
var response = await _amazonDynamoDb . PutItemAsync ( request , cancellationToken ) ;
95
97
return response . HttpStatusCode == HttpStatusCode . OK ;
@@ -102,15 +104,16 @@ public async Task<bool> DeleteAsync(string userId, CancellationToken cancellatio
102
104
TableName = _tableName ,
103
105
Key = new Dictionary < string , AttributeValue >
104
106
{
105
- { "pk" , new AttributeValue { S = UserConnectionPk } } ,
106
- { "sk" , new AttributeValue { S = userId } }
107
+ { "pk" , new AttributeValue { S = UserConnectionPk } } ,
108
+ { "sk" , new AttributeValue { S = userId } }
107
109
}
108
110
} ;
109
111
var response = await _amazonDynamoDb . DeleteItemAsync ( request , cancellationToken ) ;
110
112
return response . HttpStatusCode == HttpStatusCode . OK ;
111
113
}
112
114
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 )
114
117
{
115
118
if ( ! userIds . Any ( ) )
116
119
{
@@ -126,8 +129,8 @@ public async Task<List<string>> GetOnlineListAsync(List<string> userIds, Cancell
126
129
{
127
130
Keys = userIds . Select ( q => new Dictionary < string , AttributeValue >
128
131
{
129
- { "pk" , new AttributeValue { S = UserConnectionPk } } ,
130
- { "sk" , new AttributeValue { S = q } }
132
+ { "pk" , new AttributeValue { S = UserConnectionPk } } ,
133
+ { "sk" , new AttributeValue { S = q } }
131
134
} ) . ToList ( ) ,
132
135
ProjectionExpression = "sk"
133
136
}
@@ -146,7 +149,7 @@ public async Task<List<string>> GetOnlineListAsync(CancellationToken cancellatio
146
149
KeyConditionExpression = "pk = :pk" ,
147
150
ExpressionAttributeValues = new Dictionary < string , AttributeValue >
148
151
{
149
- { ":pk" , new AttributeValue { S = UserConnectionPk } }
152
+ { ":pk" , new AttributeValue { S = UserConnectionPk } }
150
153
} ,
151
154
} , cancellationToken ) ;
152
155
return queryResponse . Items . Select ( q => q [ "sk" ] . S ) . ToList ( ) ;
@@ -159,10 +162,10 @@ public async Task<bool> SaveLastActivityAsync(string userId, CancellationToken c
159
162
TableName = _tableName ,
160
163
Item = new Dictionary < string , AttributeValue >
161
164
{
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 ( ) } }
166
169
}
167
170
} ;
168
171
@@ -171,7 +174,8 @@ public async Task<bool> SaveLastActivityAsync(string userId, CancellationToken c
171
174
return response . HttpStatusCode == HttpStatusCode . OK ;
172
175
}
173
176
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 )
175
179
{
176
180
if ( ! userIds . Any ( ) )
177
181
return new List < UserLastActivity > ( ) ;
@@ -185,8 +189,8 @@ public async Task<List<UserLastActivity>> GetLastActivityAsync(List<string> user
185
189
{
186
190
Keys = userIds . Select ( q => new Dictionary < string , AttributeValue >
187
191
{
188
- { "pk" , new AttributeValue { S = LastActivityPk } } ,
189
- { "sk" , new AttributeValue { S = q } }
192
+ { "pk" , new AttributeValue { S = LastActivityPk } } ,
193
+ { "sk" , new AttributeValue { S = q } }
190
194
} ) . ToList ( )
191
195
}
192
196
}
0 commit comments