Skip to content

Commit

Permalink
Refactoring and package updates to all
Browse files Browse the repository at this point in the history
  • Loading branch information
RobKraft committed Dec 14, 2024
1 parent 5d480f7 commit 6d0e45f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 92 deletions.
3 changes: 1 addition & 2 deletions Lambdas/GetCharityTypes/GetCharityTypes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68.2.0.9" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.1" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.2" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
Expand Down
32 changes: 7 additions & 25 deletions Lambdas/GetItemTypes/Function.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Amazon;
// Ignore Spelling: Serializer
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Amazon.RDS.Util;
using MySqlConnector;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -12,44 +11,27 @@ namespace GetItemTypes;

public class Function
{
static string _dbHost = Environment.GetEnvironmentVariable("DB_HOST") ?? "reusefull.cq0mnx0ystdx.us-east-2.rds.amazonaws.com";
static string _dbName = Environment.GetEnvironmentVariable("DB_NAME") ?? "reusefull";
static int _dbPort = int.Parse(Environment.GetEnvironmentVariable("DB_PORT") ?? "3306");
static RegionEndpoint _dbRegion = RegionEndpoint.GetBySystemName(Environment.GetEnvironmentVariable("DB_REGION") ?? "us-east-2");
static string _dbUser = "reusefullrds";
private static string _connectionString = string.Empty;

private static async Task Main(string[] args)
{
#if DEBUG
_dbUser = "";
string pwd = "";
_connectionString = $"Server={_dbHost};Database={_dbName};Port={_dbPort};User Id={_dbUser};Password={pwd};SSL Mode=Required;";
var result = await FunctionHandler(null);
return;
#endif
string authToken = RDSAuthTokenGenerator.GenerateAuthToken(_dbRegion, _dbHost, _dbPort, _dbUser);

_connectionString = $"Server={_dbHost};Port={_dbPort};Database={_dbName};" +
$"User={_dbUser};Password={authToken};SSL Mode=Required;";

Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
};

await LambdaBootstrapBuilder.Create(
handler,
new SourceGeneratorLambdaJsonSerializer<CustomSerializer>()
new SourceGeneratorLambdaJsonSerializer<ItemTypeSerializer>()
).Build().RunAsync();
}

public static async Task<string> FunctionHandler(ILambdaContext context)
{
string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString();
List<ItemType> types = new List<ItemType>();
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();

Expand All @@ -72,9 +54,9 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
}
catch (Exception ex)
{
Console.WriteLine($"connstring={_connectionString} and error: {ex.Message}");
Console.WriteLine($"connstring={connectionString} and error: {ex.Message}");
}
string serializedTypes = JsonSerializer.Serialize(types, CustomSerializer.Default.ListItemType);
string serializedTypes = JsonSerializer.Serialize(types, ItemTypeSerializer.Default.ListItemType);
//LambdaLogger.Log($"Reusefull Serialized types: {serializedTypes}");
return serializedTypes;
}
Expand All @@ -83,7 +65,7 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
[JsonSerializable(typeof(APIGatewayProxyResponse))]
[JsonSerializable(typeof(ItemType))]
[JsonSerializable(typeof(List<ItemType>))]
public partial class CustomSerializer : JsonSerializerContext
public partial class ItemTypeSerializer : JsonSerializerContext
{
}

Expand Down
6 changes: 4 additions & 2 deletions Lambdas/GetItemTypes/GetItemTypes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.1" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.2" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.1" />
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReusefullCommonLibrary\ReusefullCommonLibrary.csproj" />
</ItemGroup>
</Project>
43 changes: 12 additions & 31 deletions Lambdas/GetOrgItems/Function.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Amazon;
// Ignore Spelling: Serializer
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Amazon.RDS.Util;
using MySqlConnector;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -12,44 +11,26 @@ namespace GetOrgItemTypes;

public class Function
{
static string _dbHost = Environment.GetEnvironmentVariable("DB_HOST") ?? "reusefull.cq0mnx0ystdx.us-east-2.rds.amazonaws.com";
static string _dbName = Environment.GetEnvironmentVariable("DB_NAME") ?? "reusefull";
static int _dbPort = int.Parse(Environment.GetEnvironmentVariable("DB_PORT") ?? "3306");
static RegionEndpoint _dbRegion = RegionEndpoint.GetBySystemName(Environment.GetEnvironmentVariable("DB_REGION") ?? "us-east-2");
static string _dbUser = "reusefullrds";
private static string _connectionString = string.Empty;

private static async Task Main(string[] args)
{
#if DEBUG
_dbUser = "";
string pwd = "";
_connectionString = $"Server={_dbHost};Database={_dbName};Port={_dbPort};User Id={_dbUser};Password={pwd};SSL Mode=Required;";
var result = await FunctionHandler(null);
return;
#endif
string authToken = RDSAuthTokenGenerator.GenerateAuthToken(_dbRegion, _dbHost, _dbPort, _dbUser);

_connectionString = $"Server={_dbHost};Port={_dbPort};Database={_dbName};" +
$"User={_dbUser};Password={authToken};SSL Mode=Required;";

Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
};

await LambdaBootstrapBuilder.Create(
handler,
new SourceGeneratorLambdaJsonSerializer<CustomSerializer>()
new SourceGeneratorLambdaJsonSerializer<CharityToItemTypeSerializer>()
).Build().RunAsync();
}

public static async Task<string> FunctionHandler(ILambdaContext context)
{
List<ItemType> types = new List<ItemType>();
string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString();
List<CharityToItemType> types = new List<CharityToItemType>();
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();

Expand All @@ -60,7 +41,7 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
while (await reader.ReadAsync())
{
types.Add(new ItemType
types.Add(new CharityToItemType
{
CharityName = reader.GetString("CharityName"),
ItemName = reader.GetString("ItemName"),
Expand All @@ -74,9 +55,9 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
}
catch (Exception ex)
{
Console.WriteLine($"connstring={_connectionString} and error: {ex.Message}");
Console.WriteLine($"connstring={connectionString} and error: {ex.Message}");
}
string serializedTypes = JsonSerializer.Serialize(types, CustomSerializer.Default.ListItemType);
string serializedTypes = JsonSerializer.Serialize(types, CharityToItemTypeSerializer.Default.CharityToItemType);
//LambdaLogger.Log($"Reusefull Serialized types: {serializedTypes}");
return serializedTypes;
}
Expand All @@ -85,12 +66,12 @@ public static async Task<string> FunctionHandler(ILambdaContext context)

[JsonSerializable(typeof(APIGatewayProxyRequest))]
[JsonSerializable(typeof(APIGatewayProxyResponse))]
[JsonSerializable(typeof(ItemType))]
[JsonSerializable(typeof(List<ItemType>))]
public partial class CustomSerializer : JsonSerializerContext
[JsonSerializable(typeof(CharityToItemType))]
[JsonSerializable(typeof(List<CharityToItemType>))]
public partial class CharityToItemTypeSerializer : JsonSerializerContext
{
}
public class ItemType
public class CharityToItemType
{
public string CharityName { get; set; } = string.Empty;
public string ItemName { get; set; } = string.Empty;
Expand Down
7 changes: 5 additions & 2 deletions Lambdas/GetOrgItems/GetOrgItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.1" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.2" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.1" />
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReusefullCommonLibrary\ReusefullCommonLibrary.csproj" />
</ItemGroup>

</Project>
33 changes: 7 additions & 26 deletions Lambdas/GetOrgs/Function.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Amazon;
// Ignore Spelling: Serializer
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Amazon.RDS.Util;
using MySqlConnector;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -12,44 +11,26 @@ namespace GetOrgs;

public class Function
{
static string _dbHost = Environment.GetEnvironmentVariable("DB_HOST") ?? "reusefull.cq0mnx0ystdx.us-east-2.rds.amazonaws.com";
static string _dbName = Environment.GetEnvironmentVariable("DB_NAME") ?? "reusefull";
static int _dbPort = int.Parse(Environment.GetEnvironmentVariable("DB_PORT") ?? "3306");
static RegionEndpoint _dbRegion = RegionEndpoint.GetBySystemName(Environment.GetEnvironmentVariable("DB_REGION") ?? "us-east-2");
static string _dbUser = "reusefullrds";
private static string _connectionString = string.Empty;

private static async Task Main(string[] args)
{
#if DEBUG
_dbUser = "";
string pwd = "";
_connectionString = $"Server={_dbHost};Database={_dbName};Port={_dbPort};User Id={_dbUser};Password={pwd};SSL Mode=Required;";
var result = await FunctionHandler(null);
return;
#endif
string authToken = RDSAuthTokenGenerator.GenerateAuthToken(_dbRegion, _dbHost, _dbPort, _dbUser);

_connectionString = $"Server={_dbHost};Port={_dbPort};Database={_dbName};" +
$"User={_dbUser};Password={authToken};SSL Mode=Required;";

Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
};

await LambdaBootstrapBuilder.Create(
handler,
new SourceGeneratorLambdaJsonSerializer<CustomSerializer>()
new SourceGeneratorLambdaJsonSerializer<OrgDataSerializer>()
).Build().RunAsync();
}

public static async Task<string> FunctionHandler(ILambdaContext context)
{
string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString();
List<OrgData> types = new List<OrgData>();
try
{
using (MySqlConnection connection = new MySqlConnection(_connectionString))
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();

Expand Down Expand Up @@ -90,9 +71,9 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
}
catch (Exception ex)
{
Console.WriteLine($"connstring={_connectionString} and error: {ex.Message}");
Console.WriteLine($"connstring={connectionString} and error: {ex.Message}");
}
string serializedTypes = JsonSerializer.Serialize(types, CustomSerializer.Default.ListOrgData);
string serializedTypes = JsonSerializer.Serialize(types, OrgDataSerializer.Default.ListOrgData);
//LambdaLogger.Log($"Reusefull Serialized types: {serializedTypes}");
return serializedTypes;
}
Expand All @@ -102,7 +83,7 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
[JsonSerializable(typeof(APIGatewayProxyResponse))]
[JsonSerializable(typeof(OrgData))]
[JsonSerializable(typeof(List<OrgData>))]
public partial class CustomSerializer : JsonSerializerContext
public partial class OrgDataSerializer : JsonSerializerContext
{
}

Expand Down
6 changes: 4 additions & 2 deletions Lambdas/GetOrgs/GetOrgs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.1" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.2" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.1" />
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReusefullCommonLibrary\ReusefullCommonLibrary.csproj" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.1" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.12.2" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.1" />
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.6" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>

Expand Down

0 comments on commit 6d0e45f

Please sign in to comment.