/// <summary> /// Upload Products /// </summary> /// <param name="products"></param> /// <returns></returns> public async Task <ResponseModel <List <ProductDataTransformation> > > UploadProducts(int accountId, List <ProductDataTransformation> products) { ResponseModel <List <ProductDataTransformation> > returnResponse = new ResponseModel <List <ProductDataTransformation> >(); try { _inventoryManagementDataService.OpenConnection(_connectionStrings.PrimaryDatabaseConnectionString); List <Product> productsAdded = new List <Product>(); foreach (ProductDataTransformation productItem in products) { _inventoryManagementDataService.BeginTransaction((int)IsolationLevel.Serializable); ProductBusinessRules <ProductDataTransformation> productBusinessRules = new ProductBusinessRules <ProductDataTransformation>(productItem, _inventoryManagementDataService); ValidationResult validationResult = await productBusinessRules.Validate(); if (validationResult.ValidationStatus == false) { _inventoryManagementDataService.RollbackTransaction(); continue; } Product product = new Product(); product.AccountId = accountId; product.ProductNumber = productItem.ProductNumber; product.Description = productItem.Description; product.UnitPrice = productItem.UnitPrice; product.BinLocation = productItem.BinLocation; await _inventoryManagementDataService.CreateProduct(product); //await _inventoryManagementDataService.UpdateDatabase(); TransactionQueueOutbound transactionQueue = new TransactionQueueOutbound(); transactionQueue.Payload = GenerateProductUpdatePayload(product); transactionQueue.TransactionCode = TransactionQueueTypes.ProductUpdated; transactionQueue.ExchangeName = MessageQueueExchanges.InventoryManagement; await _inventoryManagementDataService.CreateOutboundTransactionQueue(transactionQueue); //await _inventoryManagementDataService.UpdateDatabase(); _inventoryManagementDataService.CommitTransaction(); productsAdded.Add(product); } foreach (ProductDataTransformation productItem in products) { Product product = productsAdded.Where(x => x.ProductNumber == productItem.ProductNumber).FirstOrDefault(); if (product != null) { continue; } product = await _inventoryManagementDataService.GetProductInformationByProductNumber(productItem.ProductNumber, accountId); if (product == null) { continue; } productItem.ProductId = product.ProductId; ProductBusinessRules <ProductDataTransformation> productBusinessRules = new ProductBusinessRules <ProductDataTransformation>(productItem, _inventoryManagementDataService); ValidationResult validationResult = await productBusinessRules.Validate(); if (validationResult.ValidationStatus == false) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage = validationResult.ValidationMessages; returnResponse.ReturnStatus = validationResult.ValidationStatus; return(returnResponse); } _inventoryManagementDataService.BeginTransaction((int)IsolationLevel.Serializable); product = await _inventoryManagementDataService.GetProductInformationForUpdate(productItem.ProductId); product.ProductNumber = productItem.ProductNumber; product.Description = productItem.Description; product.UnitPrice = productItem.UnitPrice; product.BinLocation = productItem.BinLocation; await _inventoryManagementDataService.UpdateProduct(product); TransactionQueueOutbound transactionQueue = new TransactionQueueOutbound(); transactionQueue.Payload = GenerateProductUpdatePayload(product); transactionQueue.TransactionCode = TransactionQueueTypes.ProductUpdated; transactionQueue.ExchangeName = MessageQueueExchanges.InventoryManagement; await _inventoryManagementDataService.CreateOutboundTransactionQueue(transactionQueue); //await _inventoryManagementDataService.UpdateDatabase(); _inventoryManagementDataService.CommitTransaction(); } returnResponse.ReturnStatus = true; } catch (Exception ex) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); } finally { _inventoryManagementDataService.CloseConnection(); } return(returnResponse); }