Exemplo n.º 1
0
        public static PropertySwatchModel DataReader_to_PropertySwatchModel(SqlDataReader reader)
        {
            PropertySwatchModel propertySwatch = new PropertySwatchModel();

            propertySwatch.PropertyID = (Guid)reader["PropertyID"];

            propertySwatch.PropertySwatchID = (Guid)reader["PropertySwatchID"];

            propertySwatch.PropertySwatchImage       = (String)reader["PropertySwatchImage"];
            propertySwatch.PropertySwatchImageMedium = (String)reader["PropertySwatchImageMedium"];
            propertySwatch.PropertySwatchImageSmall  = (String)reader["PropertySwatchImageSmall"];

            propertySwatch.PropertySwatchLabel   = (String)reader["PropertySwatchLabel"];
            propertySwatch.PropertySwatchNameKey = (String)reader["PropertySwatchNameKey"];
            propertySwatch.OrderID     = (int)reader["OrderID"];
            propertySwatch.Visible     = (bool)reader["Visible"];
            propertySwatch.CreatedDate = (DateTime)reader["CreatedDate"];

            return(propertySwatch);
        }
Exemplo n.º 2
0
        //Step 2
        public static DataAccessResponseType CreatePropertySwatch(Account account, PropertyModel property, string swatchImageUrl, string swatchLabelName)
        {
            //TO DO: Always clear/update caches AND update counts!

            #region Verify values
            if (swatchImageUrl == null || swatchImageUrl == "")
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Swatch must have an image!"
                });
            }
            if (swatchLabelName == null || swatchLabelName == "")
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Label must have a value!"
                });
            }
            if (swatchLabelName.Length > 24)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Labels cannot be longer than 24 characters!"
                });
            }
            if (swatchLabelName.Contains("|"))
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Labels cannot contain the pipe character."
                });
            }

            #endregion

            var response = new DataAccessResponseType();

            #region Validate Property Value Name (Depricate ???)

            /*
             * ValidationResponseType ojectNameValidationResponse = ValidationManager.IsValidObjectName(propertyValueName);
             * if (!ojectNameValidationResponse.isValid)
             * {
             *  response.isSuccess = false;
             *  response.ErrorMessage = ojectNameValidationResponse.validationMessage;
             *  response.ErrorMessages.Add(ojectNameValidationResponse.validationMessage);
             *
             *  return response;
             * }
             */
            #endregion

            #region Validate Property is a swatch

            if (property.PropertyTypeNameKey != "swatch")
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "This property cannot be assigned swatch values"
                });
            }

            #endregion

            #region Verify value does not already exist on this property

            foreach (PropertySwatchModel propertySwatchModel in property.Swatches)
            {
                if (propertySwatchModel.PropertySwatchNameKey == Common.Methods.ObjectNames.ConvertToObjectNameKey(swatchLabelName))
                {
                    return(new DataAccessResponseType
                    {
                        isSuccess = false,
                        ErrorMessage = "This swatch already has this label assigned."
                    });
                }
            }

            #endregion

            var propertySwatch = new PropertySwatchModel
            {
                PropertyID                = property.PropertyID,
                PropertySwatchImage       = swatchImageUrl,
                PropertySwatchImageMedium = swatchImageUrl.Replace(".jpg", "_md.jpg"),
                PropertySwatchImageSmall  = swatchImageUrl.Replace(".jpg", "_sm.jpg"),
                PropertySwatchID          = Guid.NewGuid(),
                PropertySwatchLabel       = swatchLabelName,
                PropertySwatchNameKey     = Sahara.Core.Common.Methods.ObjectNames.ConvertToObjectNameKey(swatchLabelName),
                Visible = true
            };

            response = Sql.Statements.InsertStatements.InsertPropertySwatch(account.SqlPartition, account.SchemaName, propertySwatch);

            if (response.isSuccess)
            {
                //Clear Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);
            }

            response.SuccessMessage = propertySwatch.PropertySwatchID.ToString(); //<--Returned for logging purposes

            return(response);
        }
Exemplo n.º 3
0
        public static DataAccessResponseType InsertPropertySwatch(string sqlPartition, string schemaId, PropertySwatchModel propertySwatch)
        {
            DataAccessResponseType response = new DataAccessResponseType();

            StringBuilder SqlStatement = new StringBuilder();


            //newAccountModel.Provisioned = false;

            //SQL Statement =============================================================
            SqlStatement.Append("INSERT INTO  ");
            SqlStatement.Append(schemaId);
            SqlStatement.Append(".PropertySwatch (");

            SqlStatement.Append("PropertyID,");
            SqlStatement.Append("PropertySwatchID,");
            SqlStatement.Append("PropertySwatchImage,");
            SqlStatement.Append("PropertySwatchImageMedium,");
            SqlStatement.Append("PropertySwatchImageSmall,");
            SqlStatement.Append("PropertySwatchLabel,");
            SqlStatement.Append("PropertySwatchNameKey,");
            SqlStatement.Append("CreatedDate, ");
            SqlStatement.Append("Visible");

            SqlStatement.Append(") VALUES (");

            //Using parameterized queries to protect against injection
            SqlStatement.Append("@PropertyID, ");
            SqlStatement.Append("@PropertySwatchID, ");
            SqlStatement.Append("@PropertySwatchImage, ");
            SqlStatement.Append("@PropertySwatchImageMedium, ");
            SqlStatement.Append("@PropertySwatchImageSmall, ");
            SqlStatement.Append("@PropertySwatchLabel, ");
            SqlStatement.Append("@PropertySwatchNameKey, ");
            SqlStatement.Append("@CreatedDate, ");
            SqlStatement.Append("@Visible");

            SqlStatement.Append(")");

            //SqlCommand sqlCommand = new SqlCommand(SqlStatement.ToString(), Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition));
            SqlCommand sqlCommand = Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition).CreateCommand();

            sqlCommand.CommandText = SqlStatement.ToString();



            //Using parameterized queries to protect against injection
            sqlCommand.Parameters.Add("@PropertyID", SqlDbType.UniqueIdentifier);
            sqlCommand.Parameters.Add("@PropertySwatchID", SqlDbType.UniqueIdentifier);
            sqlCommand.Parameters.Add("@PropertySwatchImage", SqlDbType.Text);
            sqlCommand.Parameters.Add("@PropertySwatchImageMedium", SqlDbType.Text);
            sqlCommand.Parameters.Add("@PropertySwatchImageSmall", SqlDbType.Text);
            sqlCommand.Parameters.Add("@PropertySwatchLabel", SqlDbType.Text);
            sqlCommand.Parameters.Add("@PropertySwatchNameKey", SqlDbType.Text);
            sqlCommand.Parameters.Add("@CreatedDate", SqlDbType.DateTime);
            sqlCommand.Parameters.Add("@Visible", SqlDbType.Bit);

            //Assign values
            sqlCommand.Parameters["@PropertyID"].Value                = propertySwatch.PropertyID;
            sqlCommand.Parameters["@PropertySwatchID"].Value          = propertySwatch.PropertySwatchID;
            sqlCommand.Parameters["@PropertySwatchImage"].Value       = propertySwatch.PropertySwatchImage;
            sqlCommand.Parameters["@PropertySwatchImageMedium"].Value = propertySwatch.PropertySwatchImageMedium;
            sqlCommand.Parameters["@PropertySwatchImageSmall"].Value  = propertySwatch.PropertySwatchImageSmall;
            sqlCommand.Parameters["@PropertySwatchLabel"].Value       = propertySwatch.PropertySwatchLabel;
            sqlCommand.Parameters["@PropertySwatchNameKey"].Value     = propertySwatch.PropertySwatchNameKey;
            sqlCommand.Parameters["@CreatedDate"].Value               = DateTime.UtcNow;
            sqlCommand.Parameters["@Visible"].Value = propertySwatch.Visible;

            int insertAccountResult = 0;

            sqlCommand.Connection.OpenWithRetry();

            try
            {
                insertAccountResult = sqlCommand.ExecuteNonQueryWithRetry(); // returns Int indicating number of rows affected
                if (insertAccountResult > 0)
                {
                    response.isSuccess = true;
                }
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to insert a property value into SQL",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;
                return(response);
            }

            sqlCommand.Connection.Close();

            return(response);
        }
Exemplo n.º 4
0
        public static DataAccessResponseType DeletePropertySwatch(Account account, PropertyModel property, PropertySwatchModel propertySwatch)
        {
            var response = new DataAccessResponseType();

            #region Check if property value is currently in use on ANY documents for this account

            #region Get any relevant documents (Legacy)

            /*
             * //Get the DocumentDB Client
             * var client = Sahara.Core.Settings.Azure.DocumentDB.DocumentClients.AccountDocumentClient;
             * //var dbSelfLink = Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseSelfLink;
             * client.OpenAsync();
             *
             * string sqlQuery = "SELECT Top 1 * FROM Products p WHERE p.Swatches." + property.PropertyName + " = '" + propertySwatch.PropertySwatchLabel + "'";
             *
             * //Build a collection Uri out of the known IDs
             * //(These helpers allow you to properly generate the following URI format for Document DB:
             * //"dbs/{xxx}/colls/{xxx}/docs/{xxx}"
             * Uri collectionUri = UriFactory.CreateDocumentCollectionUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition);
             * //Uri storedProcUri = UriFactory.CreateStoredProcedureUri(Sahara.Core.Settings.Azure.DocumentDB.AccountPartitionDatabaseId, account.DocumentPartition, "UpdateProduct");
             *
             * var document = client.CreateDocumentQuery<Document>(collectionUri.ToString(), sqlQuery).AsEnumerable().FirstOrDefault();
             */
            #endregion

            #region Get any relevant documents (from Search)

            //$filter=tags/any(t: t eq '345')
            string searchFilter = property.SearchFieldName + "/any(s: s eq '" + propertySwatch.PropertySwatchLabel + "')";

            var documentSearchResult = ProductSearchManager.SearchProducts(account.SearchPartition, account.ProductSearchIndex, "", searchFilter, "relevance", 0, 1);

            #endregion

            #endregion

            if (documentSearchResult.Count > 0)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Cannot delete a swatch that is in use on a product. Please remove all associations before deleting."
                });
            }
            else
            {
                response.isSuccess = Sql.Statements.DeleteStatements.DeletePropertySwatch(account.SqlPartition, account.SchemaName, propertySwatch.PropertySwatchID.ToString());
            }

            if (response.isSuccess)
            {
                //Clear Caches:
                Caching.InvalidateAllPropertyCachesForAccount(account.AccountNameKey);

                #region Delete all associated images for the swatch being deleted

                /*
                 * try
                 * {
                 * //TODO (Really not very important)
                 * }
                 * catch
                 * {
                 *
                 * }
                 */
                #endregion
            }

            return(response);
        }