Exemplo n.º 1
0
        /// <summary>
        /// Get Messages To Send
        /// </summary>
        /// <param name="messageQueueConfigurations"></param>
        /// <param name="outboundSemaphoreKey"></param>
        /// <param name="connectionStrings"></param>
        /// <returns></returns>
        private async Task <ResponseModel <List <MessageQueue> > > GetMessagesToSend(List <IMessageQueueConfiguration> messageQueueConfigurations, string outboundSemaphoreKey, ConnectionStrings connectionStrings)
        {
            TransactionQueueSemaphore transactionQueueSemaphore = null;

            ResponseModel <List <MessageQueue> > returnResponse = new ResponseModel <List <MessageQueue> >();

            returnResponse.Entity = new List <MessageQueue>();

            try
            {
                _inventoryManagementDataService.OpenConnection(connectionStrings.PrimaryDatabaseConnectionString);
                _inventoryManagementDataService.BeginTransaction((int)IsolationLevel.Serializable);

                transactionQueueSemaphore = await _inventoryManagementDataService.GetTransactionQueueSemaphore(outboundSemaphoreKey);

                if (transactionQueueSemaphore == null)
                {
                    transactionQueueSemaphore = new TransactionQueueSemaphore();
                    transactionQueueSemaphore.SemaphoreKey = outboundSemaphoreKey;
                    await _inventoryManagementDataService.CreateTransactionQueueSemaphore(transactionQueueSemaphore);
                }
                else
                {
                    await _inventoryManagementDataService.UpdateTransactionQueueSemaphore(transactionQueueSemaphore);
                }

                List <TransactionQueueOutbound> transactionQueue = await _inventoryManagementDataService.GetOutboundTransactionQueue();

                foreach (TransactionQueueOutbound transactionQueueItem in transactionQueue)
                {
                    MessageQueue message = new MessageQueue();
                    message.ExchangeName       = transactionQueueItem.ExchangeName;
                    message.TransactionQueueId = transactionQueueItem.TransactionQueueOutboundId;
                    message.TransactionCode    = transactionQueueItem.TransactionCode;
                    message.Payload            = transactionQueueItem.Payload;

                    IMessageQueueConfiguration messageQueueConfiguration = messageQueueConfigurations.Where(x => x.TransactionCode == message.TransactionCode).FirstOrDefault();
                    if (messageQueueConfiguration == null)
                    {
                        break;
                    }

                    ResponseModel <MessageQueue> messageQueueResponse = messageQueueConfiguration.SendMessage(message);
                    if (messageQueueResponse.ReturnStatus == true)
                    {
                        transactionQueueItem.SentToExchange     = true;
                        transactionQueueItem.DateSentToExchange = DateTime.UtcNow;
                        await _inventoryManagementDataService.UpdateOutboundTransactionQueue(transactionQueueItem);

                        returnResponse.Entity.Add(message);
                    }
                    else
                    {
                        break;
                    }
                }

                await _inventoryManagementDataService.UpdateDatabase();

                _inventoryManagementDataService.CommitTransaction();
                _inventoryManagementDataService.CloseConnection();
            }
            catch (Exception ex)
            {
                _inventoryManagementDataService.RollbackTransaction();
                returnResponse.ReturnStatus = false;
                returnResponse.ReturnMessage.Add(ex.Message);
            }
            finally
            {
                _inventoryManagementDataService.CloseConnection();
            }

            return(returnResponse);
        }
        /// <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);
        }