File tree Expand file tree Collapse file tree 12 files changed +119
-18
lines changed
building blocks/JSE.Core/Messages/Integration
JSE.Pedido.Domain/Vouchers Expand file tree Collapse file tree 12 files changed +119
-18
lines changed Original file line number Diff line number Diff line change 1+ namespace JSE . Core . Messages . Integration
2+ {
3+ public class PedidoRealizadoIntegrationEvent : IntegrationEvent
4+ {
5+ public Guid ClienteId { get ; private set ; }
6+
7+ public PedidoRealizadoIntegrationEvent ( Guid clienteId )
8+ {
9+ ClienteId = clienteId ;
10+ }
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ using JSE . Carrinho . API . Services ;
2+ using JSE . Core . Utils ;
3+ using JSE . MessageBus ;
4+
5+ namespace JSE . Carrinho . API . Configuration
6+ {
7+ public static class MessageBusConfig
8+ {
9+ public static void AddMessageBusConfiguration ( this IServiceCollection services ,
10+ IConfiguration configuration )
11+ {
12+ services . AddMessageBus ( configuration . GetMessageQueueConnection ( "MessageBus" ) )
13+ . AddHostedService < CarrinhoIntegrationHandler > ( ) ;
14+ }
15+ }
16+ }
Original file line number Diff line number Diff line change @@ -51,7 +51,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5151 . WithOne ( i => i . CarrinhoCliente )
5252 . HasForeignKey ( c => c . CarrinhoId ) ;
5353
54- foreach ( var relationship in modelBuilder . Model . GetEntityTypes ( ) . SelectMany ( e => e . GetForeignKeys ( ) ) ) relationship . DeleteBehavior = DeleteBehavior . ClientSetNull ;
54+ foreach ( var relationship in modelBuilder . Model . GetEntityTypes ( )
55+ . SelectMany ( e => e . GetForeignKeys ( ) ) ) relationship . DeleteBehavior = DeleteBehavior . Cascade ;
5556 }
5657 }
5758}
Original file line number Diff line number Diff line change 1818 </PackageReference >
1919 <PackageReference Include =" Microsoft.EntityFrameworkCore.Relational" Version =" 8.0.10" />
2020 <PackageReference Include =" Microsoft.EntityFrameworkCore.SqlServer" Version =" 8.0.10" />
21+ <PackageReference Include =" Microsoft.EntityFrameworkCore.Tools" Version =" 8.0.10" >
22+ <PrivateAssets >all</PrivateAssets >
23+ <IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
24+ </PackageReference >
2125 <PackageReference Include =" Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version =" 1.21.0" />
2226 <PackageReference Include =" Swashbuckle.AspNetCore" Version =" 6.4.0" />
2327 </ItemGroup >
2428
2529 <ItemGroup >
30+ <ProjectReference Include =" ..\..\building blocks\JSE.MessageBus\JSE.MessageBus.csproj" />
2631 <ProjectReference Include =" ..\..\building blocks\JSE.WebAPI.Core\JSE.WebAPI.Core.csproj" />
2732 </ItemGroup >
2833
Original file line number Diff line number Diff line change 1616builder . Services . AddJwtConfiguration ( configuration ) ;
1717builder . Services . AddSwaggerConfiguration ( ) ;
1818builder . Services . RegisterServices ( ) ;
19+ builder . Services . AddMessageBusConfiguration ( configuration ) ;
20+
1921
2022var app = builder . Build ( ) ;
2123var environment = app . Environment ;
Original file line number Diff line number Diff line change 1+ using JSE . Carrinho . API . Data ;
2+ using JSE . Core . Messages . Integration ;
3+ using JSE . MessageBus ;
4+ using Microsoft . EntityFrameworkCore ;
5+
6+ namespace JSE . Carrinho . API . Services
7+ {
8+ public class CarrinhoIntegrationHandler : BackgroundService
9+ {
10+ private readonly IMessageBus _bus ;
11+ private readonly IServiceProvider _serviceProvider ;
12+
13+ public CarrinhoIntegrationHandler ( IMessageBus bus , IServiceProvider serviceProvider )
14+ {
15+ _serviceProvider = serviceProvider ;
16+ _bus = bus ;
17+ }
18+
19+ protected override Task ExecuteAsync ( CancellationToken stoppingToken )
20+ {
21+ SetSubscribers ( ) ;
22+ return Task . CompletedTask ;
23+ }
24+
25+ private void SetSubscribers ( )
26+ {
27+ _bus . SubscribeAsync < PedidoRealizadoIntegrationEvent > ( "PedidoRealizado" , async request =>
28+ await ApagarCarrinho ( request ) ) ;
29+ }
30+
31+ private async Task ApagarCarrinho ( PedidoRealizadoIntegrationEvent message )
32+ {
33+ using var scope = _serviceProvider . CreateScope ( ) ;
34+ var context = scope . ServiceProvider . GetRequiredService < CarrinhoContext > ( ) ;
35+
36+ var carrinho = await context . CarrinhoCliente . FirstOrDefaultAsync ( c => c . ClienteId == message . ClienteId ) ;
37+
38+ if ( carrinho != null )
39+ {
40+ context . CarrinhoCliente . Remove ( carrinho ) ;
41+ await context . SaveChangesAsync ( ) ;
42+ }
43+ }
44+
45+ }
46+ }
Original file line number Diff line number Diff line change 22 "ConnectionStrings" : {
33 "DefaultConnection" : " Server=.\\ SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;"
44 },
5+
6+ "MessageQueueConnection" : {
7+ "MessageBus" : " host=localhost:5672;publisherConfirms=true;timeout=10"
8+ },
9+
510 "Logging" : {
611 "LogLevel" : {
712 "Default" : " Information" ,
813 "Microsoft.AspNetCore" : " Warning"
914 }
1015 },
16+
1117 "AppSettings" : {
1218 "Secret" : " F9F52344-59C3-4EAC-90E6-CB47935038BE" ,
1319 "ExpirationHours" : 2 ,
Original file line number Diff line number Diff line change 22 "ConnectionStrings" : {
33 "DefaultConnection" : " Server=.\\ SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;"
44 },
5-
65 "MessageQueueConnection" : {
76 "MessageBus" : " host=localhost:5672;publisherConfirms=true;timeout=10"
87 },
9-
108 "Logging" : {
119 "LogLevel" : {
1210 "Default" : " Information" ,
1311 "Microsoft.AspNetCore" : " Warning"
14- },
15-
16- "AppSettings" : {
17- "Secret" : " F9F52344-59C3-4EAC-90E6-CB47935038BE" ,
18- "ExpirationHours" : 2 ,
19- "Issuer" : " MeuSistema" ,
20- "ValidOn" : " https://localhost"
2112 }
22-
13+ },
14+ "AppSettings" : {
15+ "Secret" : " F9F52344-59C3-4EAC-90E6-CB47935038BE" ,
16+ "ExpirationHours" : 2 ,
17+ "Issuer" : " MeuSistema" ,
18+ "ValidOn" : " https://localhost"
2319 }
24- }
20+ }
Original file line number Diff line number Diff line change 1+ using JSE . Core . Messages . Integration ;
2+ using JSE . MessageBus ;
3+ using MediatR ;
4+
5+ namespace JSE . Pedido_API . Application . Events
6+ {
7+ public class PedidoEventHandler : INotificationHandler < PedidoRealizadoEvent >
8+ {
9+ private readonly IMessageBus _bus ;
10+
11+ public PedidoEventHandler ( IMessageBus bus )
12+ {
13+ _bus = bus ;
14+ }
15+
16+ public async Task Handle ( PedidoRealizadoEvent message , CancellationToken cancellationToken )
17+ {
18+ await _bus . PublishAsync ( new PedidoRealizadoIntegrationEvent ( message . ClienteId ) ) ;
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change 11using JSE . Core . Utils ;
22using JSE . MessageBus ;
33
4- namespace NSE . Pedidos . API . Configuration
4+ namespace JSE . Pedidos . API . Configuration
55{
66 public static class MessageBusConfig
77 {
You can’t perform that action at this time.
0 commit comments