public async Task ReceiveSeriesCreated(SeriesCreatedEvent seriesEvent) { if (!await _messageTracker.HasProcessed(seriesEvent.EventId)) { var series = _mapper.Map <Series>(seriesEvent); using (var transaction = await _context.Database.BeginTransactionAsync()) { try { _context.Series.Add(series); await _context.SaveChangesAsync(); await _messageTracker.MarkAsProcessed(seriesEvent.EventId); await transaction.CommitAsync(); } catch (Exception e) { await transaction.RollbackAsync(); _logger.LogError(e, $"Unexpected error while processing event of type {nameof(SeriesCreatedEvent)}" + $" EventId: {seriesEvent.EventId} SeriesId:{seriesEvent.SeriesId}"); } } } }
public async Task ReceiveUserCreated(UserCreatedEvent userEvent) { if (!await _messageTracker.HasProcessed(userEvent.EventId)) { using (var transaction = await _context.Database.BeginTransactionAsync()) { try { var viewerCreated = _mapper.Map <Viewer>(userEvent); _context.Viewer.Add(viewerCreated); await _context.SaveChangesAsync(); await _messageTracker.MarkAsProcessed(userEvent.EventId); await transaction.CommitAsync(); } catch (Exception e) { await transaction.RollbackAsync(); _logger.LogError(e, $"Unexpected error while processing event of type {nameof(UserCreatedEvent)}" + $" EventId: {userEvent.EventId} UserId:{userEvent.UserId}"); } } } }
public async Task ReceiveSeriesReviewCreated(SeriesReviewCreatedEvent reviewEvent) { _logger.LogInformation($"Received event of type {nameof(SeriesReviewCreatedEvent)}" + $" with Id: {reviewEvent.EventId}"); if (!await _messageTracker.HasProcessed(reviewEvent.EventId)) { _logger.LogInformation($"Processing event of type {nameof(SeriesReviewCreatedEvent)}" + $" with Id: {reviewEvent.EventId}"); using (var transaction = await _context.Database.BeginTransactionAsync()) { try { var review = _mapper.Map <SeriesReview>(reviewEvent); _context.SeriesReview.Add(review); await _context.SaveChangesAsync(); await _messageTracker.MarkAsProcessed(reviewEvent.EventId); await transaction.CommitAsync(); _logger.LogInformation($"Successfully processed event of type {nameof(SeriesReviewCreatedEvent)}" + $" with Id: {reviewEvent.EventId}"); } catch (Exception e) { await transaction.RollbackAsync(); _logger.LogError(e, $"Unexpected error while processing event of type {nameof(SeriesReviewCreatedEvent)}" + $" EventId: {reviewEvent.EventId} SeriesId:{reviewEvent.SeriesId} Reviewerd:{reviewEvent.ReviewerId}"); } } } else { _logger.LogWarning($"We have already processed event with Id: {reviewEvent.EventId}" + $", so we dont do anything"); } }