Exemplo n.º 1
0
        public ActionResult delete_rating(Int32 id = 0, Int32 languageId = 0)
        {
            // Get the signed in user
            Administrator user = Administrator.GetSignedInAdministrator();

            // Check if the post request is valid
            if (user == null)
            {
                return RedirectToAction("login", "user");
            }

            // Get the current domain
            Domain domain = Tools.GetCurrentDomain();

            // Get the rating
            PostRating postRating = PostRating.GetOneById(id, user.id, languageId);

            // Delete the rating
            if (postRating != null && postRating.administrator_id == user.id)
            {
                // Delete the rating
                PostRating.DeleteOnId(id, user.id, languageId);

                // Update the rating for the post
                Post.UpdateRating(postRating.post_id, postRating.language_id);
            }

            // Return the edit ratings view
            return RedirectToAction("edit_ratings");

        } // End of the delete_rating method
        public ActionResult delete(Int32 id = 0, Int32 administratorId = 0, Int32 languageId = 0, string returnUrl = "/admin_ratings")
        {
            // Get the current domain
            Domain currentDomain = Tools.GetCurrentDomain();
            ViewBag.CurrentDomain = currentDomain;

            // Get query parameters
            ViewBag.QueryParams = new QueryParams(returnUrl);

            // Get the signed in administrator
            Administrator administrator = Administrator.GetSignedInAdministrator();

            // Get the post rating
            PostRating postRating = PostRating.GetOneById(id, administratorId, languageId);

            // Check if the administrator is authorized
            if (Administrator.IsAuthorized(new string[] { "Administrator" }) == true)
            {
                ViewBag.AdminSession = true;
            }
            else if (administrator != null && administrator.admin_role == "Author" && 
                (postRating == null || postRating.administrator_id == administrator.id))
            {
                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 rating post
            PostRating rating = PostRating.GetOneById(id, administratorId, languageId);

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

            // Make sure that the rating not is null
            if (rating != null)
            {
                // Delete the rating
                errorCode = PostRating.DeleteOnId(id, administratorId, languageId);

                // 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 post rating
                Post.UpdateRating(rating.post_id, rating.language_id);
            }

            // Redirect the user to the list
            return Redirect(returnUrl);

        } // End of the delete method
Exemplo n.º 3
0
    } // End of the GetSignedInAdministrator method

    #endregion

    #region Delete methods

    /// <summary>
    /// Delete a administrator post on id
    /// </summary>
    /// <param name="id">The id of the administrator post</param>
    /// <returns>An error code</returns>
    public static Int32 DeleteOnId(Int32 id)
    {
        // Delete post comments by administrator id
        PostComment.DeleteOnAdministratorId(id);

        // Delete post ratings by administrator id
        List <PostRating> postRatings = PostRating.GetAllByAdministratorId(id);

        for (int i = 0; i < postRatings.Count; i++)
        {
            PostRating.DeleteOnId(postRatings[i].post_id, postRatings[i].administrator_id, postRatings[i].language_id);
            Post.UpdateRating(postRatings[i].post_id, postRatings[i].language_id);
        }

        // Delete posts by administrator id
        List <Post> posts = Post.GetAllByAdministratorId(id);

        for (int i = 0; i < posts.Count; i++)
        {
            Post.DeleteOnId(posts[i].id);
        }

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql        = "DELETE FROM dbo.administrators_detail WHERE administrator_id = @id;DELETE FROM dbo.administrators 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))
            {
                // 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