From 818d5dcc37f06f6d63fe49f5748e384394e19304 Mon Sep 17 00:00:00 2001 From: Nguyen Xuan Nhan Date: Mon, 16 Sep 2024 13:04:37 +0700 Subject: [PATCH] refactor: Add Swashbuckle.AspNetCore.ReDoc package 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 --- Directory.Packages.props | 1 + src/BookWorm.Basket/Grpc/BasketService.cs | 2 +- src/BookWorm.Rating/Domain/Feedback.cs | 1 + src/BookWorm.Rating/Extensions/Extensions.cs | 1 + ...ackCreatedFailedIntegrationEventHandler.cs | 8 ++- .../BookWorm.ServiceDefaults.csproj | 1 + ...gger.Extension.cs => OpenApi.Extension.cs} | 60 +++++++++++-------- 7 files changed, 46 insertions(+), 28 deletions(-) rename src/BookWorm.ServiceDefaults/{Swagger.Extension.cs => OpenApi.Extension.cs} (51%) diff --git a/Directory.Packages.props b/Directory.Packages.props index d3d14df..9b5c6f7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -99,6 +99,7 @@ + diff --git a/src/BookWorm.Basket/Grpc/BasketService.cs b/src/BookWorm.Basket/Grpc/BasketService.cs index 7a41629..578a542 100644 --- a/src/BookWorm.Basket/Grpc/BasketService.cs +++ b/src/BookWorm.Basket/Grpc/BasketService.cs @@ -20,7 +20,7 @@ public override async Task 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); } diff --git a/src/BookWorm.Rating/Domain/Feedback.cs b/src/BookWorm.Rating/Domain/Feedback.cs index 2c18c54..291e4b6 100644 --- a/src/BookWorm.Rating/Domain/Feedback.cs +++ b/src/BookWorm.Rating/Domain/Feedback.cs @@ -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() { diff --git a/src/BookWorm.Rating/Extensions/Extensions.cs b/src/BookWorm.Rating/Extensions/Extensions.cs index 364e763..9e82470 100644 --- a/src/BookWorm.Rating/Extensions/Extensions.cs +++ b/src/BookWorm.Rating/Extensions/Extensions.cs @@ -9,6 +9,7 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.Configure(options => { options.SerializerOptions.PropertyNameCaseInsensitive = true; + options.SerializerOptions.Converters.Add(new DateOnlyJsonConverter()); options.SerializerOptions.Converters.Add(new StringTrimmerJsonConverter()); }); diff --git a/src/BookWorm.Rating/IntegrationEvents/EventHandlers/FeedbackCreatedFailedIntegrationEventHandler.cs b/src/BookWorm.Rating/IntegrationEvents/EventHandlers/FeedbackCreatedFailedIntegrationEventHandler.cs index 47e726b..c686b97 100644 --- a/src/BookWorm.Rating/IntegrationEvents/EventHandlers/FeedbackCreatedFailedIntegrationEventHandler.cs +++ b/src/BookWorm.Rating/IntegrationEvents/EventHandlers/FeedbackCreatedFailedIntegrationEventHandler.cs @@ -3,7 +3,7 @@ namespace BookWorm.Rating.IntegrationEvents.EventHandlers; internal sealed class FeedbackCreatedFailedIntegrationEventHandler( - IMongoCollection collection, + IRatingRepository repository, ILogger logger) : IConsumer { public async Task Consume(ConsumeContext context) @@ -15,11 +15,13 @@ public async Task Consume(ConsumeContext var id = ObjectId.Parse(@event.FeedbackId); - var feedback = await collection.Find(f => f.Id == id).FirstOrDefaultAsync(); + var filter = Builders.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); } } diff --git a/src/BookWorm.ServiceDefaults/BookWorm.ServiceDefaults.csproj b/src/BookWorm.ServiceDefaults/BookWorm.ServiceDefaults.csproj index 6bdc6cf..a92d36c 100644 --- a/src/BookWorm.ServiceDefaults/BookWorm.ServiceDefaults.csproj +++ b/src/BookWorm.ServiceDefaults/BookWorm.ServiceDefaults.csproj @@ -19,6 +19,7 @@ + diff --git a/src/BookWorm.ServiceDefaults/Swagger.Extension.cs b/src/BookWorm.ServiceDefaults/OpenApi.Extension.cs similarity index 51% rename from src/BookWorm.ServiceDefaults/Swagger.Extension.cs rename to src/BookWorm.ServiceDefaults/OpenApi.Extension.cs index ac4ba63..437f7ea 100644 --- a/src/BookWorm.ServiceDefaults/Swagger.Extension.cs +++ b/src/BookWorm.ServiceDefaults/OpenApi.Extension.cs @@ -2,7 +2,7 @@ namespace BookWorm.ServiceDefaults; -public static class SwaggerExtension +public static class OpenApiExtension { public static IHostApplicationBuilder AddOpenApi(this IHostApplicationBuilder builder) { @@ -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()?.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()?.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; }