public HttpResponseMessage update(OptionType 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")); } // Make sure that the data is valid post.title = AnnytabDataValidation.TruncateString(post.title, 100); post.google_name = AnnytabDataValidation.TruncateString(post.google_name, 50); // Get the saved post OptionType savedPost = OptionType.GetOneById(post.id, languageId); // Check if the post exists if (savedPost == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The record does not exist")); } // Update the post OptionType.UpdateMasterPost(post); OptionType.UpdateLanguagePost(post, languageId); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The update was successful")); } // End of the update 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