@ -1,7 +1,11 @@
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore.Diagnostics ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events ;
using Newtonsoft.Json ;
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Data.Common ;
using System.Linq ;
using System.Threading.Tasks ;
@ -10,12 +14,15 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
{
public class IntegrationEventLogService : IIntegrationEventLogService
{
private readonly IEventBusSubscriptionsManager _subsManager ;
private readonly IntegrationEventLogContext _integrationEventLogContext ;
private readonly DbConnection _dbConnection ;
public IntegrationEventLogService ( DbConnection dbConnection )
public IntegrationEventLogService ( IEventBusSubscriptionsManager subsManager ,
DbConnection dbConnection )
{
_dbConnection = dbConnection ? ? throw new ArgumentNullException ( nameof ( dbConnection ) ) ;
_subsManager = subsManager ? ? throw new ArgumentNullException ( nameof ( subsManager ) ) ;
_integrationEventLogContext = new IntegrationEventLogContext (
new DbContextOptionsBuilder < IntegrationEventLogContext > ( )
. UseSqlServer ( _dbConnection )
@ -23,6 +30,19 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
. Options ) ;
}
public async Task < IEnumerable < IntegrationEventLogEntry > > RetrieveEventLogsPendingToPublishAsync ( )
{
var eventLogsPendingToPublish = await _integrationEventLogContext . IntegrationEventLogs
. Where ( e = > e . State = = EventStateEnum . NotPublished )
. OrderBy ( o = > o . CreationTime )
. ToListAsync ( ) ;
eventLogsPendingToPublish . ForEach ( evtLog = >
evtLog . DeserializeJsonContent ( _subsManager . GetEventTypeByName ( evtLog . EventTypeShortName ) ) ) ;
return eventLogsPendingToPublish ;
}
public Task SaveEventAsync ( IntegrationEvent @event , DbTransaction transaction )
{
if ( transaction = = null )
@ -38,11 +58,28 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
return _integrationEventLogContext . SaveChangesAsync ( ) ;
}
public Task MarkEventAsPublishedAsync ( IntegrationEvent @event )
public Task MarkEventAsPublishedAsync ( Guid eventId )
{
return UpdateEventStatus ( eventId , EventStateEnum . Published ) ;
}
public Task MarkEventAsInProgressAsync ( Guid eventId )
{
var eventLogEntry = _integrationEventLogContext . IntegrationEventLogs . Single ( ie = > ie . EventId = = @event . Id ) ;
eventLogEntry . TimesSent + + ;
eventLogEntry . State = EventStateEnum . Published ;
return UpdateEventStatus ( eventId , EventStateEnum . InProgress ) ;
}
public Task MarkEventAsFailedAsync ( Guid eventId )
{
return UpdateEventStatus ( eventId , EventStateEnum . PublishedFailed ) ;
}
private Task UpdateEventStatus ( Guid eventId , EventStateEnum status )
{
var eventLogEntry = _integrationEventLogContext . IntegrationEventLogs . Single ( ie = > ie . EventId = = eventId ) ;
eventLogEntry . State = status ;
if ( status = = EventStateEnum . InProgress )
eventLogEntry . TimesSent + + ;
_integrationEventLogContext . IntegrationEventLogs . Update ( eventLogEntry ) ;