public HttpResponseMessage add(InspirationImageMap post) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (post.language_id != 0 && Language.MasterPostExists(post.language_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } else if (post.category_id != 0 && Category.MasterPostExists(post.category_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The category does not exist")); } // Make sure that the data is valid post.name = AnnytabDataValidation.TruncateString(post.name, 50); post.image_name = AnnytabDataValidation.TruncateString(post.image_name, 100); // Add the post InspirationImageMap.Add(post);; // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The post has been added")); } // End of the add method
public HttpResponseMessage add(InspirationImageMap post) { // Check for errors if (post == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The post is null"); } else if (post.language_id != 0 && Language.MasterPostExists(post.language_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The language does not exist"); } else if (post.category_id != 0 && Category.MasterPostExists(post.category_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The category does not exist"); } // Make sure that the data is valid post.name = AnnytabDataValidation.TruncateString(post.name, 50); post.image_name = AnnytabDataValidation.TruncateString(post.image_name, 100); // Add the post InspirationImageMap.Add(post);; // Return the success response return Request.CreateResponse<string>(HttpStatusCode.OK, "The post has been added"); } // End of the add method
public HttpResponseMessage update(InspirationImageMap post) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (post.language_id != 0 && Language.MasterPostExists(post.language_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } else if (post.category_id != 0 && Category.MasterPostExists(post.category_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The category does not exist")); } // Make sure that the data is valid post.name = AnnytabDataValidation.TruncateString(post.name, 50); post.image_name = AnnytabDataValidation.TruncateString(post.image_name, 100); // Get the saved post InspirationImageMap savedPost = InspirationImageMap.GetOneById(post.id); // Check if the post exists if (savedPost == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The record does not exist")); } // Update the post InspirationImageMap.Update(post); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The update was successful")); } // End of the update method
public List <InspirationImageMap> get_all(string sortField = "", string sortOrder = "") { // Get a list of image maps List <InspirationImageMap> posts = InspirationImageMap.GetAll(sortField, sortOrder); // Return the list return(posts); } // End of the get_all method
public List <InspirationImageMap> get_by_category_id(Int32 id = 0, Int32 languageId = 0, string sortField = "", string sortOrder = "") { // Get a list of image maps List <InspirationImageMap> posts = InspirationImageMap.GetByCategoryId(id, languageId, sortField, sortOrder); // Return the list return(posts); } // End of the get_by_category_id method
public InspirationImageMap get_by_id(Int32 id = 0) { // Create the post to return InspirationImageMap post = InspirationImageMap.GetOneById(id); // Return the post return(post); } // End of the get_by_id method
public ActionResult delete(Int32 id = 0, string returnUrl = "") { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get query parameters ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } else { // Redirect the user to the start page return RedirectToAction("index", "admin_login"); } // Get the image map InspirationImageMap imageMap = InspirationImageMap.GetOneById(id); // Create an error code variable Int32 errorCode = 0; // Delete the image map post and all the connected posts (CASCADE) errorCode = InspirationImageMap.DeleteOnId(id); // Check if there is an error if (errorCode != 0) { ViewBag.AdminErrorCode = errorCode; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } // Delete the image if (imageMap != null) { DeleteInspirationImage(imageMap.id); } // Redirect the user to the list return Redirect("/admin_inspiration" + returnUrl); } // End of the delete method
} // End of the MasterPostExists method /// <summary> /// Get one image map based on id /// </summary> /// <param name="id">The id for the post</param> /// <returns>A reference to a image map post</returns> public static InspirationImageMap GetOneById(Int32 id) { // Create the post to return InspirationImageMap post = null; // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "SELECT * FROM dbo.inspiration_image_maps WHERE id = @id;"; // The using block is used to call dispose automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@id", id); // Create a MySqlDataReader SqlDataReader reader = null; // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Fill the reader with one row of data. reader = cmd.ExecuteReader(); // Loop through the reader as long as there is something to read and add values while (reader.Read()) { post = new InspirationImageMap(reader); } } catch (Exception e) { throw e; } finally { // Call Close when done reading to avoid memory leakage. if (reader != null) reader.Close(); } } } // Return the post return post; } // End of the GetOneById method
public ActionResult image_map(Int32 id = 0, string returnUrl = "") { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get query parameters ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } else { // Redirect the user to the start page return RedirectToAction("index", "admin_login"); } // Add data to the view ViewBag.Keywords = ""; ViewBag.CurrentPage = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "name", "ASC"); ViewBag.InspirationImageMap = InspirationImageMap.GetOneById(id); ViewBag.ReturnUrl = returnUrl; // Return the view if (ViewBag.InspirationImageMap != null) { // Return the image view return View("image_map"); } else { return Redirect("/admin_inspiration" + returnUrl); } } // End of the image_map method
public Int32 get_count_by_search(string keywords = "") { // Create the string array string[] wordArray = new string[] { "" }; // Recreate the array if keywords is different from null if (keywords != null) { wordArray = keywords.Split(' '); } // Get the count Int32 count = InspirationImageMap.GetCountBySearch(wordArray); // Return the count return(count); } // End of the get_count_by_search method
public HttpResponseMessage delete(Int32 id = 0) { // Create an error code variable Int32 errorCode = 0; // Delete the post errorCode = InspirationImageMap.DeleteOnId(id); // Check if there is an error if (errorCode != 0) { return(Request.CreateResponse <string>(HttpStatusCode.Conflict, "Foreign key constraint")); } // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The delete was successful")); } // End of the delete method
} // End of the constructor #endregion #region Insert methods /// <summary> /// Add one image map /// </summary> /// <param name="post">A reference to a image map post</param> public static long Add(InspirationImageMap post) { // Create the long to return long idOfInsert = 0; // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "INSERT INTO dbo.inspiration_image_maps (language_id, name, image_name, image_map_points, category_id) " + "VALUES (@language_id, @name, @image_name, @image_map_points, @category_id);SELECT SCOPE_IDENTITY();"; // The using block is used to call dispose automatically even if there is a exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there is a exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@language_id", post.language_id); cmd.Parameters.AddWithValue("@name", post.name); cmd.Parameters.AddWithValue("@image_name", post.image_name); cmd.Parameters.AddWithValue("@image_map_points", post.image_map_points); cmd.Parameters.AddWithValue("@category_id", post.category_id); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases try { // Open the connection cn.Open(); // Execute the insert idOfInsert = Convert.ToInt64(cmd.ExecuteScalar()); } catch (Exception e) { throw e; } } } // Return the id of the inserted item return idOfInsert; } // End of the Add method
} // End of the constructor #endregion #region Insert methods /// <summary> /// Add one image map /// </summary> /// <param name="post">A reference to a image map post</param> public static long Add(InspirationImageMap post) { // Create the long to return long idOfInsert = 0; // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "INSERT INTO dbo.inspiration_image_maps (language_id, name, image_name, image_map_points, category_id) " + "VALUES (@language_id, @name, @image_name, @image_map_points, @category_id);SELECT SCOPE_IDENTITY();"; // The using block is used to call dispose automatically even if there is a exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there is a exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@language_id", post.language_id); cmd.Parameters.AddWithValue("@name", post.name); cmd.Parameters.AddWithValue("@image_name", post.image_name); cmd.Parameters.AddWithValue("@image_map_points", post.image_map_points); cmd.Parameters.AddWithValue("@category_id", post.category_id); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases try { // Open the connection cn.Open(); // Execute the insert idOfInsert = Convert.ToInt64(cmd.ExecuteScalar()); } catch (Exception e) { throw e; } } } // Return the id of the inserted item return idOfInsert; } // End of the Add method
public Dictionary <Int32, string> get_all_images() { // Create the dictionary to return Dictionary <Int32, string> imageUrls = new Dictionary <Int32, string>(); // Get all the image maps List <InspirationImageMap> posts = InspirationImageMap.GetAll("id", "ASC"); // Loop all the posts for (int i = 0; i < posts.Count; i++) { // Add the url to the list imageUrls.Add(posts[i].id, Tools.GetInspirationImageUrl(posts[i].id, posts[i].image_name)); } // Return the dictionary return(imageUrls); } // End of the get_all_images method
public List <InspirationImageMap> get_by_search(string keywords = "", Int32 pageSize = 0, Int32 pageNumber = 0, string sortField = "", string sortOrder = "") { // Create the string array string[] wordArray = new string[] { "" }; // Recreate the array if keywords is different from null if (keywords != null) { wordArray = keywords.Split(' '); } // Create the list to return List <InspirationImageMap> posts = InspirationImageMap.GetBySearch(wordArray, pageSize, pageNumber, sortField, sortOrder); // Return the list return(posts); } // End of the get_by_search method
} // End of the delete method #endregion #region Helper methods /// <summary> /// Update the image map image /// </summary> /// <param name="imageMap">A reference to the image map</param> /// <param name="inspirationImage">The uploaded image</param> private void UpdateInspirationImage(InspirationImageMap imageMap, HttpPostedFileBase inspirationImage) { // Create the directory string string inspirationImageDirectory = Tools.GetInspirationImageDirectoryUrl(imageMap.id); // Delete the old image DeleteInspirationImage(imageMap.id); // Check if the directory exists if (System.IO.Directory.Exists(Server.MapPath(inspirationImageDirectory)) == false) { // Create the directory System.IO.Directory.CreateDirectory(Server.MapPath(inspirationImageDirectory)); } // Save the new image inspirationImage.SaveAs(Server.MapPath(inspirationImageDirectory + imageMap.image_name)); } // End of the UpdateInspirationImage method
public string get_image_by_id(Int32 id = 0) { // Create the string to return string url = ""; // Get the post InspirationImageMap post = InspirationImageMap.GetOneById(id); // Make sure that the post not is null if (post == null) { return(url); } // Get the url url = Tools.GetInspirationImageUrl(post.id, post.image_name); // Return the url return(url); } // End of the get_image_by_id method
} // End of the Add method #endregion #region Update methods /// <summary> /// Update a image map post /// </summary> /// <param name="post">A reference to a image map post</param> public static void Update(InspirationImageMap post) { // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "UPDATE dbo.inspiration_image_maps SET language_id = @language_id, name = @name, image_name = @image_name, " + "image_map_points = @image_map_points, category_id = @category_id WHERE id = @id;"; // The using block is used to call dispose automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(connection)) { // The Using block is used to call dispose automatically even if there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@id", post.id); cmd.Parameters.AddWithValue("@language_id", post.language_id); cmd.Parameters.AddWithValue("@name", post.name); cmd.Parameters.AddWithValue("@image_name", post.image_name); cmd.Parameters.AddWithValue("@image_map_points", post.image_map_points); cmd.Parameters.AddWithValue("@category_id", post.category_id); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Execute the update cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } } } } // End of the Update method
public HttpResponseMessage update(InspirationImageMap post) { // Check for errors if (post == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The post is null"); } else if (post.language_id != 0 && Language.MasterPostExists(post.language_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The language does not exist"); } else if (post.category_id != 0 && Category.MasterPostExists(post.category_id) == false) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The category does not exist"); } // Make sure that the data is valid post.name = AnnytabDataValidation.TruncateString(post.name, 50); post.image_name = AnnytabDataValidation.TruncateString(post.image_name, 100); // Get the saved post InspirationImageMap savedPost = InspirationImageMap.GetOneById(post.id); // Check if the post exists if (savedPost == null) { return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The record does not exist"); } // Update the post InspirationImageMap.Update(post); // Return the success response return Request.CreateResponse<string>(HttpStatusCode.OK, "The update was successful"); } // End of the update method
} // End of the Add method #endregion #region Update methods /// <summary> /// Update a image map post /// </summary> /// <param name="post">A reference to a image map post</param> public static void Update(InspirationImageMap post) { // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "UPDATE dbo.inspiration_image_maps SET language_id = @language_id, name = @name, image_name = @image_name, " + "image_map_points = @image_map_points, category_id = @category_id WHERE id = @id;"; // The using block is used to call dispose automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(connection)) { // The Using block is used to call dispose automatically even if there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@id", post.id); cmd.Parameters.AddWithValue("@language_id", post.language_id); cmd.Parameters.AddWithValue("@name", post.name); cmd.Parameters.AddWithValue("@image_name", post.image_name); cmd.Parameters.AddWithValue("@image_map_points", post.image_map_points); cmd.Parameters.AddWithValue("@category_id", post.category_id); // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Execute the update cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } } } } // End of the Update method
public ActionResult edit(FormCollection collection) { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get the return url string returnUrl = collection["returnUrl"]; ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } else { // Redirect the user to the start page return RedirectToAction("index", "admin_login"); } // Get all the form values Int32 id = Convert.ToInt32(collection["txtId"]); Int32 language_id = Convert.ToInt32(collection["selectLanguage"]); string name = collection["txtName"]; Int32 category_id = Convert.ToInt32(collection["selectCategory"]); HttpPostedFileBase inspirationImage = Request.Files["uploadInspirationImage"]; // Get translated texts KeyStringList tt = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); // Get the image map InspirationImageMap imageMap = InspirationImageMap.GetOneById(id); bool postExists = true; // Check if the image map exists if (imageMap == null) { // Create an empty image map imageMap = new InspirationImageMap(); postExists = false; } // Update values imageMap.language_id = language_id; imageMap.name = name; imageMap.category_id = category_id; // Set the image name if (inspirationImage.ContentLength > 0) { imageMap.image_name = System.IO.Path.GetFileName(inspirationImage.FileName); } // Create a error message string errorMessage = ""; // Check for errors in the image map if (imageMap.name.Length > 50) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("name"), "50") + "<br/>"; } if (imageMap.image_name.Length > 100) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("file_name"), "100") + "<br/>"; } // Check if there is errors if (errorMessage == "") { // Check if we should add or update the image map if (postExists == false) { // Add the image map Int64 insertId = InspirationImageMap.Add(imageMap); imageMap.id = Convert.ToInt32(insertId); } else { // Update the image map InspirationImageMap.Update(imageMap); } // Update the image if (inspirationImage.ContentLength > 0) { UpdateInspirationImage(imageMap, inspirationImage); } // Redirect the user to the list return Redirect("/admin_inspiration" + returnUrl); } else { // Set form values ViewBag.ErrorMessage = errorMessage; ViewBag.TranslatedTexts = tt; ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "id", "ASC"); ViewBag.InspirationImageMap = imageMap; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("edit"); } } // End of the edit method
public ActionResult image_map(FormCollection collection) { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get all the form values Int32 id = Convert.ToInt32(collection["txtId"]); string image_map_points = collection["hiddenImageMapPoints"]; string keywords = collection["txtSearch"]; Int32 currentPage = Convert.ToInt32(collection["hiddenPage"]); string returnUrl = collection["returnUrl"]; // Get query parameters ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return View("index"); } else { // Redirect the user to the start page return RedirectToAction("index", "admin_login"); } // Get translated texts KeyStringList tt = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); // Get the image map InspirationImageMap imageMap = InspirationImageMap.GetOneById(id); // Update values imageMap.image_map_points = image_map_points; // Check if the user wants to do a search if (collection["btnSearch"] != null) { // Set form values ViewBag.Keywords = keywords; ViewBag.CurrentPage = 1; ViewBag.InspirationImageMap = imageMap; ViewBag.TranslatedTexts = tt; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("image_map"); } // Check if the user wants to do a search if (collection["btnPreviousPage"] != null) { // Set form values ViewBag.Keywords = keywords; ViewBag.CurrentPage = currentPage - 1; ViewBag.InspirationImageMap = imageMap; ViewBag.TranslatedTexts = tt; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("image_map"); } // Check if the user wants to do a search if (collection["btnNextPage"] != null) { // Set form values ViewBag.Keywords = keywords; ViewBag.CurrentPage = currentPage + 1; ViewBag.InspirationImageMap = imageMap; ViewBag.TranslatedTexts = tt; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("image_map"); } // Check if there is errors if (collection["btnSearchProducts"] == null) { // Update the image map InspirationImageMap.Update(imageMap); // Redirect the user to the list return Redirect("/admin_inspiration" + returnUrl); } else { // Set form values ViewBag.Keywords = keywords; ViewBag.CurrentPage = 1; ViewBag.TranslatedTexts = tt; ViewBag.InspirationImageMap = imageMap; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("image_map"); } } // End of the image_map method
} // End of the MasterPostExists method /// <summary> /// Get one image map based on id /// </summary> /// <param name="id">The id for the post</param> /// <returns>A reference to a image map post</returns> public static InspirationImageMap GetOneById(Int32 id) { // Create the post to return InspirationImageMap post = null; // Create the connection and the sql statement string connection = Tools.GetConnectionString(); string sql = "SELECT * FROM dbo.inspiration_image_maps WHERE id = @id;"; // The using block is used to call dispose automatically even if there are an exception. using (SqlConnection cn = new SqlConnection(connection)) { // The using block is used to call dispose automatically even if there are an exception. using (SqlCommand cmd = new SqlCommand(sql, cn)) { // Add parameters cmd.Parameters.AddWithValue("@id", id); // Create a MySqlDataReader SqlDataReader reader = null; // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to // avoid having our application crash in such cases. try { // Open the connection. cn.Open(); // Fill the reader with one row of data. reader = cmd.ExecuteReader(); // Loop through the reader as long as there is something to read and add values while (reader.Read()) { post = new InspirationImageMap(reader); } } catch (Exception e) { throw e; } finally { // Call Close when done reading to avoid memory leakage. if (reader != null) reader.Close(); } } } // Return the post return post; } // End of the GetOneById method