public async Task DeleteAsync(int id) { try { var invoice = await _invoiceDataProvider.GetByOrderIdAsync(id); var message = $"Order {id} cannot be deleted because invoice {invoice.Id} is attached to it."; _logger.LogError(message); throw new InvalidOperationException(message); } catch (EntityNotFoundException) { var pageQuery = new QuerySet(); pageQuery.Page.Size = 1; var depositInvoices = await _depositInvoiceDataProvider.GetAllByOrderIdAsync(id, pageQuery); if (depositInvoices.Count > 0) { var message = $"Order {id} cannot be deleted because {depositInvoices.Count} deposit-invoices attached to it."; _logger.LogError(message); throw new InvalidOperationException(message); } var deposits = await _depositDataProvider.GetAllByOrderIdAsync(id, pageQuery); if (deposits.Count > 0) { var message = $"Order {id} cannot be deleted because {deposits.Count} deposits are attached to it."; _logger.LogError(message); throw new InvalidOperationException(message); } try { var order = await _orderDataProvider.GetByIdAsync(id); await _documentGenerationManager.DeleteOrderDocumentAsync(id); await _documentGenerationManager.DeleteDeliveryNoteAsync(id); await _documentGenerationManager.DeleteProductionTicketTemplateAsync(id); await _documentGenerationManager.DeleteProductionTicketAsync(id); await _orderDataProvider.DeleteByIdAsync(id); } catch (EntityNotFoundException) { // Order not found. Nothing should happen. } } }