Skip to content

Commit 0f07a81

Browse files
committed
rabbit listener error
1 parent 02df9d1 commit 0f07a81

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

src/Ordering/Ordering.API/Controllers/OrderController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MediatR;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Extensions.Logging;
4+
using Ordering.Application.Commands;
45
using Ordering.Application.Queries;
56
using Ordering.Application.Responses;
67
using System;
@@ -31,5 +32,14 @@ public async Task<ActionResult<IEnumerable<OrderResponse>>> GetOrdersByUserName(
3132
var orders = await _mediator.Send(query);
3233
return Ok(orders);
3334
}
35+
36+
//Added for testing purpose
37+
[HttpPost]
38+
[ProducesResponseType(typeof(OrderResponse), (int)HttpStatusCode.OK)]
39+
public async Task<IActionResult> CheckoutOrder([FromBody] CheckoutOrderCommand command)
40+
{
41+
var result = await _mediator.Send(command);
42+
return Ok(result);
43+
}
3444
}
3545
}

src/Ordering/Ordering.API/RabbitMQ/EventBusRabbitMQConsumer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ public void Consume()
3838
channel.BasicConsume(queue: EventBusConstants.BasketCheckoutQueue, autoAck: true, consumer: consumer);
3939
}
4040

41-
private void ReceivedEvent(object sender, BasicDeliverEventArgs e)
41+
private async void ReceivedEvent(object sender, BasicDeliverEventArgs e)
4242
{
4343
if (e.RoutingKey == EventBusConstants.BasketCheckoutQueue)
4444
{
4545
var message = Encoding.UTF8.GetString(e.Body);
4646
var basketCheckoutEvent = JsonConvert.DeserializeObject<BasketCheckoutEvent>(message);
4747

48+
4849
// Internal Checkout Operation Call
4950
var command = _mapper.Map<CheckoutOrderCommand>(basketCheckoutEvent);
50-
var result = _mediator.Send(command);
51+
var result = await _mediator.Send(command); // ERROR : CANT RESOLVE SUB OBJECTS FROM THIS CALL
52+
5153
}
5254
}
5355

src/Ordering/Ordering.API/Startup.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
1010
using Microsoft.OpenApi.Models;
11+
using Ordering.API.Extentions;
1112
using Ordering.API.RabbitMQ;
1213
using Ordering.Application.Handlers;
14+
using Ordering.Application.PipelineBehaviours;
1315
using Ordering.Core.Repositories;
1416
using Ordering.Core.Repositories.Base;
1517
using Ordering.Infrastructure.Data;
@@ -50,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
5052

5153
// Add Infrastructure Layer
5254
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
53-
services.AddScoped(typeof(IOrderRepository), typeof(OrderRepository));
55+
services.AddScoped(typeof(IOrderRepository), typeof(OrderRepository));
5456
services.AddScoped<IOrderRepository, OrderRepository>();
5557

5658
// Add AutoMapper
@@ -59,6 +61,9 @@ public void ConfigureServices(IServiceCollection services)
5961
// Add MediatR
6062
services.AddMediatR(typeof(CheckoutOrderHandler).GetTypeInfo().Assembly);
6163

64+
//Domain Level Validation
65+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
66+
6267
#endregion
6368

6469
#region RabbitMQ Dependencies
@@ -114,8 +119,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
114119
endpoints.MapControllers();
115120
});
116121

117-
////Initilize Rabbit Listener in ApplicationBuilderExtentions
118-
//app.UseRabbitListener();
122+
//Initilize Rabbit Listener in ApplicationBuilderExtentions
123+
app.UseRabbitListener();
119124

120125
app.UseSwagger();
121126
app.UseSwaggerUI(c =>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using FluentValidation;
2+
using MediatR;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Ordering.Application.PipelineBehaviours
9+
{
10+
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
11+
where TRequest : IRequest<TResponse>
12+
{
13+
private readonly IEnumerable<IValidator<TRequest>> _validators;
14+
15+
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators)
16+
{
17+
_validators = validators;
18+
}
19+
20+
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
21+
{
22+
var context = new ValidationContext(request);
23+
var failures = _validators
24+
.Select(x => x.Validate(context))
25+
.SelectMany(x => x.Errors)
26+
.Where(x => x != null)
27+
.ToList();
28+
29+
// TODO: add context to which command/query is throwing the exception and its origination
30+
31+
if (failures.Any())
32+
{
33+
throw new ValidationException(failures);
34+
}
35+
36+
return next();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)