public async Task <IActionResult> SalvarSLA([FromBody] SLAItem slaToSave) { string msgRule = ""; if (_configuracaoContext.Set <SLAItem>().Any(e => e.Id_SLA == slaToSave.Id_SLA)) { _configuracaoContext.SLAItems.Update(slaToSave); } else { _configuracaoContext.SLAItems.Add(slaToSave); } //Create Integration Event to be published through the Event Bus var slaSaveEvent = new SLASaveIE(slaToSave.Id_SLA, slaToSave.Id_TipoSituacaoAcomodacao, slaToSave.Id_TipoAtividadeAcomodacao, slaToSave.Id_TipoAcaoAcomodacao, slaToSave.Id_Empresa, slaToSave.Tempo_Minutos, slaToSave.Versao_SLA); try { // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction await _configuracaoIntegrationEventService.SaveEventAndSLAContextChangesAsync(slaSaveEvent, slaToSave); } catch (Exception e) { //Validações das CONSTRAINTS do BANCO if (ruleValidaVersaoUnique(e.Message, ref msgRule)) { return(BadRequest(msgRule)); } else { return(BadRequest(e.Message)); } } // Publish through the Event Bus and mark the saved event as published await _configuracaoIntegrationEventService.PublishThroughEventBusAsync(slaSaveEvent); return(CreatedAtAction(nameof(SalvarSLA), slaToSave.Id_SLA)); }
public async Task SaveEventAndSLAContextChangesAsync(IntegrationEvent evt, SLAItem sLAToSave) { var strategy = _configuracaoContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _configuracaoContext.Database.BeginTransaction()) { try { await _configuracaoContext.SaveChangesAsync(); //Tratamento de Identity ((Events.SLASaveIE)evt).SLAId = sLAToSave.Id_SLA; await _eventLogService.SaveEventAsync(evt, _configuracaoContext.Database.CurrentTransaction.GetDbTransaction()); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); var sqlException = ex.InnerException as System.Data.SqlClient.SqlException; throw new Exception(sqlException.Number + "::" + sqlException.Message); } } }); }