-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.cs
More file actions
100 lines (87 loc) · 3.57 KB
/
Function.cs
File metadata and controls
100 lines (87 loc) · 3.57 KB
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
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using GetAllChats.Models;
using System.Net;
using System.Text.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace GetAllChats;
public class Function
{
private readonly AmazonDynamoDBClient _client;
private readonly DynamoDBContext _context;
public Function()
{
_client = new AmazonDynamoDBClient();
_context = new DynamoDBContext(_client);
}
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
var userId = request.QueryStringParameters["userId"];
//added
var limit = int.Parse(request.QueryStringParameters["limit"]);
var exclusiveStartKey = request.QuerryStringParameters["exclusiveStartKey"];
List<Chat> chats = await GetAllChats(userId, limit, exclusiveStartKey);
var result = new List<GetAllChatsResponseItem>(chats.Count);
// TODO
foreach (var chat in chats)
{
var item = new GetAllChatsResponseItem()
{
ChatId = chat.ChatId,
User1 = chat.User1,
User2 = chat.User2,
LastMessage = chat.LastMessage,
UpdateDt = chat.UpdateDt,
};
result.Add(item);
}
// end todo
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json" },
{ "Access-Control-Allow-Origin", "*" }
},
Body = JsonSerializer.Serialize(result)
};
}
private async Task<List<Chat>> GetAllChats(string userId, int limit, string exclusiveStartKey)
{
var user1 = new QueryOperationConfig()
{
Limit = limit,
ExclusiveStartKey = exclusiveStartKey != null ?
new Dictionary<string, AttributeValue> { { "user1", new AttributeValue { S = exclusiveStartKey } } }
:null,
IndexName = "user1-updatedDt-index",
KeyExpression = new Expression()
{
ExpressionStatement = "user1 = :user",
ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>() { { ":user", userId } }
}
};
var user1Results = await _context.FromQueryAsync<Chat>(user1).GetRemainingAsync();
var user2 = new QueryOperationConfig()
{
Limit = limit,
ExclusiveStartKey = exclusiveStartKey != null ?
new Dictionary<string, AttributeValue> { { "user2", new AttributeValue { S = exclusiveStartKey } } }
: null,
IndexName = "user2-updatedDt-index",
KeyExpression = new Expression()
{
ExpressionStatement = "user2 = :user",
ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>() { { ":user", userId } }
}
};
var user2Results = await _context.FromQueryAsync<Chat>(user2).GetRemainingAsync();
user1Results.AddRange(user2Results);
return user1Results.OrderBy(x => x.UpdateDt).ToList();
}
}