Skip to content

Commit

Permalink
Merge pull request #8 from HammerheadShark666/add-log-messages
Browse files Browse the repository at this point in the history
Add log messages
  • Loading branch information
HammerheadShark666 authored Aug 20, 2024
2 parents bf07c3a + 783d3d3 commit 68cf491
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
19 changes: 15 additions & 4 deletions Microservice.Customer.Api/Endpoints/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Asp.Versioning;
using FluentValidation;
using MediatR;
using Microservice.Customer.Api.Extensions;
using Microservice.Customer.Api.Helpers.Exceptions;
Expand All @@ -19,14 +20,15 @@ public static void ConfigureRoutes(this WebApplication app, ConfigurationManager
{
var customerGroup = app.MapGroup("v{version:apiVersion}/customers").WithTags("customers");

customerGroup.MapGet("/logged-in", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
async ([FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor) =>
customerGroup.MapGet("/logged-in", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
async ([FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor) =>
{
var getCustomerResponse = await mediator.Send(new GetCustomerRequest(customerHttpAccessor.CustomerId));
return Results.Ok(getCustomerResponse);
})
.Produces<GetCustomerResponse>((int)HttpStatusCode.OK)
.Produces<BadRequestException>((int)HttpStatusCode.BadRequest)
.Produces<ValidationException>((int)HttpStatusCode.BadRequest)
.WithName("GetCustomer")
.WithApiVersionSet(app.GetApiVersionSet())
.MapToApiVersion(new ApiVersion(1, 0))
Expand All @@ -38,15 +40,24 @@ public static void ConfigureRoutes(this WebApplication app, ConfigurationManager
});

customerGroup.MapPut("/update", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
async ([FromBody] UpdateCustomerRequest updateCustomerRequest, [FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor) =>
async ([FromBody] UpdateCustomerRequest updateCustomerRequest, [FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor, ILogger logger) =>
{
logger.LogInformation("Start - Update customer: {0}", updateCustomerRequest.Id);

updateCustomerRequest = updateCustomerRequest with { Id = customerHttpAccessor.CustomerId };
var updateCustomerResponse = await mediator.Send(updateCustomerRequest);

logger.LogInformation("End - Update customer: {0}", updateCustomerRequest.Id);

return Results.Ok(updateCustomerResponse);
})
.Accepts<UpdateCustomerRequest>("application/json")
.Accepts<UpdateCustomerRequest>("application/json")
.Produces<UpdateCustomerResponse>((int)HttpStatusCode.OK)
.Produces<UpdateCustomerResponse>((int)HttpStatusCode.OK)
.Produces<BadRequestException>((int)HttpStatusCode.BadRequest)
.Produces<ValidationException>((int)HttpStatusCode.BadRequest)
.Produces<ArgumentException>((int)HttpStatusCode.BadRequest)
.Produces<NotFoundException>((int)HttpStatusCode.BadRequest)
.WithName("UpdateCustomer")
.WithApiVersionSet(app.GetApiVersionSet())
.MapToApiVersion(new ApiVersion(1, 0))
Expand Down
13 changes: 11 additions & 2 deletions Microservice.Customer.Api/Extensions/AppExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Asp.Versioning;
using Asp.Versioning.Builder;
using Microservice.Customer.Api.Middleware;

namespace Microservice.Customer.Api.Extensions;

Expand All @@ -25,7 +26,7 @@ public static void ConfigureSwagger(this WebApplication app)
}
});
}
}
}

public static ApiVersionSet GetApiVersionSet(this WebApplication app)
{
Expand All @@ -34,4 +35,12 @@ public static ApiVersionSet GetApiVersionSet(this WebApplication app)
.ReportApiVersions()
.Build();
}
}

public static void ConfigureMiddleware(this WebApplication app)
{
if (!app.Environment.IsDevelopment())
{
app.UseMiddleware<ExceptionHandlingMiddleware>();
}
}
}
9 changes: 2 additions & 7 deletions Microservice.Customer.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@
builder.Services.ConfigureMediatr();
builder.Services.ConfigureSwagger();


var app = builder.Build();

app.ConfigureSwagger();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

if (!app.Environment.IsDevelopment())
{
app.UseMiddleware<ExceptionHandlingMiddleware>();
}
app.UseAuthorization();
app.ConfigureMiddleware();

Endpoints.ConfigureRoutes(app, builder.Configuration);

Expand Down

0 comments on commit 68cf491

Please sign in to comment.