Skip to content

Commit

Permalink
Merge pull request #4 from HammerheadShark666/fix-null-issues
Browse files Browse the repository at this point in the history
Fix null issues
  • Loading branch information
HammerheadShark666 authored Aug 30, 2024
2 parents f190de5 + 9aa2f3b commit 839cc63
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ namespace Microservice.CustomerAddress.Grpc.Data.Repository;

public class CustomerAddressRepository(IDbContextFactory<CustomerAddressDbContext> dbContextFactory) : ICustomerAddressRepository
{
public IDbContextFactory<CustomerAddressDbContext> _dbContextFactory { get; set; } = dbContextFactory;

public async Task<Grpc.Domain.CustomerAddress> ByIdAsync(Guid customerId, Guid addressId)
{
await using var db = await _dbContextFactory.CreateDbContextAsync();
await using var db = await dbContextFactory.CreateDbContextAsync();
return await db.CustomerAddresses
.Where(o => o.Id.Equals(addressId) && o.CustomerId.Equals(customerId))
.Include(e => e.Country)
Expand Down
20 changes: 16 additions & 4 deletions Microservice.CustomerAddress.Grpc/Helpers/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
namespace Microservice.CustomerAddress.Grpc.Helpers;
using Microservice.CustomerAddress.Grpc.Helpers.Exceptions;

namespace Microservice.CustomerAddress.Grpc.Helpers;

public class EnvironmentVariablesHelper
{
public static string JwtIssuer = Environment.GetEnvironmentVariable(Constants.JwtIssuer);
public static string JwtAudience = Environment.GetEnvironmentVariable(Constants.JwtAudience);
public static string JwtSymmetricSecurityKey = Environment.GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey);
public static string JwtIssuer => GetEnvironmentVariable(Constants.JwtIssuer);
public static string JwtAudience => GetEnvironmentVariable(Constants.JwtAudience);
public static string JwtSymmetricSecurityKey => GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey);

public static string GetEnvironmentVariable(string name)
{
var variable = Environment.GetEnvironmentVariable(name);

if (string.IsNullOrEmpty(variable))
throw new EnvironmentVariableNotFoundException($"Environment Variable Not Found: {name}.");

return variable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Microservice.CustomerAddress.Grpc.Helpers.Exceptions;

public class EnvironmentVariableNotFoundException : Exception
{
public EnvironmentVariableNotFoundException()
{
}

public EnvironmentVariableNotFoundException(string message)
: base(message)
{
}

public EnvironmentVariableNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
}
catch (RpcNotFoundException)
{
_logger.LogError($"Error thrown by {context.Method}.");
_logger.LogError("Error thrown by {context.Method}.", context.Method);
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error thrown by {context.Method}.");
_logger.LogError(ex, "Error thrown by {context.Method}.", context.Method);
throw new RpcException(new Status(StatusCode.Internal, ex.ToString()));
}
}
Expand All @@ -35,7 +35,7 @@ private void LogMethodCall<TRequest, TResponse>(TRequest request, ServerCallCont
switch (context.Method)
{
case "/CustomerAddress.CustomerAddressGrpc/GetCustomerAddress":
_logger.LogInformation($"Call to Method: {context.Method}. Request: {request}");
_logger.LogInformation("Call to Method: {context.Method}. Request: {request}", context.Method, request);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
_logger.LogError(e, "{e.Message}", e.Message);
await HandleExceptionAsync(context, e);
}
}
Expand Down Expand Up @@ -61,5 +61,4 @@ private static int GetStatusCode(Exception exception) =>
NotFoundException => StatusCodes.Status404NotFound,
_ => StatusCodes.Status500InternalServerError
};

}

0 comments on commit 839cc63

Please sign in to comment.