Skip to content

Commit

Permalink
230905
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Sep 5, 2023
1 parent e59bec0 commit a8ec13a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 45 deletions.
16 changes: 5 additions & 11 deletions src/Core/Application/Behaviors/ValidationBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
return await next();
}

var context = new ValidationContext<TRequest>(request);
var errorsDictionary = _validators
.Select(x => x.Validate(context))
.SelectMany(x => x.Errors)
.Where(x => x is not null)
.GroupBy(x => x.PropertyName, x => x.ErrorMessage, (propertyName, errorMessage) => new
{
Key = propertyName,
Values = errorMessage.Distinct().ToArray()
})
.ToDictionary(x => x.Key, x => x.Values);
var errorsDictionary = _validators.Select(x => x.Validate(new ValidationContext<TRequest>(request))).SelectMany(x => x.Errors).Where(x => x is not null).GroupBy(x => x.PropertyName, x => x.ErrorMessage, (propertyName, errorMessage) => new
{
Key = propertyName,
Values = errorMessage.Distinct().ToArray()
}).ToDictionary(x => x.Key, x => x.Values);

return errorsDictionary.Any() ? throw new Exceptions.ValidationException(errorsDictionary) : await next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public sealed class GetWebinarQueryHandler : IQueryHandler<GetWebinarByIdQuery,

public async Task<WebinarResponse> Handle(GetWebinarByIdQuery request, CancellationToken cancellationToken)
{
const string sql = @"SELECT * FROM ""Webinars"" WHERE ""Id"" = @WebinarId";
var webinar = await _dbConnection.QueryFirstOrDefaultAsync<WebinarResponse>(sql, new
var webinar = await _dbConnection.QueryFirstOrDefaultAsync<WebinarResponse>(@"SELECT * FROM ""Webinars"" WHERE ""Id"" = @WebinarId", new
{
request.WebinarId
});
Expand Down
11 changes: 2 additions & 9 deletions src/External/Presentation/Controllers/WebinarsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ public sealed class WebinarsController : ApiController
[HttpGet("{webinarId:guid}")]
[ProducesResponseType(typeof(WebinarResponse), Status200OK)]
[ProducesResponseType(Status404NotFound)]
public async ValueTask<IActionResult> GetWebinar(Guid webinarId, CancellationToken cancellationToken)
{
var query = new GetWebinarByIdQuery(webinarId);
var webinar = await Sender!.Send(query, cancellationToken);

return Ok(webinar);
}
public async ValueTask<IActionResult> GetWebinar(Guid webinarId, CancellationToken cancellationToken) => Ok(await Sender!.Send(new GetWebinarByIdQuery(webinarId), cancellationToken));

/// <summary>
/// Creates a new webinar based on the specified request.
Expand All @@ -39,8 +33,7 @@ public async ValueTask<IActionResult> GetWebinar(Guid webinarId, CancellationTok
[ProducesResponseType(Status404NotFound)]
public async Task<IActionResult> CreateWebinar([FromBody] CreateWebinarRequest request, CancellationToken cancellationToken)
{
var command = request.Adapt<CreateWebinarCommand>();
var webinarId = await Sender!.Send(command, cancellationToken);
var webinarId = await Sender!.Send(request.Adapt<CreateWebinarCommand>(), cancellationToken);

return CreatedAtAction(nameof(GetWebinar), new
{
Expand Down
5 changes: 2 additions & 3 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ public void ConfigureServices(IServiceCollection services)
_ = services.AddMediatR(applicationAssembly);
_ = services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
_ = services.AddValidatorsFromAssembly(applicationAssembly);

_ = services.AddSwaggerGen(c =>
{
var presentationDocumentationFile = $"{presentationAssembly.GetName().Name}.xml";
var presentationDocumentationFilePath = Combine(BaseDirectory, presentationDocumentationFile);
c.IncludeXmlComments(Combine(BaseDirectory, $"{presentationAssembly.GetName().Name}.xml"));

c.IncludeXmlComments(presentationDocumentationFilePath);
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Web",
Expand Down
32 changes: 12 additions & 20 deletions test/Architecture.Tests/ArchitectureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public void Domain_Should_Not_HaveDependencyOnOtherProjects()
// Arrange
var assembly = typeof(Domain.AssemblyReference).Assembly;

var otherProjects = new[]
// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(new[]
{
ApplicationNamespace,
InfrastructureNamespace,
PresentationNamespace,
WebNamespace
};

// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(otherProjects).GetResult();
}).GetResult();

// Assert
_ = testResult.IsSuccessful.Should().BeTrue();
Expand All @@ -38,16 +36,14 @@ public void Application_Should_Not_HaveDependencyOnOtherProjects()
// Arrange
var assembly = typeof(Application.AssemblyReference).Assembly;

var otherProjects = new[]
// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(new[]
{
DomainNamespace,
InfrastructureNamespace,
PresentationNamespace,
WebNamespace
};

// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(otherProjects).GetResult();
}).GetResult();

// Assert
_ = testResult.IsSuccessful.Should().BeTrue();
Expand All @@ -59,16 +55,14 @@ public void Infrastructure_Should_Not_HaveDependencyOnOtherProjects()
// Arrange
var assembly = typeof(Infrastructure.AssemblyReference).Assembly;

var otherProjects = new[]
// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(new[]
{
DomainNamespace,
ApplicationNamespace,
PresentationNamespace,
WebNamespace
};

// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(otherProjects).GetResult();
}).GetResult();

// Assert
_ = testResult.IsSuccessful.Should().BeTrue();
Expand All @@ -80,16 +74,14 @@ public void Presentation_Should_Not_HaveDependencyOnOtherProjects()
// Arrange
var assembly = typeof(Presentation.AssemblyReference).Assembly;

var otherProjects = new[]
// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(new[]
{
DomainNamespace,
ApplicationNamespace,
InfrastructureNamespace,
WebNamespace
};

// Act
var testResult = InAssembly(assembly).ShouldNot().HaveDependencyOnAll(otherProjects).GetResult();
}).GetResult();

// Assert
_ = testResult.IsSuccessful.Should().BeTrue();
Expand Down

0 comments on commit a8ec13a

Please sign in to comment.