Exemple #1
0
            private static NullResponse SaveCart(SaveCartDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.SalesTransactions, "request.SalesTransactions");

                var salesTransactionDataCollection = SalesTransactionConverter.ConvertToData(request.SalesTransactions, serializeData: true);

                using (DataTable table = new DataTable(SalesTransactionTableTypeName))
                {
                    SalesTransactionData.FillSchemaForSave(table);
                    foreach (SalesTransactionData salesTransactionData in salesTransactionDataCollection)
                    {
                        table.Rows.Add(salesTransactionData.CreateDataRow(table));
                    }

                    ParameterSet parameters = new ParameterSet();
                    parameters[SalesTransactionTableTypeVariableName] = table;
                    parameters["@b_IgnoreRowVersionCheck"]            = request.IgnoreRowVersionCheck;

                    try
                    {
                        int errorCode;
                        using (var databaseContext = new SqlServerDatabaseContext(request))
                        {
                            errorCode = databaseContext.ExecuteStoredProcedureNonQuery(SaveSalesTransactionSprocName, parameters);
                        }

                        if (errorCode == (int)DatabaseErrorCodes.VersionMismatchError)
                        {
                            throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectVersionMismatchError, errorCode, "Version mismatch while saving transaction.");
                        }

                        if (errorCode != (int)DatabaseErrorCodes.Success)
                        {
                            throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError, errorCode, "Unable to save transactions.");
                        }
                    }
                    catch (DatabaseException databaseException)
                    {
                        throw new StorageException(
                                  StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError,
                                  (int)databaseException.ErrorCode,
                                  databaseException,
                                  "Unable to save transactions.");
                    }
                }

                return(new NullResponse());
            }
            public void Execute()
            {
                using (var databaseContext = new SqliteDatabaseContext(this.request.RequestContext))
                    using (var transaction = databaseContext.BeginTransaction())
                    {
                        foreach (SalesTransaction salesTransaction in this.request.SalesTransactions)
                        {
                            if (!this.request.IgnoreRowVersionCheck)
                            {
                                // no out of the box rowversion support in sqlite, check row version before performing operation
                                DateTime transactionCreatedDateTime = RetrieveTransactionCreatedDateOrThrowOnInvalidRowVersion(databaseContext, salesTransaction.Id, salesTransaction.Version);

                                // updates the created datetime on the entity, since the update query will replace all columns in the db with the values present in the entity
                                salesTransaction.SetProperty(CreatedDateTimeColumName, new DateTimeOffset(transactionCreatedDateTime));
                            }
                        }

                        var salesTransactionDataCollection = SalesTransactionConverter.ConvertToData(
                            this.request.SalesTransactions,
                            serializeData: true);

                        using (DataTable table = CreateSalesTransactionDataTable())
                        {
                            foreach (SalesTransactionData salesTransactionData in salesTransactionDataCollection)
                            {
                                DateTimeOffset createdDateTimeOffset = this.request.SalesTransactions.First(t => t.Id == salesTransactionData.Id).BeginDateTime;

                                AddSalesTransactionDataRow(table, salesTransactionData, createdDateTimeOffset.DateTime);
                            }

                            // saves or updates records
                            databaseContext.SaveTable(table);
                        }

                        transaction.Commit();
                    }
            }