Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

## [v6.0.0](https://github.com/Genocs/genocs-library/compare/6.0.0-preview.2.0...v6.0.0) - 2024-07-31

### Commits

- Refactor and update dependencies and logging level [`63533a2`](https://github.com/Genocs/genocs-library/commit/63533a2ccfb64106dd81e296ca1a1de55c556f48)
- Refactor auth and update docs [`a63d816`](https://github.com/Genocs/genocs-library/commit/a63d816a4b617246ec99b3400d019a1b46ff40b0)
- Update packages, fix typos, and improve code formatting [`d634694`](https://github.com/Genocs/genocs-library/commit/d6346942c88195b831bd66f4ff8a1e039695e0b2)

## [6.0.0-preview.2.0](https://github.com/Genocs/genocs-library/compare/v5.0.0-preview.4.0...6.0.0-preview.2.0) - 2024-07-19

### Merged
Expand Down
2 changes: 1 addition & 1 deletion src/Genocs.Core.Demo.WebApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"displayVersion": true
},
"logger": {
"level": "information",
"level": "debug",
"applicationName": "demo-service",
"excludePaths": [ "/ping", "/metrics" ],
"console": {
Expand Down
2 changes: 1 addition & 1 deletion src/Genocs.Core.Demo.Worker/Genocs.Core.Demo.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ItemGroup>
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.3.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Genocs.Core.UnitTests/Genocs.Core.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions src/Genocs.Core/Genocs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Genocs.Logging/Genocs.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SmartFormat.NET" Version="3.4.0" />
<PackageReference Include="SmartFormat.NET" Version="3.5.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.4" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Genocs.Secrets.AzureKeyVault.Configurations;

/// <summary>
/// The vault Setting definition.
/// The Azure Kay Vault setting definition.
/// </summary>
public class AzureKeyVaultOptions
{
Expand Down
15 changes: 10 additions & 5 deletions src/Genocs.Secrets.AzureKeyVault/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public static IHostBuilder UseAzureKeyVault(
}

cfg.AddAzureKeyVault(
new Uri($"https://{settings.Name}.vault.azure.net/"),
new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
ManagedIdentityClientId = settings.ManagedIdentityId
}));
new Uri($"https://{settings.Name}.vault.azure.net/"),
new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
ManagedIdentityClientId = settings.ManagedIdentityId
}));
});

/// <summary>
Expand All @@ -56,6 +56,11 @@ public static IWebHostBuilder UseAzureKeyVault(
string sectionName = AzureKeyVaultOptions.Position)
=> builder.ConfigureAppConfiguration((ctx, cfg) =>
{
if (string.IsNullOrWhiteSpace(sectionName))
{
sectionName = AzureKeyVaultOptions.Position;
}

AzureKeyVaultOptions settings = ctx.Configuration.GetOptions<AzureKeyVaultOptions>(sectionName);
if (!settings.Enabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace Genocs.Identities.Application.DTO;

/// <summary>
/// The AuthDto class.
/// </summary>
public class AuthDto
{
public Guid UserId { get; set; }
public string Username { get; set; }
public string Role { get; set; }
public string AccessToken { get; set; }
public string? Role { get; set; }
public string? AccessToken { get; set; }
public string RefreshToken { get; set; }
public long Expires { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ public class RefreshToken : AggregateRoot
public DateTime? RevokedAt { get; private set; }
public bool Revoked => RevokedAt.HasValue;

public RefreshToken(AggregateId id, AggregateId userId, string token, DateTime createdAt,
DateTime? revokedAt = null) : base(id)
public RefreshToken(
AggregateId id,
AggregateId userId,
string token,
DateTime createdAt,
DateTime? revokedAt = null)
: base(id)
{
if (string.IsNullOrWhiteSpace(token))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace Genocs.Identities.Application.Domain.Exceptions;

public class InvalidNameException : DomainException
{
public InvalidNameException(string name) : base($"Invalid name: {name}.")
public InvalidNameException(string name)
: base($"Invalid name: {name}.")
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace Genocs.Identities.Application.Exceptions;

public abstract class AppException : Exception
{
protected AppException(string message) : base(message)
protected AppException(string message)
: base(message)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ public class ExceptionToResponseMapper : IExceptionToResponseMapper
public ExceptionResponse Map(Exception exception)
=> exception switch
{
DomainException ex => new ExceptionResponse(new { code = GetCode(ex), reason = ex.Message },
HttpStatusCode.BadRequest),
AppException ex => new ExceptionResponse(new { code = GetCode(ex), reason = ex.Message },
HttpStatusCode.BadRequest),
_ => new ExceptionResponse(new { code = "error", reason = "There was an error." },
HttpStatusCode.BadRequest)
DomainException ex => new ExceptionResponse(new { code = GetCode(ex), reason = ex.Message }, HttpStatusCode.BadRequest),
AppException ex => new ExceptionResponse(new { code = GetCode(ex), reason = ex.Message }, HttpStatusCode.BadRequest),
_ => new ExceptionResponse(new { code = "error", reason = "There was an error." }, HttpStatusCode.BadRequest)
};

private static string GetCode(Exception exception)
private static string? GetCode(Exception exception)
{
var type = exception.GetType();
if (Codes.TryGetValue(type, out var code))
if (Codes.TryGetValue(type, out string? code))
{
return code;
}

var exceptionCode = exception.GetType().Name.Underscore().Replace("_exception", string.Empty);
Codes.TryAdd(type, exceptionCode);
string? exceptionCode = exception.GetType().Name.Underscore()?.Replace("_exception", string.Empty);
if (!string.IsNullOrWhiteSpace(exceptionCode))
{
Codes.TryAdd(type, exceptionCode);
}

return exceptionCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ public static class Extensions
public static IApplicationBuilder UseMongo(this IApplicationBuilder builder)
{
using var scope = builder.ApplicationServices.CreateScope();
var users = scope.ServiceProvider.GetService<IMongoRepository<UserDocument, Guid>>().Collection;
var users = scope.ServiceProvider.GetService<IMongoRepository<UserDocument, Guid>>()?.Collection;

if (users is null)
{
return builder;
}

var userBuilder = Builders<UserDocument>.IndexKeys;

Task.Run(async () => await users.Indexes.CreateManyAsync(
new[]
{
new CreateIndexModel<UserDocument>(userBuilder.Ascending(i => i.Email),
new CreateIndexModel<UserDocument>(
userBuilder.Ascending(i => i.Email),
new CreateIndexOptions
{
Unique = true
}),
new CreateIndexModel<UserDocument>(userBuilder.Ascending(i => i.Name),
new CreateIndexModel<UserDocument>(
userBuilder.Ascending(i => i.Name),
new CreateIndexOptions
{
Unique = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
namespace Genocs.Identities.Application.Services;

/// <summary>
/// JwtProvider interface definition
/// JwtProvider interface definition.
/// </summary>
public interface IJwtProvider
{
AuthDto Create(Guid userId, string username, string role, string? audience = null,
IDictionary<string, IEnumerable<string>>? claims = null);
AuthDto Create(
Guid userId,
string username,
string role,
string? audience = null,
IDictionary<string,
IEnumerable<string>>? claims = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public JwtProvider(IJwtHandler jwtHandler)
_jwtHandler = jwtHandler;
}

public AuthDto Create(Guid userId,
string username,
string role,
string? audience = null,
IDictionary<string, IEnumerable<string>>? claims = null)
public AuthDto Create(
Guid userId,
string username,
string role,
string? audience = null,
IDictionary<string, IEnumerable<string>>? claims = null)
{
var jwt = _jwtHandler.CreateToken(userId.ToString("N"), role, audience, claims);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ internal class MessageBroker : IMessageBroker
private readonly ILogger<IMessageBroker> _logger;
private readonly string _spanContextHeader;

public MessageBroker(IBusPublisher busPublisher, IMessageOutbox outbox,
ICorrelationContextAccessor contextAccessor, IHttpContextAccessor httpContextAccessor,
IMessagePropertiesAccessor messagePropertiesAccessor, ICorrelationIdFactory correlationIdFactory,
RabbitMQOptions options, ILogger<IMessageBroker> logger)
public MessageBroker(
IBusPublisher busPublisher,
IMessageOutbox outbox,
ICorrelationContextAccessor contextAccessor,
IHttpContextAccessor httpContextAccessor,
IMessagePropertiesAccessor messagePropertiesAccessor,
ICorrelationIdFactory correlationIdFactory,
RabbitMQOptions options,
ILogger<IMessageBroker> logger)
{
if (options is null)
{
Expand Down Expand Up @@ -53,11 +58,12 @@ private async Task PublishAsync(IEnumerable<IEvent>? events)
}

var messageProperties = _messagePropertiesAccessor.MessageProperties;
var originatedMessageId = messageProperties?.MessageId;
var correlationId = _correlationIdFactory.Create();
var spanContext = messageProperties?.GetSpanContext(_spanContextHeader);
var correlationContext = _contextAccessor.CorrelationContext ??
string? originatedMessageId = messageProperties?.MessageId;
string? correlationId = _correlationIdFactory.Create();
string? spanContext = messageProperties?.GetSpanContext(_spanContextHeader);
object correlationContext = _contextAccessor.CorrelationContext ??
_httpContextAccessor.GetCorrelationContext();

var headers = new Dictionary<string, object>();

foreach (var @event in events)
Expand All @@ -67,11 +73,12 @@ private async Task PublishAsync(IEnumerable<IEvent>? events)
continue;
}

var messageId = Guid.NewGuid().ToString("N");
string messageId = Guid.NewGuid().ToString("N");
_logger.LogTrace($"Publishing integration event: {@event.GetType().Name.Underscore()} [ID: '{messageId}'].");
if (_outbox.Enabled)
{
await _outbox.SendAsync(@event,
await _outbox.SendAsync(
@event,
originatedMessageId,
messageId,
correlationId,
Expand All @@ -81,12 +88,13 @@ await _outbox.SendAsync(@event,
continue;
}

await _busPublisher.PublishAsync(@event,
messageId,
correlationId,
spanContext,
correlationContext,
headers);
await _busPublisher.PublishAsync(
@event,
messageId,
correlationId,
spanContext,
correlationContext,
headers);
}
}
}
3 changes: 1 addition & 2 deletions src/apps/orders/Genocs.Orders.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@
.Get(string.Empty, ctx => ctx.Response.WriteAsync("Orders Service"))
.Get("ping", ctx => ctx.Response.WriteAsync("pong"))
.Get<GetOrder, OrderDto>("orders/{orderId}")
.Post<CreateOrder>("orders",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"orders/{cmd.OrderId}")))
.Post<CreateOrder>("orders", afterDispatch: (cmd, ctx) => ctx.Response.Created($"orders/{cmd.OrderId}")))
.UseJaeger()
.UseSwaggerDocs()
.UseRabbitMq()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public GetOrderHandler(IMongoRepository<Order, Guid> repository)
}

/// <summary>
/// GetOrder query handler
/// GetOrder query handler.
/// </summary>
/// <param name="query">The query</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <param name="query">The query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<OrderDto?> HandleAsync(GetOrder query, CancellationToken cancellationToken = default)
{
Expand Down
Loading