Esempio n. 1
0
        /// <summary>
        /// Create Supplier
        /// </summary>
        /// <param name="productDataTransformation"></param>
        /// <returns></returns>
        public async Task <ResponseModel <SupplierDataTransformation> > CreateSupplier(SupplierDataTransformation supplierDataTransformation)
        {
            ResponseModel <SupplierDataTransformation> returnResponse = new ResponseModel <SupplierDataTransformation>();

            Supplier supplier = new Supplier();

            try
            {
                _purchaseOrderManagementDataService.OpenConnection(_connectionStrings.PrimaryDatabaseConnectionString);
                _purchaseOrderManagementDataService.BeginTransaction((int)IsolationLevel.Serializable);

                SupplierBusinessRules <SupplierDataTransformation> supplierBusinessRules = new SupplierBusinessRules <SupplierDataTransformation>(supplierDataTransformation, _purchaseOrderManagementDataService);
                ValidationResult validationResult = await supplierBusinessRules.Validate();

                if (validationResult.ValidationStatus == false)
                {
                    _purchaseOrderManagementDataService.RollbackTransaction();

                    returnResponse.ReturnMessage = validationResult.ValidationMessages;
                    returnResponse.ReturnStatus  = validationResult.ValidationStatus;

                    return(returnResponse);
                }

                supplier.AccountId    = supplierDataTransformation.AccountId;
                supplier.Name         = supplierDataTransformation.SupplierName;
                supplier.AddressLine1 = supplierDataTransformation.AddressLine1;
                supplier.AddressLine2 = supplierDataTransformation.AddressLine2;
                supplier.City         = supplierDataTransformation.City;
                supplier.Region       = supplierDataTransformation.Region;
                supplier.PostalCode   = supplierDataTransformation.PostalCode;

                await _purchaseOrderManagementDataService.CreateSupplier(supplier);

                //await _purchaseOrderManagementDataService.UpdateDatabase();

                _purchaseOrderManagementDataService.CommitTransaction();

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

            supplierDataTransformation.SupplierId = supplier.SupplierId;

            returnResponse.Entity = supplierDataTransformation;

            return(returnResponse);
        }
Esempio n. 2
0
        /// <summary>
        /// Send Queue Messages
        /// </summary>
        /// <param name="messageQueueing"></param>
        /// <param name="outboundSemaphoreKey"></param>
        /// <returns></returns>
        public async Task <ResponseModel <List <MessageQueue> > > SendQueueMessages(List <IMessageQueueConfiguration> messageQueueConfigurations, string outboundSemaphoreKey, ConnectionStrings connectionStrings)
        {
            ResponseModel <List <MessageQueue> > returnResponse = new ResponseModel <List <MessageQueue> >();

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

            lock (_sendingLock)
            {
                if (_sending)
                {
                    Console.WriteLine("Aborted iteration still sending");
                    return(returnResponse);
                }

                _sending = true;
            }

            TransactionQueueSemaphore transactionQueueSemaphore = null;

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

                transactionQueueSemaphore = await _purchaseOrderManagementDataService.GetTransactionQueueSemaphore(outboundSemaphoreKey);

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

                List <TransactionQueueOutbound> transactionQueue = await _purchaseOrderManagementDataService.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 _purchaseOrderManagementDataService.UpdateOutboundTransactionQueue(transactionQueueItem);

                        returnResponse.Entity.Add(message);
                    }
                }

                await _purchaseOrderManagementDataService.UpdateDatabase();

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

            return(returnResponse);
        }