public async Task <ContractDetailDto> Handle(Command message) { var contractId = message.ContractDetailDto.Id = message.ContractId; var contractDto = message.ContractDetailDto; var contract = await _context.Contracts .SingleOrDefaultAsync(r => r.Id == contractId); if (contract == null) { throw new DataNotFoundException(nameof(Domain.Entities.Contract.Contract), DataNotFoundException.OperationType.Update, contractId); } _mapper.Map(contractDto, contract); using (var transaction = await _context.Database.BeginTransactionAsync()) { try { await _context.SaveChangesAsync(); await _requestRelationUpdater.UpdateAsync(contract.Id, contractDto.RequestRelations); transaction.Commit(); var contractWithIncludes = await _context.Contracts .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.CurrentStage) .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.FromStage) .Include(r => r.ProtectionDocType) .Include(r => r.ContractType) .Include(r => r.ContractCustomers).ThenInclude(c => c.CustomerRole) .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Type) .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Country) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Tariff) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.PaymentUses) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Status) .Include(r => r.Workflows).ThenInclude(r => r.FromStage) .Include(r => r.Workflows).ThenInclude(r => r.CurrentStage) .Include(r => r.Workflows).ThenInclude(r => r.FromUser) .Include(r => r.Workflows).ThenInclude(r => r.CurrentUser) .Include(r => r.Workflows).ThenInclude(r => r.Route) .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.Request).ThenInclude(r => r.ProtectionDocType) .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.ContractRequestICGSRequests).ThenInclude(r => r.ICGSRequest).ThenInclude(r => r.Icgs) .Include(r => r.ProtectionDocs) .SingleAsync(r => r.Id == contractId); return(_mapper.Map <Domain.Entities.Contract.Contract, ContractDetailDto>(contractWithIncludes)); } catch (Exception e) { transaction.Rollback(); throw new DatabaseException(e.InnerException?.Message ?? e.Message); } } }
public async Task <ContractDetailDto> Handle(Command message) { var contractDto = message.ContractDetailDto; var contract = _mapper.Map <ContractDetailDto, Domain.Entities.Contract.Contract>(contractDto); contract.RegDate = DateTimeOffset.Now; contract.ContractNum = _numberGenerator.Generate("ContractNum").ToString(); _numberGenerator.GenerateBarcode(contract); _context.Contracts.Add(contract); try { await _context.SaveChangesAsync(); await _workflowApplier.ApplyInitialAsync(contract, message.UserId); await _context.SaveChangesAsync(); await _requestRelationUpdater.UpdateAsync(contract.Id, contractDto.RequestRelations); var contractWithIncludes = await _context.Contracts .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.CurrentStage) .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.FromStage) .Include(r => r.ProtectionDocType) .Include(r => r.ContractType) .Include(r => r.ContractCustomers).ThenInclude(c => c.CustomerRole) .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Type) .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Country) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Tariff) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.PaymentUses) .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Status) .Include(r => r.Workflows).ThenInclude(r => r.FromStage) .Include(r => r.Workflows).ThenInclude(r => r.CurrentStage) .Include(r => r.Workflows).ThenInclude(r => r.FromUser) .Include(r => r.Workflows).ThenInclude(r => r.CurrentUser) .Include(r => r.Workflows).ThenInclude(r => r.Route) .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.Request).ThenInclude(r => r.ProtectionDocType) .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.ContractRequestICGSRequests).ThenInclude(r => r.ICGSRequest).ThenInclude(r => r.Icgs) .Include(r => r.ProtectionDocs) .SingleAsync(c => c.Id == contract.Id); return(_mapper.Map <Domain.Entities.Contract.Contract, ContractDetailDto>(contractWithIncludes)); } catch (Exception e) { throw new DatabaseException(e.InnerException?.Message ?? e.Message); } }