public async Task <IActionResult> SalvarSLASituacao([FromBody] SLASituacaoItem slaSituacaoToSave) { string msgRule = ""; if (_configuracaoContext.Set <SLASituacaoItem>().Any(e => e.Id_SLA == slaSituacaoToSave.Id_SLA)) { _configuracaoContext.SLASituacaoItems.Update(slaSituacaoToSave); } else { _configuracaoContext.SLASituacaoItems.Add(slaSituacaoToSave); } //Create Integration Event to be published through the Event Bus var slaSituacaoSaveEvent = new SLASituacaoSaveIE(slaSituacaoToSave.Id_SLA, slaSituacaoToSave.Id_TipoSituacaoAcomodacao, slaSituacaoToSave.Id_Empresa, slaSituacaoToSave.Tempo_Minutos, slaSituacaoToSave.Versao_SLA); try { // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction await _configuracaoIntegrationEventService.SaveEventAndSLASituacaoContextChangesAsync(slaSituacaoSaveEvent, slaSituacaoToSave); } 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(slaSituacaoSaveEvent); return(CreatedAtAction(nameof(SalvarSLASituacao), slaSituacaoToSave.Id_SLA)); }
public async Task SaveEventAndSLASituacaoContextChangesAsync(IntegrationEvent evt, SLASituacaoItem sLASituacaoToSave) { var strategy = _configuracaoContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _configuracaoContext.Database.BeginTransaction()) { try { await _configuracaoContext.SaveChangesAsync(); //Tratamento de Identity ((Events.SLASituacaoSaveIE)evt).SLASituacaoId = sLASituacaoToSave.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); } } }); }