public async Task <IActionResult> AddEmployeeAsync([FromBody] Infrastructure.Entities.Employee employee) { var userIdentity = Request.Headers["claims_userid"]; if (userIdentity.Count == 0) { return(NotFound(new { Message = $"You are not authorized to add employee." })); } employee.CreatedBy = employee.ModifiedBy = userIdentity; try { // Commit Add Employee var addedEmployee = await _employeeService.AddEmployee(employee); var eployeeChangedEvent = new EmployeeAddIntegrationEvent(employee.Id, employee.FirstName, employee.LastName, employee.ModifiedBy); await _employeeIntegrationEventService.AddAndSaveEventAsync(eployeeChangedEvent); await _employeeIntegrationEventService.PublishEventsThroughEventBusAsync(eployeeChangedEvent); return(Ok(employee.Id)); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { var response = default(TResponse); var typeName = request.GetGenericTypeName(); try { if (_dbContext.HasActiveTransaction) { return(await next()); } var strategy = _dbContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { Guid transactionId; using (var transaction = await _dbContext.BeginTransactionAsync()) using (LogContext.PushProperty("TransactionContext", transaction.TransactionId)) { _logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transaction.TransactionId, typeName, request); response = await next(); _logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName); await _dbContext.CommitTransactionAsync(transaction); transactionId = transaction.TransactionId; } await _employeeIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId); }); return(response); } catch (Exception ex) { _logger.LogError(ex, "ERROR Handling transaction for {CommandName} ({@Command})", typeName, request); throw; } }