From 6d0e45f4898dbf885f898513c4b72e38934c71c9 Mon Sep 17 00:00:00 2001 From: RobKraft Date: Fri, 13 Dec 2024 20:30:20 -0600 Subject: [PATCH] Refactoring and package updates to all --- .../GetCharityTypes/GetCharityTypes.csproj | 3 +- Lambdas/GetItemTypes/Function.cs | 32 +++----------- Lambdas/GetItemTypes/GetItemTypes.csproj | 6 ++- Lambdas/GetOrgItems/Function.cs | 43 ++++++------------- Lambdas/GetOrgItems/GetOrgItems.csproj | 7 ++- Lambdas/GetOrgs/Function.cs | 33 +++----------- Lambdas/GetOrgs/GetOrgs.csproj | 6 ++- .../ReusefullCommonLibrary.csproj | 4 +- 8 files changed, 42 insertions(+), 92 deletions(-) diff --git a/Lambdas/GetCharityTypes/GetCharityTypes.csproj b/Lambdas/GetCharityTypes/GetCharityTypes.csproj index 363c06b..9336af6 100644 --- a/Lambdas/GetCharityTypes/GetCharityTypes.csproj +++ b/Lambdas/GetCharityTypes/GetCharityTypes.csproj @@ -26,10 +26,9 @@ - - + diff --git a/Lambdas/GetItemTypes/Function.cs b/Lambdas/GetItemTypes/Function.cs index 82433e0..8d2c9ba 100644 --- a/Lambdas/GetItemTypes/Function.cs +++ b/Lambdas/GetItemTypes/Function.cs @@ -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; @@ -12,27 +11,9 @@ 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> handler = async (context) => { return await FunctionHandler(context); @@ -40,16 +21,17 @@ private static async Task Main(string[] args) await LambdaBootstrapBuilder.Create( handler, - new SourceGeneratorLambdaJsonSerializer() + new SourceGeneratorLambdaJsonSerializer() ).Build().RunAsync(); } public static async Task FunctionHandler(ILambdaContext context) { + string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString(); List types = new List(); try { - using (MySqlConnection connection = new MySqlConnection(_connectionString)) + using (MySqlConnection connection = new MySqlConnection(connectionString)) { await connection.OpenAsync(); @@ -72,9 +54,9 @@ public static async Task 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; } @@ -83,7 +65,7 @@ public static async Task FunctionHandler(ILambdaContext context) [JsonSerializable(typeof(APIGatewayProxyResponse))] [JsonSerializable(typeof(ItemType))] [JsonSerializable(typeof(List))] -public partial class CustomSerializer : JsonSerializerContext +public partial class ItemTypeSerializer : JsonSerializerContext { } diff --git a/Lambdas/GetItemTypes/GetItemTypes.csproj b/Lambdas/GetItemTypes/GetItemTypes.csproj index 0b152f9..0c94b0f 100644 --- a/Lambdas/GetItemTypes/GetItemTypes.csproj +++ b/Lambdas/GetItemTypes/GetItemTypes.csproj @@ -16,10 +16,12 @@ - + - + + + \ No newline at end of file diff --git a/Lambdas/GetOrgItems/Function.cs b/Lambdas/GetOrgItems/Function.cs index 62aeaa4..8e85f1e 100644 --- a/Lambdas/GetOrgItems/Function.cs +++ b/Lambdas/GetOrgItems/Function.cs @@ -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; @@ -12,27 +11,8 @@ 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> handler = async (context) => { return await FunctionHandler(context); @@ -40,16 +20,17 @@ private static async Task Main(string[] args) await LambdaBootstrapBuilder.Create( handler, - new SourceGeneratorLambdaJsonSerializer() + new SourceGeneratorLambdaJsonSerializer() ).Build().RunAsync(); } public static async Task FunctionHandler(ILambdaContext context) { - List types = new List(); + string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString(); + List types = new List(); try { - using (MySqlConnection connection = new MySqlConnection(_connectionString)) + using (MySqlConnection connection = new MySqlConnection(connectionString)) { await connection.OpenAsync(); @@ -60,7 +41,7 @@ public static async Task FunctionHandler(ILambdaContext context) { while (await reader.ReadAsync()) { - types.Add(new ItemType + types.Add(new CharityToItemType { CharityName = reader.GetString("CharityName"), ItemName = reader.GetString("ItemName"), @@ -74,9 +55,9 @@ public static async Task 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; } @@ -85,12 +66,12 @@ public static async Task FunctionHandler(ILambdaContext context) [JsonSerializable(typeof(APIGatewayProxyRequest))] [JsonSerializable(typeof(APIGatewayProxyResponse))] -[JsonSerializable(typeof(ItemType))] -[JsonSerializable(typeof(List))] -public partial class CustomSerializer : JsonSerializerContext +[JsonSerializable(typeof(CharityToItemType))] +[JsonSerializable(typeof(List))] +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; diff --git a/Lambdas/GetOrgItems/GetOrgItems.csproj b/Lambdas/GetOrgItems/GetOrgItems.csproj index f72f66c..761f3af 100644 --- a/Lambdas/GetOrgItems/GetOrgItems.csproj +++ b/Lambdas/GetOrgItems/GetOrgItems.csproj @@ -13,11 +13,14 @@ - + - + + + + diff --git a/Lambdas/GetOrgs/Function.cs b/Lambdas/GetOrgs/Function.cs index 5e36247..5a8bc59 100644 --- a/Lambdas/GetOrgs/Function.cs +++ b/Lambdas/GetOrgs/Function.cs @@ -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; @@ -12,27 +11,8 @@ 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> handler = async (context) => { return await FunctionHandler(context); @@ -40,16 +20,17 @@ private static async Task Main(string[] args) await LambdaBootstrapBuilder.Create( handler, - new SourceGeneratorLambdaJsonSerializer() + new SourceGeneratorLambdaJsonSerializer() ).Build().RunAsync(); } public static async Task FunctionHandler(ILambdaContext context) { + string connectionString = ReusefullCommonLibrary.DatabaseHelper.GetConnectionString(); List types = new List(); try { - using (MySqlConnection connection = new MySqlConnection(_connectionString)) + using (MySqlConnection connection = new MySqlConnection(connectionString)) { await connection.OpenAsync(); @@ -90,9 +71,9 @@ public static async Task 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; } @@ -102,7 +83,7 @@ public static async Task FunctionHandler(ILambdaContext context) [JsonSerializable(typeof(APIGatewayProxyResponse))] [JsonSerializable(typeof(OrgData))] [JsonSerializable(typeof(List))] -public partial class CustomSerializer : JsonSerializerContext +public partial class OrgDataSerializer : JsonSerializerContext { } diff --git a/Lambdas/GetOrgs/GetOrgs.csproj b/Lambdas/GetOrgs/GetOrgs.csproj index 0b152f9..0c94b0f 100644 --- a/Lambdas/GetOrgs/GetOrgs.csproj +++ b/Lambdas/GetOrgs/GetOrgs.csproj @@ -16,10 +16,12 @@ - + - + + + \ No newline at end of file diff --git a/Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.csproj b/Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.csproj index ba916b1..9465628 100644 --- a/Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.csproj +++ b/Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.csproj @@ -9,9 +9,9 @@ - + - +