Skip to content

Commit

Permalink
fix save product validation
Browse files Browse the repository at this point in the history
  • Loading branch information
robsmitha committed Aug 31, 2024
1 parent 6992064 commit a1a4a30
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/Application/Behaviors/LoggingBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
using Elysian.Application.Interfaces;
using MediatR;
using MediatR.Pipeline;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

namespace Elysian.Application.Behaviors
{
public class LoggingBehavior<TRequest>(
public class LoggingBehavior<TRequest, TResponse>(
ILogger<TRequest> logger, IClaimsPrincipalAccessor claimsPrincipalAccessor)
: IRequestPreProcessor<TRequest>
: IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly ILogger _logger = logger;

public async Task Process(
TRequest request,
CancellationToken cancellationToken)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var requestName = typeof(TRequest).Name;
var user = claimsPrincipalAccessor.IsAuthenticated ? claimsPrincipalAccessor.UserId : "Anonymous";

_logger.LogInformation($"Application Request [User: {user}, Claims: {claimsPrincipalAccessor.Claims}, RequestName: {requestName}, Payload: {request}]");
await Task.FromResult(0);

return await next();
}
}
}
2 changes: 1 addition & 1 deletion src/Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(PerformanceBehavior<,>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ApiExceptionBehavior<,>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehavior<,>));
cfg.AddOpenRequestPreProcessor(typeof(LoggingBehavior<>));
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
});

services.AddValidatorsFromAssembly(assembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public async Task<bool> BeUniqueSerialNumber(SaveProductRequest saveProductReque
var query = _context.Products.Where(c => !c.IsDeleted && c.SerialNumber == saveProductRequest.SerialNumber);

return saveProductRequest.ProductId.HasValue
? await query.AnyAsync(c => c.ProductId != saveProductRequest.ProductId, cancellationToken)
: await query.AnyAsync(cancellationToken);
? !await query.AnyAsync(c => c.ProductId != saveProductRequest.ProductId, cancellationToken)
: !await query.AnyAsync(cancellationToken);

}

Expand Down

0 comments on commit a1a4a30

Please sign in to comment.