Skip to content

Commit

Permalink
Handle nulls in database that prevented returning all Orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
RobKraft committed Dec 17, 2024
1 parent a4ea310 commit 0dfeffc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
7 changes: 6 additions & 1 deletion Lambdas/GetCharityTypes/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Function
{
private static async Task Main(string[] args)
{
#if DEBUG
var result = await FunctionHandler(null);
return;
#endif
Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
Expand All @@ -22,6 +26,7 @@ await LambdaBootstrapBuilder.Create(
handler,
new SourceGeneratorLambdaJsonSerializer<CharityTypeSerializer>()
).Build().RunAsync();

}

public static async Task<string> FunctionHandler(ILambdaContext context)
Expand All @@ -42,7 +47,7 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
types.Add(new CharityType
{
Id = reader.GetInt32("id"),
Id = ReusefullCommonLibrary.DatabaseHelper.SafeGetInt32FromDB(reader, "id"),
Type = reader.GetString("name")
});
}
Expand Down
7 changes: 5 additions & 2 deletions Lambdas/GetItemTypes/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ namespace GetItemTypes;

public class Function
{

private static async Task Main(string[] args)
{
#if DEBUG
var result = await FunctionHandler(null);
return;
#endif
Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
Expand Down Expand Up @@ -44,7 +47,7 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
types.Add(new ItemType
{
Id = reader.GetInt32("id"),
Id = ReusefullCommonLibrary.DatabaseHelper.SafeGetInt32FromDB(reader, "id"),
Name = reader.GetString("name")
});
}
Expand Down
8 changes: 6 additions & 2 deletions Lambdas/GetOrgItems/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Function
{
private static async Task Main(string[] args)
{
#if DEBUG
var result = await FunctionHandler(null);
return;
#endif
Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
Expand Down Expand Up @@ -45,8 +49,8 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
CharityName = reader.GetString("CharityName"),
ItemName = reader.GetString("ItemName"),
CharityId = reader.GetInt32("CharityId"),
ItemId = reader.GetInt32("ItemId")
CharityId = ReusefullCommonLibrary.DatabaseHelper.SafeGetInt32FromDB(reader, "CharityId"),
ItemId = ReusefullCommonLibrary.DatabaseHelper.SafeGetInt32FromDB(reader, "ItemId"),
});
}
}
Expand Down
25 changes: 16 additions & 9 deletions Lambdas/GetOrgs/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Function
{
private static async Task Main(string[] args)
{
#if DEBUG
var result = await FunctionHandler(null);
return;
#endif
Func<ILambdaContext, Task<string>> handler = async (context) =>
{
return await FunctionHandler(context);
Expand Down Expand Up @@ -43,25 +47,28 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
types.Add(new OrgData
{
Id = reader.GetInt32("id"),
Id = ReusefullCommonLibrary.DatabaseHelper.SafeGetInt32FromDB(reader, "id"),
Name = reader.GetString("name"),
Address = reader.GetString("address"),
ZipCode = reader.GetString("zip_code"),
City = reader.GetString("city"),
ContactName = reader.GetString("contact_name"),
Description = reader.GetString("description"),
Dropoff = reader.GetBoolean("dropoff"),
Email = reader.GetString("email"), Faith = reader.GetBoolean("faith"),
GoodItems = reader.GetBoolean("good_items"),
Lat = reader.GetDouble("lat"),
Dropoff = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "dropoff"),
Email = reader.GetString("email"),
Faith = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "faith"),
GoodItems = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "good_items"),
Lat = ReusefullCommonLibrary.DatabaseHelper.SafeGetDoubleFromDB(reader, "lat"),
LinkVolunteer = reader.GetString("link_volunteer"),
LinkWebsite = reader.GetString("link_website"),
LinkWishlist = reader.GetString("link_wishlist"), Lng = reader.GetDouble("lng"),
LinkWishlist = reader.GetString("link_wishlist"),
Lng = ReusefullCommonLibrary.DatabaseHelper.SafeGetDoubleFromDB(reader, "lng"),
LogoUrl = reader.GetString("logo_url"),
Mission = reader.GetString("mission"),
NewItems = reader.GetBoolean("new_items"),
Phone = reader.GetString("phone"), Pickup = reader.GetBoolean("pickup"),
Resell = reader.GetBoolean("resell"),
NewItems = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "new_items"),
Phone = reader.GetString("phone"),
Pickup = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "pickup"),
Resell = ReusefullCommonLibrary.DatabaseHelper.SafeGetBooleanFromDB(reader, "resell"),
State = reader.GetString("state")
});
}
Expand Down
19 changes: 17 additions & 2 deletions Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Amazon;
using Amazon.RDS.Util;
using MySqlConnector;

namespace ReusefullCommonLibrary
{
Expand All @@ -16,15 +17,29 @@ public static string GetConnectionString()
string connectionString = string.Empty;
string pwd = "";

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used

Check warning on line 18 in Lambdas/ReusefullCommonLibrary/ReusefullCommonLibrary.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The variable 'pwd' is assigned but its value is never used
#if DEBUG
_dbUser = "reusefull";
pwd = "";
_dbUser = "";
connectionString = $"Server={_dbHost};Database={_dbName};Port={_dbPort};User Id={_dbUser};Password={pwd};SSL Mode=Required;";
#endif
return connectionString;

string authToken = RDSAuthTokenGenerator.GenerateAuthToken(_dbRegion, _dbHost, _dbPort, _dbUser);

connectionString = $"Server={_dbHost};Port={_dbPort};Database={_dbName};" +
$"User={_dbUser};Password={authToken};SSL Mode=Required;";
return connectionString;
#endif
}
public static Int32 SafeGetInt32FromDB(MySqlDataReader reader, string columnName)
{
return !reader.IsDBNull(reader.GetOrdinal(columnName)) ? reader.GetInt32(columnName) : 0;
}
public static bool SafeGetBooleanFromDB(MySqlDataReader reader, string columnName)
{
return !reader.IsDBNull(reader.GetOrdinal(columnName)) ? reader.GetBoolean(columnName) : false;
}
public static double SafeGetDoubleFromDB(MySqlDataReader reader, string columnName)
{
return !reader.IsDBNull(reader.GetOrdinal(columnName)) ? reader.GetDouble(columnName) : 0.0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="AWSSDK.RDS" Version="3.7.409.6" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
</ItemGroup>

</Project>

0 comments on commit 0dfeffc

Please sign in to comment.