1- using Microsoft . AspNetCore . Mvc ;
1+ using Catalog . API . IntegrationEvents ;
2+ using Microsoft . AspNetCore . Mvc ;
23using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . EntityFrameworkCore . Storage ;
35using Microsoft . eShopOnContainers . BuildingBlocks . EventBus . Abstractions ;
46using Microsoft . eShopOnContainers . BuildingBlocks . IntegrationEventLogEF ;
57using Microsoft . eShopOnContainers . Services . Catalog . API . Infrastructure ;
@@ -17,18 +19,18 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
1719 public class CatalogController : ControllerBase
1820 {
1921 private readonly CatalogContext _catalogContext ;
20- private readonly IntegrationEventLogContext _integrationEventLogContext ;
2122 private readonly IOptionsSnapshot < Settings > _settings ;
2223 private readonly IEventBus _eventBus ;
24+ private readonly IIntegrationEventLogService _integrationEventLogService ;
2325
24- public CatalogController ( CatalogContext catalogContext , IntegrationEventLogContext integrationEventLogContext , IOptionsSnapshot < Settings > settings , IEventBus eventBus )
26+ public CatalogController ( CatalogContext Context , IOptionsSnapshot < Settings > settings , IEventBus eventBus , IIntegrationEventLogService integrationEventLogService )
2527 {
26- _catalogContext = catalogContext ;
27- _integrationEventLogContext = integrationEventLogContext ;
28+ _catalogContext = Context ;
2829 _settings = settings ;
2930 _eventBus = eventBus ;
31+ _integrationEventLogService = integrationEventLogService ;
3032
31- ( ( DbContext ) catalogContext ) . ChangeTracker . QueryTrackingBehavior = QueryTrackingBehavior . NoTracking ;
33+ ( ( DbContext ) Context ) . ChangeTracker . QueryTrackingBehavior = QueryTrackingBehavior . NoTracking ;
3234 }
3335
3436 // GET api/v1/[controller]/items[?pageSize=3&pageIndex=10]
@@ -149,21 +151,21 @@ public async Task<IActionResult> EditProduct([FromBody]CatalogItem product)
149151 {
150152 var oldPrice = item . Price ;
151153 item . Price = product . Price ;
152- _catalogContext . CatalogItems . Update ( item ) ;
153-
154154 var @event = new ProductPriceChangedIntegrationEvent ( item . Id , item . Price , oldPrice ) ;
155- var eventLogEntry = new IntegrationEventLogEntry ( @event ) ;
156- _integrationEventLogContext . IntegrationEventLogs . Add ( eventLogEntry ) ;
157155
158- await _integrationEventLogContext . SaveChangesAsync ( ) ;
159- await _catalogContext . SaveChangesAsync ( ) ;
160-
156+ using ( var transaction = _catalogContext . Database . BeginTransaction ( ) )
157+ {
158+ _catalogContext . CatalogItems . Update ( item ) ;
159+ await _catalogContext . SaveChangesAsync ( ) ;
160+
161+ await _integrationEventLogService . SaveEventAsync ( @event ) ;
162+
163+ transaction . Commit ( ) ;
164+ }
165+
161166 _eventBus . Publish ( @event ) ;
162-
163- eventLogEntry . TimesSent ++ ;
164- eventLogEntry . State = EventStateEnum . Published ;
165- _integrationEventLogContext . IntegrationEventLogs . Update ( eventLogEntry ) ;
166- await _integrationEventLogContext . SaveChangesAsync ( ) ;
167+
168+ await _integrationEventLogService . MarkEventAsPublishedAsync ( @event ) ;
167169 }
168170
169171 return Ok ( ) ;
0 commit comments