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 review
            ProductReview review = ProductReview.GetOneById(id);

            // Create an error code variable
            Int32 errorCode = 0;

            // Make sure that the review not is null
            if(review != null)
            {
                // Delete the review
                errorCode = ProductReview.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");
                }

                // Update the product rating
                Product.UpdateRating(review.product_id, review.language_id);
            }
            
            // Redirect the user to the list
            return Redirect("/admin_product_reviews" + returnUrl);

        } // End of the delete method
        public HttpResponseMessage delete(Int32 id = 0)
        {
            // Create an error code variable
            Int32 errorCode = 0;

            // Delete the post
            errorCode = ProductReview.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
Exemple #3
0
    } // End of the GetEmailAddresses method

    #endregion

    #region Delete methods

    /// <summary>
    /// Delete a customer post on id
    /// </summary>
    /// <param name="id">The id number for the customer</param>
    /// <returns>An error code</returns>
    public static Int32 DeleteOnId(Int32 id)
    {
        // Delete reviews by customer id (Update product rating)
        Int32 page = 1;
        List<ProductReview> productReviews = ProductReview.GetByCustomerId(id, 10, page, "id", "ASC");
        while (productReviews.Count > 0)
        {
            for (int i = 0; i < productReviews.Count; i++)
            {
                ProductReview.DeleteOnId(productReviews[i].id);
                Product.UpdateRating(productReviews[i].product_id, productReviews[i].language_id);
            }

            // Increase the page number and get more posts
            page = page + 1;
            productReviews = ProductReview.GetByCustomerId(id, 10, page, "id", "ASC");
        }

        // Delete orders by customer id
        page = 1;
        List<Order> orders = Order.GetByCustomerId(id, 10, page, "id", "ASC");
        while (orders.Count > 0)
        {
            for (int i = 0; i < orders.Count; i++)
            {
                Order.DeleteOnId(orders[i].id);
            }

            // Increase the page number and get more posts
            page = page + 1;
            orders = Order.GetByCustomerId(id, 10, page, "id", "ASC");
        }

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "DELETE FROM dbo.customers_files WHERE customer_id = @id;DELETE FROM dbo.customers WHERE id = @id;";

        // 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))
            {
                // Set command timeout to 90 seconds
                cmd.CommandTimeout = 90;

                // Add parameters
                cmd.Parameters.AddWithValue("@id", 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 (SqlException e)
                {
                    // Check for a foreign key constraint error
                    if (e.Number == 547)
                    {
                        return 5;
                    }
                    else
                    {
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

        // Return the code for success
        return 0;

    } // End of the DeleteOnId method