private async Task SetorIncluir(Guid EmpresaId, EmpresaInclusaoIntegrationEvent @event) { using (_setorContext) { List<SetorItem> list = new List<SetorItem>(); var lstSetorDefaultNames = _setorContext.SetorDefaultItems.FromSql("SELECT * FROM DefaultSetor").ToList(); foreach (SetorDefaultItem it in lstSetorDefaultNames) { Guid gKey; gKey = Guid.NewGuid(); SetorItem setorToSave = new SetorItem(); setorToSave.id_Setor = gKey; setorToSave.id_Empresa = EmpresaId; setorToSave.nome_Setor = it.Nome_Setor; // Update current setor _setorContext.SetorItems.Add(setorToSave); list.Add(setorToSave); } //var setorInclusaoEvent = new SetorInclusaoGrupoIntegrationEvent(list); //await _setorIntegrationEventService.SaveEventAndSetorContextChangesAsync(setorInclusaoEvent); //await _setorIntegrationEventService.PublishThroughEventBusAsync(setorInclusaoEvent); //Processado evento EMPRESA await _setorIntegrationEventService.ProcessedThroughEventBusAsync(@event); } }
public async Task <IActionResult> IncluirSetor([FromBody] SetorItem setorToSave) { Guid gKey; gKey = Guid.NewGuid(); setorToSave.id_Setor = gKey; // Update current setor _setorContext.SetorItems.Add(setorToSave); //if (_settings.UseCache) //{ // Cache<SetorItem> mycache = new Cache<SetorItem>(); // await mycache.SetAsync(cachePrefix + setorToSave.id_Empresa+ setorToSave.id_Setor, setorToSave); //} //Create Integration Event to be published through the Event Bus var setorInclusaoEvent = new SetorInclusaoIntegrationEvent(setorToSave.id_Empresa, setorToSave.id_Setor, setorToSave.nome_Setor); // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction await _setorIntegrationEventService.SaveEventAndSetorContextChangesAsync(setorInclusaoEvent); // Publish through the Event Bus and mark the saved event as published await _setorIntegrationEventService.PublishThroughEventBusAsync(setorInclusaoEvent); return(CreatedAtAction(nameof(IncluirSetor), null)); }
public async Task <IActionResult> SalvarSetor([FromBody] SetorItem setorToSave) { //AREA DE VALIDACAO string msgRule = ""; if (!ruleValidaNomeSetor(setorToSave.nome_Setor, ref msgRule)) { return(BadRequest(msgRule)); } if (!ruleValidaCodExternoSetor(setorToSave.CodExterno_Setor, ref msgRule)) { return(BadRequest(msgRule)); } //FIM AREA DE VALIDACAO if (_administrativoContext.Set <SetorItem>().Any(e => e.id_Empresa == setorToSave.id_Empresa && e.id_Setor == setorToSave.id_Setor)) { _administrativoContext.SetorItems.Update(setorToSave); } else { _administrativoContext.SetorItems.Add(setorToSave); } //Create Integration Event to be published through the Event Bus var setorSaveEvent = new SetorSaveIE(setorToSave.id_Empresa, setorToSave.nome_Setor, setorToSave.CodExterno_Setor); try { // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction await _administrativoIntegrationEventService.SaveEventAndSetorContextChangesAsync(setorSaveEvent, setorToSave); } catch (Exception e) { //Validações das CONSTRAINTS do BANCO if (ruleValidaNomeSetorUnique(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 _administrativoIntegrationEventService.PublishThroughEventBusAsync(setorSaveEvent); return(CreatedAtAction(nameof(SalvarSetor), setorToSave.id_Setor)); }
private SetorItem CreateSetorItem(string[] column, string[] headers) { if (column.Count() != headers.Count()) { throw new Exception($"column count '{column.Count()}' not the same as headers count'{headers.Count()}'"); } var setorItem = new SetorItem() { id_Empresa = Guid.Parse(column[Array.IndexOf(headers, "id_empresa")].Trim('"').Trim()), id_Setor = Guid.Parse(column[Array.IndexOf(headers, "id_setor")].Trim('"').Trim()), nome_Setor = column[Array.IndexOf(headers, "nome_setor")].Trim('"').Trim() }; return(setorItem); }
public async Task Del_ExcluirSetor() { using (var server = CreateServer()) { //var response = await server.CreateClient() // .DeleteAsync(Del.ExcluirSetor(Guid.Parse("4C8D935A-E673-47D3-A8F5-9A3934DF72E7"))); for (int i = 0; i < 1000; i++) { SetorItem setorItem = new SetorItem(); setorItem.id_Empresa = Guid.Parse("B8AC98BA-50B3-4947-B042-09DD09A76C21"); setorItem.id_Setor = Guid.NewGuid(); setorItem.nome_Setor = "AAA" + i.ToString(); var content = new StringContent(JsonConvert.SerializeObject(setorItem), System.Text.Encoding.UTF8, "application/json"); var responsex = await server.CreateClient() .PostAsync(Post.IncluirSetor(), content); } } }
public async Task SaveEventAndSetorContextChangesAsync(IntegrationEvent evt, SetorItem setorToSave) { var strategy = _administrativoContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _administrativoContext.Database.BeginTransaction()) { try { await _administrativoContext.SaveChangesAsync(); //Tratamento de Identity ((Events.SetorSaveIE)evt).SetorId = setorToSave.id_Setor; await _eventLogService.SaveEventAsync(evt, _administrativoContext.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); } } }); }