Beispiel #1
0
        public HttpResponseMessage add(Option post, Int32 languageId = 0)
        {
            // Check for errors
            if (post == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null"));
            }
            else if (Language.MasterPostExists(languageId) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist"));
            }
            else if (OptionType.MasterPostExists(post.option_type_id) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The option type does not exist"));
            }

            // Make sure that the data is valid
            post.title = AnnytabDataValidation.TruncateString(post.title, 50);
            post.product_code_suffix = AnnytabDataValidation.TruncateString(post.product_code_suffix, 10);

            // Add the post
            Int64 insertId = Option.AddMasterPost(post);

            post.id = Convert.ToInt32(insertId);
            Option.AddLanguagePost(post, languageId);

            // Return the success response
            return(Request.CreateResponse <string>(HttpStatusCode.OK, "The post has been added"));
        } // End of the add method
        } // End of the AddOption method

        /// <summary>
        /// Update the option in the database
        /// </summary>
        /// <param name="optionType">A reference to a option type</param>
        /// <param name="options">A list of options</param>
        /// <param name="languageId">A language id</param>
        private void UpdateOption(OptionType optionType, List <Option> options, Int32 languageId)
        {
            // Update the option type
            OptionType.UpdateMasterPost(optionType);
            OptionType.UpdateLanguagePost(optionType, languageId);

            // Get all the saved options
            List <Option> savedOptions = Option.GetByOptionTypeId(optionType.id, languageId);

            // Update or add options
            foreach (Option option in options)
            {
                // Get the saved option
                Option savedOption = Option.GetOneById(option.id, languageId);

                if (savedOption != null)
                {
                    Option.UpdateMasterPost(option);
                    Option.UpdateLanguagePost(option, languageId);
                }
                else
                {
                    long insertId = Option.AddMasterPost(option);
                    option.id = Convert.ToInt32(insertId);
                    Option.AddLanguagePost(option, languageId);
                }
            }

            // Delete options
            foreach (Option savedOption in savedOptions)
            {
                // A boolean to indicate if the id is found
                bool idFound = false;

                // Loop all the input options
                foreach (Option option in options)
                {
                    // Id has been found
                    if (savedOption.id == option.id)
                    {
                        idFound = true;
                    }
                }

                // Delete the id if has not been found
                if (idFound == false)
                {
                    Option.DeleteOnId(savedOption.id);
                }
            }
        } // End of the Update Option method
        } // End of the delete method

        #endregion

        #region Helper methods

        /// <summary>
        /// Add the option to the database
        /// </summary>
        /// <param name="optionType">A reference to a option type</param>
        /// <param name="options">A list of options</param>
        /// <param name="languageId">A language id</param>
        private void AddOption(OptionType optionType, List <Option> options, Int32 languageId)
        {
            // Save the option type
            long insertId = OptionType.AddMasterPost(optionType);

            optionType.id = Convert.ToInt32(insertId);
            OptionType.AddLanguagePost(optionType, languageId);

            // Save all the options
            foreach (Option option in options)
            {
                option.option_type_id = optionType.id;
                insertId  = Option.AddMasterPost(option);
                option.id = Convert.ToInt32(insertId);
                Option.AddLanguagePost(option, languageId);
            }
        } // End of the AddOption method