Skip to content

Commit

Permalink
refactor: Add Swashbuckle.AspNetCore.ReDoc package
Browse files Browse the repository at this point in the history
chore: Update logging message in BasketService

feat: Add CreatedAt property to Feedback domain model

fix: Add DateOnlyJsonConverter to JsonOptions

refactor: Rename Swagger.Extension.cs to OpenApi.Extension.cs
  • Loading branch information
foxminchan committed Sep 16, 2024
1 parent 90e2572 commit 818d5dc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<PackageVersion Include="Marten.AspNetCore" Version="7.27.0" />
<PackageVersion Include="FluentEmail.Mailtrap" Version="2.7.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.7.3" />
<PackageVersion Include="Swashbuckle.AspNetCore.ReDoc" Version="6.7.3" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageVersion Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/BookWorm.Basket/Grpc/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override async Task<BasketResponse> GetBasket(Empty request, ServerCallCo

if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("[{Service}] - - Begin grpc call {Method} with {BasketId}",
logger.LogDebug("[{Service}] - Begin grpc call {Method} with {BasketId}",
nameof(BasketService), nameof(GetBasket), userId);
}

Expand Down
1 change: 1 addition & 0 deletions src/BookWorm.Rating/Domain/Feedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public sealed class Feedback(Guid bookId, int rating, string? comment, Guid user
public string? Comment { get; private set; } = comment;
public Guid UserId { get; private set; } = Guard.Against.Default(userId);
public bool IsHidden { get; private set; }
public DateOnly CreatedAt { get; private set; } = DateOnly.FromDateTime(DateTime.UtcNow);

public void Hide()
{
Expand Down
1 change: 1 addition & 0 deletions src/BookWorm.Rating/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.PropertyNameCaseInsensitive = true;
options.SerializerOptions.Converters.Add(new DateOnlyJsonConverter());
options.SerializerOptions.Converters.Add(new StringTrimmerJsonConverter());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BookWorm.Rating.IntegrationEvents.EventHandlers;

internal sealed class FeedbackCreatedFailedIntegrationEventHandler(
IMongoCollection<Feedback> collection,
IRatingRepository repository,
ILogger<FeedbackCreatedFailedIntegrationEventHandler> logger) : IConsumer<FeedbackCreatedFailedIntegrationEvent>
{
public async Task Consume(ConsumeContext<FeedbackCreatedFailedIntegrationEvent> context)
Expand All @@ -15,11 +15,13 @@ public async Task Consume(ConsumeContext<FeedbackCreatedFailedIntegrationEvent>

var id = ObjectId.Parse(@event.FeedbackId);

var feedback = await collection.Find(f => f.Id == id).FirstOrDefaultAsync();
var filter = Builders<Feedback>.Filter.Eq(x => x.Id, id);

var feedback = await repository.GetAsync(filter, context.CancellationToken);

Guard.Against.NotFound(id, feedback);

await collection.DeleteOneAsync(f => f.Id == id);
await repository.DeleteAsync(filter, context.CancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ItemGroup>
<PackageReference Include="MongoDB.Bson" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Swashbuckle.AspNetCore.ReDoc" />
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BookWorm.ServiceDefaults;

public static class SwaggerExtension
public static class OpenApiExtension
{
public static IHostApplicationBuilder AddOpenApi(this IHostApplicationBuilder builder)
{
Expand Down Expand Up @@ -36,37 +36,49 @@ public static WebApplication UseOpenApi(this WebApplication app)
];
}));

if (!app.Environment.IsDevelopment())
{
return app;
}

app.UseSwaggerUI(options =>
app.UseReDoc(options =>
{
app.DescribeApiVersions()
.Select(desc => new
{
url = $"/swagger/{desc.GroupName}/swagger.json", name = desc.GroupName.ToUpperInvariant()
})
.Select(desc => $"/swagger/{desc.GroupName}/swagger.json")
.ToList()
.ForEach(endpoint => options.SwaggerEndpoint(endpoint.url, endpoint.name));
.ForEach(spec => options.SpecUrl(spec));
var auth = app.Configuration.GetSection(nameof(OpenApi)).Get<OpenApi>()?.Auth;
options.EnableUntrustedSpec();
});

if (auth is null)
if (!app.Environment.IsDevelopment())
{
app.UseSwaggerUI(options =>
{
return;
}
app.DescribeApiVersions()
.Select(desc => new
{
url = $"/swagger/{desc.GroupName}/swagger.json", name = desc.GroupName.ToUpperInvariant()
})
.ToList()
.ForEach(endpoint => options.SwaggerEndpoint(endpoint.url, endpoint.name));
options.DocumentTitle = auth.AppName;
options.OAuthClientId(auth.ClientId);
options.OAuthClientSecret(auth.ClientSecret);
options.OAuthAppName(auth.AppName);
options.OAuthUsePkce();
options.EnableValidator();
});
var auth = app.Configuration.GetSection(nameof(OpenApi)).Get<OpenApi>()?.Auth;
if (auth is null)
{
return;
}
app.MapGet("/", () => Results.Redirect("/swagger")).ExcludeFromDescription();
options.DocumentTitle = auth.AppName;
options.OAuthClientId(auth.ClientId);
options.OAuthClientSecret(auth.ClientSecret);
options.OAuthAppName(auth.AppName);
options.OAuthUsePkce();
options.EnableValidator();
});

app.MapGet("/", () => Results.Redirect("/swagger")).ExcludeFromDescription();
}
else
{
app.MapGet("/", () => Results.Redirect("/api-docs")).ExcludeFromDescription();
}

return app;
}
Expand Down

0 comments on commit 818d5dc

Please sign in to comment.